diff --git a/app/controllers/spree/admin/banners_controller.rb b/app/controllers/spree/admin/banners_controller.rb index c300fe3..16b2a54 100644 --- a/app/controllers/spree/admin/banners_controller.rb +++ b/app/controllers/spree/admin/banners_controller.rb @@ -3,9 +3,10 @@ module Admin class BannersController < ResourceController before_action :load_banner, only: [:edit, :update, :destroy, :toggle_banner_active_status] + before_action :load_folders, only: [:new, :create, :edit, :update] + before_action :create_banner_and_images, only: [:create] def create - @banner = Banner.new(permitted_params) if @banner.save flash[:success] = Spree.t(:successfully_created_banner) redirect_to admin_banners_path @@ -40,6 +41,18 @@ def update private + def create_banner_and_images + @banner = Banner.new(permitted_params) + if permitted_resource_params[:images][:digital_asset_id].present? + @banner.images << Spree::Image.new(permitted_resource_params[:images]) + end + end + + def load_folders + @folders = Spree::Folder.all + @digital_assets = Spree::DigitalAsset.approved.page(params[:page]) + end + def permitted_params params.require(:banner).permit(:title, :attachment, :link, :active, :mobile_banner) end diff --git a/app/helpers/spree/admin/base_helper_decorator.rb b/app/helpers/spree/admin/base_helper_decorator.rb index 9abc420..f4393e1 100644 --- a/app/helpers/spree/admin/base_helper_decorator.rb +++ b/app/helpers/spree/admin/base_helper_decorator.rb @@ -65,8 +65,8 @@ def asset_details(digital_asset) def related_products(digital_asset) products = {} digital_asset.assets.each do |asset| - product = asset.viewable.product - products[product.id] = { slug: product.slug, name: product.name } + product = asset.viewable.try(:product) + products[product.id] = { slug: product.slug, name: product.name } if product end products.values end diff --git a/app/models/spree/banner.rb b/app/models/spree/banner.rb index 1ac0eb5..95196ae 100644 --- a/app/models/spree/banner.rb +++ b/app/models/spree/banner.rb @@ -10,11 +10,13 @@ class Banner < Spree::Base validates_attachment :attachment, content_type: { content_type: ["image/jpg", "image/jpeg", "image/png", "image/gif"] } validate :only_one_mobile_banner + has_many :images, -> { order(:position) }, as: :viewable, dependent: :destroy, class_name: 'Spree::Image' with_options presence: true do validates :title, uniqueness: true validates :link - validates :attachment + validates :attachment, unless: :images + validates :images, unless: :attachment end validates_format_of :link, with: URL_VALIDATION_REGEX, multiline: true, allow_blank: true @@ -24,6 +26,7 @@ class Banner < Spree::Base before_destroy :restrict_if_active + def change_active_status if active? update_columns(active: false, mobile_banner: false) diff --git a/app/views/spree/admin/banners/_form.html.erb b/app/views/spree/admin/banners/_form.html.erb index d494a2e..e476c51 100644 --- a/app/views/spree/admin/banners/_form.html.erb +++ b/app/views/spree/admin/banners/_form.html.erb @@ -12,9 +12,12 @@ <% end %> -
+
<%= f.field_container :attachment do %> <%= f.label :attachment %>*
+ <%= f.fields_for :images, @banner.images.build do |form| %> + <%= render partial: "spree/admin/images/upload_digital_asset", locals: { f: form } %> + <% end %> <%= f.file_field :attachment, class: 'form-control select-file btn btn-default' %>

Upload pic with approximate 1022 × 60 dimensions

<% end %> diff --git a/app/views/spree/admin/banners/index.html.erb b/app/views/spree/admin/banners/index.html.erb index 138bf19..3d15af0 100644 --- a/app/views/spree/admin/banners/index.html.erb +++ b/app/views/spree/admin/banners/index.html.erb @@ -6,40 +6,50 @@ <%= button_link_to Spree.t(:new_banner), spree.new_admin_banner_path, icon: 'arrow-left', class: 'btn btn-primary' %> <% end %> - - - - - - - - - - - - - - <% @banners.each do |banner| %> - - - - - - - + + + + <% end %> + +
<%= Spree.t(:Title) %><%= Spree.t(:Link) %><%= Spree.t(:image) %><%= Spree.t(:active) %><%= Spree.t(:mobile_banner) %>
+<% else %> +
+ <%= Spree.t(:no_resource_found, resource: plural_resource_name(Spree::Banner)) %>, + <%= link_to Spree.t(:add_one), new_object_url if can?(:create, Spree::Banner) %>! +
+<% end %> <%= paginate @banners %> diff --git a/config/routes.rb b/config/routes.rb index 8920232..ef4c02b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -5,6 +5,7 @@ resources :folders, only: [:create, :update, :destroy] resources :banners, except: [:show] do patch :toggle_banner_active_status, on: :member + resources :images end end