Skip to content

Commit

Permalink
admin can upload and remove images from a campaign
Browse files Browse the repository at this point in the history
  • Loading branch information
mikedao committed Jun 5, 2023
1 parent d9e9d78 commit e38a258
Show file tree
Hide file tree
Showing 19 changed files with 35 additions and 13 deletions.
8 changes: 8 additions & 0 deletions app/controllers/admin/campaigns/image_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Admin::Campaigns::ImageController < Admin::BaseController

def destroy
campaign = Campaign.find(params[:campaign_id])
campaign.image.purge_later
redirect_to edit_admin_campaign_path(campaign), notice: "Image successfully removed."
end
end
6 changes: 3 additions & 3 deletions app/controllers/admin/campaigns_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def new

def create
campaign = Campaign.create(campaign_params)
redirect_to admin_campaigns_path
redirect_to admin_campaign_path(campaign)
end

def show
Expand All @@ -23,7 +23,7 @@ def edit
def update
campaign = Campaign.find(params[:id])
campaign.update(campaign_params)
redirect_to admin_campaigns_path
redirect_to admin_campaign_path(campaign)
end

def destroy
Expand All @@ -34,6 +34,6 @@ def destroy

private
def campaign_params
params.require(:campaign).permit(:title, :description, :image_url)
params.require(:campaign).permit(:title, :description, :image)
end
end
1 change: 1 addition & 0 deletions app/models/campaign.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class Campaign < ApplicationRecord
has_rich_text :description
has_one_attached :image

has_many :campaign_users
has_many :users, through: :campaign_users
Expand Down
2 changes: 1 addition & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class User < ApplicationRecord
validates :username, uniqueness: true, presence: true
validates_presence_of :password, :first_name, :last_name, :email

has_secure_password
has_secure_password

enum role: [:default, :admin]

Expand Down
8 changes: 8 additions & 0 deletions app/views/admin/campaigns/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@
<div class="field">
<%= form.label :title %>
<%= form.text_area :title %>
<%= form.label :image %>
<% if @campaign.image.present? %>
<div id="<%= dom_id(@campaign, :image) %>">
<%= image_tag @campaign.image.representation(resize_to_limit: [320, 180]) %>
<%= link_to "Remove Image", admin_campaign_image_path(@campaign), data: { turbo_method: :delete, turbo_confirm: "Are you sure?" } %>
</div>
<% end %>
<%= form.file_field :image %>
<%= form.label :description %>
<%= form.rich_text_area :description %>
<%= form.submit "Update Campaign" %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/admin/campaigns/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<div class='campaigns'>
<% @campaigns.each do |campaign| %>
<div id="<%= campaign.title.parameterize %>">
<p><%= image_tag campaign.image_url if campaign.image_url %></p>
<p><%= image_tag campaign.image if campaign.image.present? %></p>
<p><h2><%= campaign.title %></h2></p>
<p><%= campaign.description %></p>
<p><%= link_to "Details", admin_campaign_path(campaign) %></p>
Expand Down
2 changes: 2 additions & 0 deletions app/views/admin/campaigns/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
<div class="field">
<%= form.label :title %>
<%= form.text_area :title %>
<%= form.label :image %>
<%= form.file_field :image %>
<%= form.label :description %>
<%= form.rich_text_area :description %>
<%= form.submit "Create Campaign" %>
Expand Down
3 changes: 3 additions & 0 deletions app/views/admin/campaigns/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
<div>
<p><h2><%= @campaign.title %></h2></p>
</div>
<div>
<%= image_tag @campaign.image if @campaign.image.present? %>
</div>
<div>
<p><%= @campaign.description %></p>
</div>
Expand Down
2 changes: 1 addition & 1 deletion app/views/admin/dashboard/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<div class='campaigns'>
<% @campaigns.each do |campaign| %>
<div id="<%= dom_id(campaign) %>">
<p><%= image_tag campaign.image_url %></p>
<p><%= image_tag campaign.image.representation(resize_to_limit: [320, 180]) if campaign.image.present? %></p>
<p><h2><%= campaign.title %></h2></p>
<p><%= campaign.description %></p>
<p><%= link_to "Details", admin_campaign_path(campaign) %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/welcome/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<% @campaigns.each do |campaign| %>
<div id="<%= dom_id(campaign) %>">
<div>
<p><%= image_tag campaign.image_url %></p>
<p><%= image_tag campaign.image.representation(resize_to_limit: [320, 180]) if campaign.image.present? %></p>
</div>
<div>
<p><h2><%= campaign.title %></h2></p>
Expand Down
4 changes: 3 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@

namespace :admin do
get "/dashboard", to: "dashboard#index"
resources :campaigns
resources :campaigns do
resources :image, only: [:destroy], module: :campaigns
end
resources :characters, except: [:new, :create]
end

Expand Down
1 change: 0 additions & 1 deletion db/migrate/20230602145506_create_campaigns.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ class CreateCampaigns < ActiveRecord::Migration[7.0]
def change
create_table :campaigns do |t|
t.string :title
t.string :image_url

t.timestamps
# ! This migration also creates a rich text field called description
Expand Down
1 change: 0 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added spec/.DS_Store
Binary file not shown.
2 changes: 1 addition & 1 deletion spec/factories/campaign.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
factory :campaign do
title { "#{Faker::Cosmere.surge}-#{Faker::Cosmere.surge}-#{Faker::Number.hexadecimal(digits: 4)}" }
description { Faker::Quotes::Shakespeare.hamlet_quote + Faker::Number.hexadecimal(digits: 4) }
image_url { Faker::Placeholdit.image }
image { Rack::Test::UploadedFile.new('spec/fixtures/files/kingdom.webp', 'image/webp') }
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

find(:xpath, "//\*[@id='campaign_description_trix_input_campaign']", visible: false).set('Campaign Description')
click_on "Create Campaign"
expect(current_path).to eq(admin_campaigns_path)
expect(current_path).to eq(admin_campaign_path(Campaign.last))
expect(page).to have_content("Campaign Title")
expect(page).to have_content("Campaign Description")
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
find(:xpath, "//\*[@id='campaign_description_trix_input_campaign_#{campaign.id}']", visible: false).set('Changed field')
click_on 'Update Campaign'

expect(current_path).to eq(admin_campaigns_path)
expect(current_path).to eq(admin_campaign_path(campaign))

expect(page).to have_content('Totally Edited')
expect(page).to have_content('Changed field')
Expand Down
Binary file added spec/fixtures/.DS_Store
Binary file not shown.
Binary file added spec/fixtures/files/kingdom.webp
Binary file not shown.

0 comments on commit e38a258

Please sign in to comment.