Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
246 changes: 246 additions & 0 deletions app/controllers/spree/admin/images_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,246 @@
module Spree
module Admin
class ImagesController < ResourceController
before_action :load_edit_data, except: :index
before_action :load_index_data, only: :index

create.before :set_viewable
update.before :set_viewable

def edit
if params[:vendor_id].present?
@vendor = Spree::Vendor.friendly.find(params[:vendor_id])
render 'edit_vendor'
elsif params[:auction_id].present?
@auction = Spree::Auction.find(params[:auction_id])
render "edit_auction"
else
respond_with(@object) do |format|
format.html { render layout: !request.xhr? }
format.js { render layout: false } if request.xhr?
end
end
end

def update
invoke_callbacks(:update, :before)
if @object.update(permitted_resource_params)
invoke_callbacks(:update, :after)
if params[:vendor_id].present?
@vendor = Spree::Vendor.friendly.find(params[:vendor_id])
redirect_vendor_image
elsif params[:auction_id].present?
@auction = Spree::Auction.find(params[:auction_id])
render "auction_index"
else
respond_with(@object) do |format|
format.html do
flash[:success] = flash_message_for(@object, :successfully_updated)
redirect_to location_after_save
end
format.js { render layout: false }
end
end

else
invoke_callbacks(:update, :fails)
if params[:vendor_id].present?
@vendor = Spree::Vendor.friendly.find(params[:vendor_id])
redirect_vendor_image
elsif params[:auction_id].present?
render "auction_index"
else
respond_with(@object) do |format|
format.html { render action: :edit }
format.js { render layout: false }
end
end

end
end

def destroy
invoke_callbacks(:destroy, :before)
if @object.destroy
if params[:vendor_id].present?
@vendor = Spree::Vendor.friendly.find(params[:vendor_id])
# redirect_vendor_image
respond_with(@vendor) do |format|
format.html {render :js => "window.location.href='"+request.referer+"'" }
format.js { render :js => "window.location.href='"+request.referer+"'" }
end
return
elsif params[:auction_id].present?
@auction = Spree::Auction.find(params[:auction_id])
respond_with(@auction) do |format|
format.html {render :js => "window.location.href='"+request.referer+"'" }
format.js { render :js => "window.location.href='"+request.referer+"'" }
end
return
else
invoke_callbacks(:destroy, :after)
flash[:success] = flash_message_for(@object, :successfully_removed)
end

else
if params[:vendor_id].present?
@vendor = Spree::Vendor.friendly.find(params[:vendor_id])
# redirect_vendor_image
respond_with(@vendor) do |format|
format.html {render :js => "window.location.href='"+request.referer+"'" }
format.js { render :js => "window.location.href='"+request.referer+"'" }
end
return
elsif params[:auction_id].present?
@auction = Spree::Auction.find(params[:auction_id])
respond_with(@auction) do |format|
format.html {render :js => "window.location.href='"+request.referer+"'" }
format.js { render :js => "window.location.href='"+request.referer+"'" }
end
return
else
invoke_callbacks(:destroy, :fails)
flash[:error] = @object.errors.full_messages.join(', ')
end

end

end

def create
invoke_callbacks(:create, :before)
@object.attributes = permitted_resource_params
if @object.save
if params[:vendor_id].present?
@vendor = Spree::Vendor.friendly.find(params[:vendor_id])
redirect_vendor_image
elsif params[:auction_id].present?
@auction = Spree::Auction.find(params[:auction_id])
render "auction_index"
else
invoke_callbacks(:create, :after)
flash[:success] = flash_message_for(@object, :successfully_created)
respond_with(@object) do |format|
format.html { redirect_to location_after_save }
format.js { render layout: false }
end
end

else
if params[:vendor_id].present?
redirect_vendor_image
elsif params[:auction_id].present?
render "auction_index"
end
invoke_callbacks(:create, :fails)
respond_with(@object) do |format|
format.html { render action: :new }
format.js { render layout: false }
end
end
end

def redirect_vendor_image
render "vendor_index"
end

private

def location_after_destroy
admin_product_images_url(@product)
end

def location_after_save
admin_product_images_url(@product)
end

def load_index_data
if params[:vendor_id].present?
@vendor = Vendor.friendly.find(params[:vendor_id])
render "vendor_index"
elsif params[:auction_id].present?
@auction = Auction.find(params[:auction_id])
render "auction_index"
else
@product = Product.friendly.includes(*variant_index_includes).find(params[:product_id])
end
end

def load_edit_data
if params[:vendor_id].present?
load_edit_data_vendors
if params[:action] == "new"
render "spree/admin/images/new_vendor"
end
elsif params[:auction_id].present?
load_edit_data_auctions
if params[:action] == "new"
render "spree/admin/images/new_auction"
end
else
load_edit_data_products
end
end

def load_edit_data_products
@product = Product.friendly.includes(*variant_edit_includes).find(params[:product_id])
root_taxon = get_root_taxon
@taxons = root_taxon.present? ? root_taxon.taxons.where(name: "product").first.children : nil
@variants = @product.variants.map do |variant|
[variant.sku_and_options_text, variant.id]
end
@variants.insert(0, [Spree.t(:all), @product.master.id])
end

def load_edit_data_vendors
@vendors = Vendor.friendly.find(params[:vendor_id])
# @taxons = Spree::Taxonomy.includes(root: :children).find_by(name: 'Image_Category').taxons.select{|s|s.children.empty?}
root_taxon = get_root_taxon
@taxons = root_taxon.present? ? root_taxon.taxons.where(name: "vendor").first.children : nil
end

def load_edit_data_auctions
@auctions = Auction.find(params[:auction_id])
root_taxon = get_root_taxon
@taxons = root_taxon.present? ? root_taxon.taxons.where(name: "auction").first.children : nil
end

def get_root_taxon
Spree::Taxonomy.includes(root: :children).find_by(name: 'Image')
end

def set_viewable
if params[:vendor_id].present?
@image.viewable_type = 'Spree::Vendor'
@image.viewable_id = Spree::Vendor.friendly.find(params[:vendor_id]).id
# @taxons = Spree::Taxonomy.includes(root: :children).find_by(name: 'Image_Category').taxons.select{|s|s.children.empty?}
@taxons = Spree::Taxonomy.includes(root: :children).find_by(name: 'Image').taxons.where(name: "vendor").first.children
# render "new_vendor"
elsif params[:auction_id].present?
@image.viewable_type = 'Spree::Auction'
@image.viewable_id = Spree::Auction.find(params[:auction_id]).id

else
@image.viewable_type = 'Spree::Variant'
@image.viewable_id = params[:image][:viewable_id]
end

end

def variant_index_includes
[
variant_images: [viewable: { option_values: :option_type }]
]
end

def variant_edit_includes
[
variants_including_master: { option_values: :option_type, images: :viewable }
]
end
def permitted_resource_params
params[resource.object_name].present? ? params.require(resource.object_name).permit! : ActionController::Parameters.new
end
end
end
end
8 changes: 7 additions & 1 deletion app/models/spree/vendor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ class Vendor < Spree::Base
if Spree.version.to_f >= 3.6
validates_associated :image
end

validates :notification_email, email: true, allow_blank: true
has_many :auctions, class_name: 'Spree::Auction'

with_options dependent: :destroy do
if Spree.version.to_f >= 3.6
Expand All @@ -30,7 +30,13 @@ class Vendor < Spree::Base
has_many :stock_locations
has_many :variants
has_many :vendor_users
has_many :images, -> { order(:position) }, as: :viewable, dependent: :destroy, class_name: 'Spree::Image'
has_many :vendor_calenders
has_many :calenders, through: :vendor_calenders
end

scope :from_collection, -> (vendors_arr) {where("spree_vendors.id IN (?)", vendors_arr)}
scope :available_vendors, -> {pluck(:id, :name)}

has_many :users, through: :vendor_users

Expand Down
6 changes: 3 additions & 3 deletions app/overrides/spree/admin/layouts/admin.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
Deface::Override.new(
virtual_path: 'spree/layouts/admin',
name: 'vendors_main_menu_tabs',
insert_bottom: '#main-sidebar',
insert_bottom: '#custom_vendor_tab',
text: <<-HTML
<% if current_spree_user.respond_to?(:has_spree_role?) && current_spree_user.has_spree_role?(:admin) %>
<ul class="nav nav-sidebar">
<ul class="nav nav-sidebar border-bottom">
<%= tab plural_resource_name(Spree::Vendor), url: admin_vendors_path, icon: 'money' %>
</ul>
<% end %>
<% if defined?(current_spree_vendor) && current_spree_vendor %>
<ul class="nav nav-sidebar">
<ul class="nav nav-sidebar border-bottom">
<%= tab Spree::Vendor.model_name.human, url: admin_vendor_settings_path, icon: 'money' %>
</ul>
<% end %>
Expand Down
22 changes: 11 additions & 11 deletions app/views/spree/admin/vendors/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@
options_for_select(Spree::Vendor.state_machines[:state].states.collect { |s| [Spree.t("vendor_states.#{s.name}"), s.value] }, selected: @vendor.state),
{}, class: 'select2' %>
<% end %>
<% if Spree.version.to_f >= 3.6 %>
<%= f.field_container :image do %>
<%= f.label :image %>
<%= f.file_field :image %>
<%= image_tag main_app.url_for(@vendor.image.url(:small)) if @vendor.image %>
<% end %>
<% end %>
<%= f.field_container :commission_rate do %>
<%= f.label :commission_rate %>
<%= f.number_field :commission_rate, step: '0.01', class: 'form-control' %>
<% end %>
<%# if Spree.version.to_f >= 3.6 %>
<%#= f.field_container :image do %>
<%#= f.label :image %>
<%#= f.file_field :image %>
<%#= image_tag main_app.url_for(@vendor.image.url(:small)) if @vendor.image %>
<%# end %>
<%# end %>
<%#= f.field_container :commission_rate do %>
<%#= f.label :commission_rate %>
<%#= f.number_field :commission_rate, step: '0.01', class: 'form-control' %>
<%# end %>
<%= f.field_container :notification_email do%>
<%= f.label :notification_email %>
<%= f.email_field :notification_email, class: 'form-control' %>
Expand Down
3 changes: 3 additions & 0 deletions app/views/spree/admin/vendors/edit.html.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
<button class="btn btn-primary btn-success">
<%= link_to 'Add Images', spree.admin_vendor_images_url(@vendor) %>
</button>
<% content_for :page_title do %>
<%= link_to plural_resource_name(Spree::Vendor), spree.admin_vendors_url %> /
<%= @vendor.name %>
Expand Down