Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP #1314 add product type #1323

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
module SpreeCmCommissioner
module Admin
module ProductsControllerDecorator
include SpreeCmCommissioner::Admin::ProductTypeHelper

def self.prepended(base)
# spree update user sign_in_count
base.around_action :set_writing_role, only: %i[index]
end

# overrided
def permitted_resource_params
product_type_value = calculate_product_type_value(params[:product])

params.require(:product).permit(:name, :sku, :prototype_id, :price, :shipping_category_id,
:vendor_id, :subscribable, :need_confirmation
).merge(product_type: product_type_value)
end
end
end
end
Expand Down
15 changes: 15 additions & 0 deletions app/helpers/spree_cm_commissioner/admin/product_type_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module SpreeCmCommissioner
module Admin
module ProductTypeHelper
def calculate_product_type_value(params)
product_type_params = params.slice(*SpreeCmCommissioner::ProductTypeBitwise::BIT_SEGMENT.keys)

return nil unless product_type_params.values.any?

product_type_params.values.each_with_index.sum do |value, index|
value.to_i * (2**index)
end
end
end
end
end
27 changes: 27 additions & 0 deletions app/models/concerns/spree_cm_commissioner/product_type_bitwise.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module SpreeCmCommissioner
module ProductTypeBitwise
extend ActiveSupport::Concern

BIT_SEGMENT = {
accommodation: 0b1,
ecommerce: 0b10,
service: 0b100
}.freeze

BIT_SEGMENT.each do |segment, bit_value|
define_method "#{segment}?" do
segment_enabled?(bit_value)
end
end

def segments
BIT_SEGMENT.filter_map do |segment_value, bit_value|
segment_value if segment_enabled?(bit_value)
end
end

def segment_enabled?(bit_value)
segment & bit_value != 0
end
end
end

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

<div data-hook="product-from-prototype" id="product-from-prototype">
<% @prototype = @prototype || Spree::Prototype.new(option_types: []) %>
<%= render template: 'spree/admin/prototypes/show' %>
</div>
<%= render template: 'spree/admin/prototypes/show', locals: { f: f } %>
</div>
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<!-- insert_before "erb[silent]:contains('if @prototype.option_types.present?')" -->

<% initial_type = @prototype&.product_type || SpreeCmCommissioner::ProductType::PRODUCT_TYPES[0] %>
<% initial_type = @prototype&.product_type || SpreeCmCommissioner::ProductTypeBitwise::BIT_SEGMENT[0] %>

<div class="form-group mb-4">
<%= label_tag :product_type, Spree.t(:product_type) %>
<% SpreeCmCommissioner::ProductType::PRODUCT_TYPES.each do |product_type| %>
<div class="radio" data-id="<%= product_type.to_s %>">
<label data-hook="product_type_field">
<%= radio_button_tag 'product[product_type]', product_type.to_s, product_type.to_s == initial_type.to_s, { class: "product_types_radios" } %>
<%= product_type.capitalize %>
</label>
</div>
<% end %>
</div>
<%= f.field_container :product_type do %>
<%= f.label :product_type, class: 'd-flex align-items-center' %>
<div class="d-flex flex-wrap">
<% SpreeCmCommissioner::ProductTypeBitwise::BIT_SEGMENT.each do |type, bit_value| %>
<div class="form-check mr-3 mb-2">
<%= f.check_box type, class: 'form-check-input', checked: (f.object.product_type & bit_value) != 0 %>
<%= f.label type, class: 'form-check-label ml-1' %>
</div>
<% end %>
</div>
<% end %>
Loading