Skip to content

Commit

Permalink
product color creation implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitrychekalin committed Apr 18, 2016
1 parent b5c0878 commit 77879fa
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 9 deletions.
Expand Up @@ -18,26 +18,64 @@ def index
end
end

def new
@product_color_value = ProductColorValue.new
end

def create
@product_color_value = ProductColorValue.new(params[:product_color_value])
if @product_color_value.valid?
@product_color_value.save!
message = { success: "Color '#{@product_color_value.option_value.name}'
for the product '#{@product_color_value.product.name}' successfully created" }
redirect_to product_colors_path, flash: message
else
render :new
end
end

def edit
@product_color_value = ProductColorValue.find(params[:id])
end

def update
@product_color_value = ProductColorValue.find(params[:id])
@product_color_value.tap do |p|
p.active = ActiveRecord::ConnectionAdapters::Column.value_to_boolean(params[:product_color_value][:active])
p.custom = ActiveRecord::ConnectionAdapters::Column.value_to_boolean(params[:product_color_value][:custom])
end

if @product_color_value.save
new_state = @product_color_value.active? ? 'active' : 'inactive'
message = { success: "Product Color is now (#{new_state})" }
if @product_color_value.update_attributes!(params[:product_color_value])
color_state = @product_color_value.active? ? 'Active' : 'Inactive'
message = { success: "Color '#{@product_color_value.option_value.name}'
for the product '#{@product_color_value.product.name}' is now #{color_state}" }
else
message = { error: 'A problem occurred on saving. Please try again later.' }
end

redirect_to product_colors_path, flash: message
end

def colors_options_json
render json: get_color_options(params[:product_id]).map { |c| {id: c.id, name: c.name} }
end

private

helper_method :products, :color_options

def products
Spree::Product.active
end

def color_options
get_color_options(params[:product_color_value].try(:[], :product_id))
end

def get_color_options(product_id)
if product_id.present?
option_values_ids = Spree::Product.find(product_id).product_color_values.pluck(:option_value_id)
Spree::OptionValue.colors.where(Spree::OptionValue.arel_table[:id].not_in(option_values_ids))
else
{}
end
end

end
end
Expand Up @@ -12,7 +12,7 @@
.row
.col-md-3
p
b SKY
b SKU
.col-md-9
span.label #{@product_color_value.product.sku}
.row
Expand Down Expand Up @@ -51,7 +51,7 @@
= f.check_box :custom, class: 'form-control'
= f.label :custom
.form-group
= f.submit 'Save'
= f.submit 'Update', class: 'btn btn-primary'
alert.alert-warning
| Warning: updating colors custom/active attributes require product reindexing. To do it, proceed to the  
= link_to 'product index section', backend_product_indexes_path
Expand Down
@@ -1,2 +1,7 @@
- content_for :page_title do
| Colors variants

= render 'admin_ui/grid/grid_collection_filters', collection_url: product_colors_path
= link_to 'Create a Color', new_product_color_path, class: 'btn btn-primary'
br
= render 'admin_ui/grid/paginated_collection_grid'
23 changes: 23 additions & 0 deletions engines/admin_ui/app/views/admin_ui/product_colors/new.html.slim
@@ -0,0 +1,23 @@
.pages.wrapper.wrapper-content
= form_for @product_color_value, :url => {:action => "create"}, method: :post, :html => {:class => 'form-horizontal'} do |f|
.ibox.float-e-margins
.ibox-title
h5 Create Color Option
.ibox-content
.form-group
label.col-sm-2.control-label Product
.col-sm-10 = f.collection_select :product_id, products, :id, :name_with_sku,
{ include_blank: true }, { class: 'chosen-select', data: { selectable_target: '#product_color_value_option_value_id' }}
.form-group
label.col-sm-2.control-label Colors Options
.col-sm-10 = f.collection_select :option_value_id, color_options, :id, :name, { include_blank: true }, { class: 'chosen-select' }
.form-group
label.col-sm-2.control-label Active
.col-sm-10 = f.check_box :active
.form-group
label.col-sm-2.control-label Custom
.col-sm-10 = f.check_box :custom
.form-group
.col-sm-2
.col-sm-10
= f.submit 'Create', class: 'btn btn-primary'
1 change: 1 addition & 0 deletions engines/admin_ui/config/routes.rb
Expand Up @@ -52,6 +52,7 @@

resources :variants
resources :product_colors
get 'product_colors/colors_options/:product_id' => 'product_colors#colors_options_json'

namespace :content do
resources :pages
Expand Down

0 comments on commit 77879fa

Please sign in to comment.