Skip to content

Instantly share code, notes, and snippets.

@ascsystems
Last active December 20, 2015 14:39
Show Gist options
  • Select an option

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

Select an option

Save ascsystems/6148761 to your computer and use it in GitHub Desktop.
<%= image_tag "options/#{lid.display_data}", alt: lid.name, title: lid.name, class: 'lid option', option_id: lid.id %>
<div class="option_wrapper">
<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>
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_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 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 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