diff --git a/app/models/spree/product_property_decorator.rb b/app/models/spree/product_property_decorator.rb index 9bb9eddc..a6c9cb29 100644 --- a/app/models/spree/product_property_decorator.rb +++ b/app/models/spree/product_property_decorator.rb @@ -1,4 +1,14 @@ + module Spree::ProductPropertyDecorator + def self.prepended(base) + base.attr_accessor :vendor_id + + base.before_validation :set_vendor_id, unless: proc { property.vendor_id } + + base.scope :with_vendor, -> (vendor_id) { joins(:property).where(spree_properties: { vendor_id: vendor_id }) } + base.scope :with_name, -> (name) { joins(:property).where(spree_properties: { name: name }) } + end + def property_name=(name) if name.present? # don't use `find_by :name` to workaround globalize/globalize#423 bug @@ -7,6 +17,12 @@ def property_name=(name) .first_or_create(presentation: name) end end + + private + + def set_vendor_id + property.update(vendor_id: vendor_id) + end end Spree::ProductProperty.prepend Spree::ProductPropertyDecorator diff --git a/app/views/spree/admin/product_properties/_product_property_fields.html.erb b/app/views/spree/admin/product_properties/_product_property_fields.html.erb new file mode 100644 index 00000000..94a0de25 --- /dev/null +++ b/app/views/spree/admin/product_properties/_product_property_fields.html.erb @@ -0,0 +1,22 @@ + + + <% if f.object.persisted? && can?(:edit, f.object) %> + + <%= f.hidden_field :id %> + <% end %> + + + <%= f.text_field :property_name, class: 'autocomplete form-control' %> + + + <%= f.text_field :value, class: 'form-control' %> + + <% if spree_current_vendor %> + <%= f.hidden_field :vendor_id, value: spree_current_vendor.id %> + <% end %> + + <% if f.object.persisted? && can?(:destroy, f.object) %> + <%= link_to_delete f.object, no_text: true %> + <% end %> + + diff --git a/app/views/spree/admin/product_properties/index.html.erb b/app/views/spree/admin/product_properties/index.html.erb new file mode 100644 index 00000000..15fc3232 --- /dev/null +++ b/app/views/spree/admin/product_properties/index.html.erb @@ -0,0 +1,43 @@ +<%= render 'spree/admin/shared/product_tabs', current: :properties %> +<%= render 'spree/admin/shared/error_messages', target: @product %> + +<% content_for :page_actions do %> + <%= button_link_to(Spree.t(:add_product_properties), "javascript:;", { icon: 'add', :'data-target' => "tbody#product_properties", class: 'btn-success spree_add_fields' }) %> + <%= button_link_to Spree.t(:select_from_prototype), available_admin_prototypes_url, { icon: 'properties', remote: true, 'data-update' => 'prototypes', class: 'btn-default' } %> +<% end if can? :create, Spree::ProductProperty && current_spree_user.respond_to?(:has_spree_role?) && current_spree_user.has_spree_role?(:admin) %> + +<%= form_for @product, url: spree.admin_product_url(@product), method: :put do |f| %> +
+
+ + + + + + + + + + + <%= f.fields_for :product_properties do |pp_form| %> + <%= render 'product_property_fields', f: pp_form %> + <% end %> + +
<%= Spree.t(:property) %><%= Spree.t(:value) %>
+ + <%= render('spree/admin/shared/edit_resource_links') if can? :update, Spree::ProductProperty %> + + <%= hidden_field_tag 'clear_product_properties', 'true' %> +
+<% end %> + +<%= javascript_tag do %> + var properties = <%= raw(@properties.to_json) %>; + $('#product_properties').on('keydown', 'input.autocomplete', function() { + already_auto_completed = $(this).is('ac_input'); + if (!already_auto_completed) { + $(this).autocomplete({source: properties}); + $(this).focus(); + } + }); +<% end %> diff --git a/spec/features/spree/admin/product_spec.rb b/spec/features/spree/admin/product_spec.rb index 1bc978e2..c8f9db0d 100644 --- a/spec/features/spree/admin/product_spec.rb +++ b/spec/features/spree/admin/product_spec.rb @@ -159,7 +159,7 @@ fill_in 'product[product_properties_attributes][0][property_name]', with: 'Testing edit' click_button 'Update' expect(page).to have_text 'successfully updated!' - # expect(Spree::ProductProperty.last.property.vendor_id).to eq vendor.id + expect(Spree::ProductProperty.last.property.vendor_id).to eq vendor.id end end