Skip to content
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
16 changes: 16 additions & 0 deletions app/models/spree/product_property_decorator.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<tr class="product_property fields" id="spree_<%= dom_id(f.object) %>" data-hook="product_property">
<td class="move-handle">
<% if f.object.persisted? && can?(:edit, f.object) %>
<span class="icon icon-move handle"></span>
<%= f.hidden_field :id %>
<% end %>
</td>
<td class='property_name'>
<%= f.text_field :property_name, class: 'autocomplete form-control' %>
</td>
<td class='value'>
<%= f.text_field :value, class: 'form-control' %>
</td>
<% if spree_current_vendor %>
<%= f.hidden_field :vendor_id, value: spree_current_vendor.id %>
<% end %>
<td class="actions actions-1">
<% if f.object.persisted? && can?(:destroy, f.object) %>
<%= link_to_delete f.object, no_text: true %>
<% end %>
</td>
</tr>
43 changes: 43 additions & 0 deletions app/views/spree/admin/product_properties/index.html.erb
Original file line number Diff line number Diff line change
@@ -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' }) %>
<span class="js-new-ptype-link"><%= button_link_to Spree.t(:select_from_prototype), available_admin_prototypes_url, { icon: 'properties', remote: true, 'data-update' => 'prototypes', class: 'btn-default' } %></span>
<% 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| %>
<fieldset>
<div id="prototypes" data-hook></div>

<table class="table sortable" data-hook data-sortable-link="<%= update_positions_admin_product_product_properties_url %>">
<thead>
<tr data-hook="product_properties_header">
<th colspan="2"><%= Spree.t(:property) %></th>
<th><%= Spree.t(:value) %></th>
<th class="actions"></th>
</tr>
</thead>
<tbody id="product_properties" data-hook>
<%= f.fields_for :product_properties do |pp_form| %>
<%= render 'product_property_fields', f: pp_form %>
<% end %>
</tbody>
</table>

<%= render('spree/admin/shared/edit_resource_links') if can? :update, Spree::ProductProperty %>

<%= hidden_field_tag 'clear_product_properties', 'true' %>
</fieldset>
<% 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 %>
2 changes: 1 addition & 1 deletion spec/features/spree/admin/product_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down