Created
August 20, 2008 19:18
-
-
Save jthomp/6433 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Product < ActiveRecord::Base | |
########################### | |
# Including our Permalinked module | |
include Permalinked | |
########################### | |
# Products should have: | |
# => name [r] | |
# => permalink | |
# => description [r] | |
# => product group [a] | |
# => category [a] | |
# => images [a] | |
# => preview image [a] | |
# => zones [a] | |
# => pricegroup [a] | |
# => ship start time | |
# => ship end time | |
# => always shipping indicator | |
validates_presence_of :name, :description | |
validates_presence_of :always_shipping, :if => :shipping_dates_are_empty?, :message => "must be set to 'Yes' if you do not have both start and end shipping dates" | |
validates_uniqueness_of :name | |
has_many :product_zones | |
has_many :zones, :through => :product_zones | |
has_many :images, :as => :imageable, :conditions => "imageable_subtype = 'main_image'" | |
has_one :preview_image, :as => :imageable, :conditions => "imageable_subtype = 'preview_image'", :class_name => 'Image' | |
belongs_to :pricegroup | |
belongs_to :category | |
belongs_to :product_group | |
########################### | |
# After save it should: | |
# => update/create permalink | |
after_save :create_permalink | |
########################### | |
# Methods for the association | |
# of our Product and ProductGroup | |
def group | |
product_group.name if product_group | |
end | |
def group=(name) | |
self.product_group = ProductGroup.find_or_create_by_name(name) unless name.blank? | |
end | |
########################### | |
# Methods for the association | |
# of our Product and Preview | |
# Image | |
def preview_image_array=(individual_image) | |
if not individual_image[:uploaded_data].blank? | |
individual_image["imageable_subtype"] = 'preview_image' | |
if individual_image[:id].blank? | |
image_save = images.build(individual_image) | |
else | |
image_save = self.preview_image | |
image_save.attributes = individual_image | |
end | |
self.preview_image = image_save | |
end | |
end | |
########################### | |
# Methods for the association | |
# of our Product and Images | |
def main_images=(main_images_array) | |
main_images_array.each do |individual_image| | |
if not individual_image[:uploaded_data].blank? | |
individual_image["imageable_subtype"] = 'main_image' | |
if individual_image[:id].blank? | |
image_save = images.build(individual_image) | |
else | |
image_save = images.detect { |i| i.id == individual_image[:id].to_i } | |
image_save.attributes = individual_image | |
end | |
self.images << image_save | |
end | |
end | |
end | |
protected | |
def shipping_dates_are_empty? | |
ship_start.blank? || ship_end.blank? | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment