Skip to content

Instantly share code, notes, and snippets.

@ascsystems
Created August 5, 2013 05:00
Show Gist options
  • Select an option

  • Save ascsystems/6153627 to your computer and use it in GitHub Desktop.

Select an option

Save ascsystems/6153627 to your computer and use it in GitHub Desktop.
<div class="option_wrapper <%= if option_counter == 0
'first'
end %>">
<div class="option_header">SELECT <%= option.name %>: <span class="option_text"><%= option.option_values[0].name %></span></div>
<div class="options"><input type="hidden" name="options[<%= option_counter %>]" class="option_value" value="<%= option.option_values[0].id %>">
<%= render partial: option.option_type.template, collection: option.option_values %>
</div>
</div>
<%= render partial: 'option', collection: @product.sorted_options %>
class Product < ActiveRecord::Base
attr_accessible :description, :name, :price, :brand_id, :product_type_id
belongs_to :brand
belongs_to :product_type
has_many :category_products, dependent: :destroy
has_many :categories, through: :category_products
has_many :reviews
has_many :line_items
has_many :product_images
has_many :product_option_values
has_many :option_values, through: :product_option_values, order: "option_values.order_num"
has_many :options, through: :option_values, order: "options.order_num", uniq: true
has_many :related_products
has_many :products, through: :related_products, source: :related_product
has_one :product_image, conditions: {default_image: 1}
before_destroy :ensure_not_referenced_by_any_line_item
validates :name, uniqueness: true, presence: true
validates :price, presence: true, numericality: true, format: { with: /^\d{1,4}(\.\d{0,2})?$/ }
validates :brand_id, presence: true
validates :product_type_id, presence: true
def ensure_not_referenced_by_any_line_item
if line_items.count.zero?
return true
else
errors.add(:base, 'Line Items Exist')
return false
end
end
def sorted_options
self.option_values.group_by(&:option)
end
def product_options_by_id(id)
option_values.includes(option: :option_type).where("option_types.id = ?", id)
end
def product_colors
self.product_options_by_id(1)
end
def product_lids
self.product_options_by_id(2)
end
def product_accessories
self.product_options_by_id(3)
end
def rating
rating = reviews.average(:rating).to_f.round
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment