Skip to content

Instantly share code, notes, and snippets.

@flyfy1
Last active June 6, 2016 10:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flyfy1/4840776de253bed1faed24743d957339 to your computer and use it in GitHub Desktop.
Save flyfy1/4840776de253bed1faed24743d957339 to your computer and use it in GitHub Desktop.
For Solidus: making Property selectable field
Spree::ProductProperty.class_eval do
belongs_to :value_object, class_name: 'Spree::PropertyValue', foreign_key: :value_id
before_validation { self.value = value_object.value if value_id and changes[:value_id] }
end
Spree::Property.class_eval do
enum data_type: [:textable, :selectable]
has_many :property_values, :class_name => 'Spree::PropertyValue', foreign_key: :property_id
accepts_nested_attributes_for :property_values, allow_destroy: true
end
class Spree::PropertyValue < ActiveRecord::Base
belongs_to :property, :class_name => 'Spree::Property'
end
# Step 1
class CreateSpreePropertyValues < ActiveRecord::Migration
def change
create_table :spree_property_values do |t|
t.integer :property_id, index: true
t.string :value
t.timestamps null: false
end
end
end
# Step 2
class AddDataTypeToSpreeProperty < ActiveRecord::Migration
def change
add_column :spree_properties, :data_type, :integer, default: 0
end
end
# Step 3
class AddValueIdToSpreeProductProperty < ActiveRecord::Migration
def change
add_column :spree_product_properties, :value_id, :integer, index: true
end
end
Deface::Override.new(virtual_path: 'spree/admin/properties/index',
name: 'add_data_type_to_admin_properties_index_header',
insert_after: '[data-hook="listing_properties_header"] th:nth-child(2)',
text: <<ERB)
<th><%= Spree::Property.human_attribute_name(:data_type) %></th>
ERB
Deface::Override.new(virtual_path: 'spree/admin/properties/index',
name: 'add_data_type_to_admin_properties_index_row',
insert_after: '[data-hook="listing_properties_row"] td:nth-child(2)',
text: <<ERB)
<td style="padding-left:1em"><%= property.data_type %></td>
ERB
Deface::Override.new(virtual_path: 'spree/admin/product_properties/_product_property_fields',
name: 'make_property_value_selectable_when_property_is_selectable',
replace_contents: '[data-hook="product_property"] .value',
text: <<ERB)
<%- prop = f.object.property %>
<%- if prop&.selectable? %>
<%= f.collection_select :value_id, prop.property_values, :id, :value %>
<%- else %>
<%= f.text_field :value %>
<%- end %>
ERB
Deface::Override.new(virtual_path: 'spree/admin/product_properties/_product_property_fields',
name: 'property_name_is_not_modifiable_in_product_property_edit_page',
replace_contents: '[data-hook="product_property"] .property_name',
text: <<ERB)
<%- if f.object.persisted? %>
<%= f.object.property_name %>
<%= f.hidden_field :property_name %>
<%- else %>
<%= f.collection_select :property_name, Spree::Property.all, :name, :presentation, {prompt: true}, {id: 'product-select-new-property'} %>
<%- end %>
ERB
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment