From 4b3a4494464aef0faa61901c015419ad88f58bb6 Mon Sep 17 00:00:00 2001 From: vaneath Date: Tue, 9 Apr 2024 15:44:15 +0700 Subject: [PATCH] close #1314 add product type --- .../admin/products_controller_decorator.rb | 11 ++++++++ .../admin/product_type_helper.rb | 15 +++++++++++ .../product_type_bitwise.rb | 27 +++++++++++++++++++ .../new/include_kyc_checkbox.html.erb.deface | 10 ------- .../product_from_prototype.html.erb.deface | 4 +-- .../show/product_type.html.erb.deface | 24 ++++++++--------- 6 files changed, 67 insertions(+), 24 deletions(-) create mode 100644 app/helpers/spree_cm_commissioner/admin/product_type_helper.rb create mode 100644 app/models/concerns/spree_cm_commissioner/product_type_bitwise.rb delete mode 100644 app/overrides/spree/admin/products/new/include_kyc_checkbox.html.erb.deface diff --git a/app/controllers/spree_cm_commissioner/admin/products_controller_decorator.rb b/app/controllers/spree_cm_commissioner/admin/products_controller_decorator.rb index 37b5aa025..8b4b85628 100644 --- a/app/controllers/spree_cm_commissioner/admin/products_controller_decorator.rb +++ b/app/controllers/spree_cm_commissioner/admin/products_controller_decorator.rb @@ -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 diff --git a/app/helpers/spree_cm_commissioner/admin/product_type_helper.rb b/app/helpers/spree_cm_commissioner/admin/product_type_helper.rb new file mode 100644 index 000000000..8a6d593dd --- /dev/null +++ b/app/helpers/spree_cm_commissioner/admin/product_type_helper.rb @@ -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 diff --git a/app/models/concerns/spree_cm_commissioner/product_type_bitwise.rb b/app/models/concerns/spree_cm_commissioner/product_type_bitwise.rb new file mode 100644 index 000000000..b02be90ce --- /dev/null +++ b/app/models/concerns/spree_cm_commissioner/product_type_bitwise.rb @@ -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 diff --git a/app/overrides/spree/admin/products/new/include_kyc_checkbox.html.erb.deface b/app/overrides/spree/admin/products/new/include_kyc_checkbox.html.erb.deface deleted file mode 100644 index b8a399a64..000000000 --- a/app/overrides/spree/admin/products/new/include_kyc_checkbox.html.erb.deface +++ /dev/null @@ -1,10 +0,0 @@ - - -
- <%= f.field_container :kyc do %> - <%= f.label :kyc do %> - <%= f.check_box :kyc %> - <%= Spree.t(:kyc) %> - <% end %> - <% end %> -
\ No newline at end of file diff --git a/app/overrides/spree/admin/products/new/product_from_prototype.html.erb.deface b/app/overrides/spree/admin/products/new/product_from_prototype.html.erb.deface index 4950500f2..9d2350669 100644 --- a/app/overrides/spree/admin/products/new/product_from_prototype.html.erb.deface +++ b/app/overrides/spree/admin/products/new/product_from_prototype.html.erb.deface @@ -2,5 +2,5 @@
<% @prototype = @prototype || Spree::Prototype.new(option_types: []) %> - <%= render template: 'spree/admin/prototypes/show' %> -
\ No newline at end of file + <%= render template: 'spree/admin/prototypes/show', locals: { f: f } %> + diff --git a/app/overrides/spree/admin/prototypes/show/product_type.html.erb.deface b/app/overrides/spree/admin/prototypes/show/product_type.html.erb.deface index 26b1dd789..8e46b582f 100644 --- a/app/overrides/spree/admin/prototypes/show/product_type.html.erb.deface +++ b/app/overrides/spree/admin/prototypes/show/product_type.html.erb.deface @@ -1,15 +1,15 @@ -<% initial_type = @prototype&.product_type || SpreeCmCommissioner::ProductType::PRODUCT_TYPES[0] %> +<% initial_type = @prototype&.product_type || SpreeCmCommissioner::ProductTypeBitwise::BIT_SEGMENT[0] %> -
- <%= label_tag :product_type, Spree.t(:product_type) %> - <% SpreeCmCommissioner::ProductType::PRODUCT_TYPES.each do |product_type| %> -
- -
- <% end %> -
\ No newline at end of file +<%= f.field_container :product_type do %> + <%= f.label :product_type, class: 'd-flex align-items-center' %> +
+ <% SpreeCmCommissioner::ProductTypeBitwise::BIT_SEGMENT.each do |type, bit_value| %> +
+ <%= 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' %> +
+ <% end %> +
+<% end %>