Class: Product

Inherits:
ActiveRecord::Base show all
Includes:
Permalink::Builder
Defined in:
app/models/product.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Permalink::Builder

#set_permalink, #to_param

Instance Attribute Details

#pictures_orderObject

Returns the value of attribute pictures_order.



7
8
9
# File 'app/models/product.rb', line 7

def pictures_order
  @pictures_order
end

Instance Method Details

#attach_picture(filename_with_extension, path) ⇒ Object

Attaches picture to a product. This method is used only in development and test.

Arguments

  • :filename_with_extension

  • :path – full path to the file. Something like Rails.root.join(‘db’, ‘original_pictures’, filename )



39
40
41
42
43
44
45
46
# File 'app/models/product.rb', line 39

def attach_picture(filename_with_extension, path)
  img = File.open(path) {|i| i.read}
  encoded_img = Base64.encode64 img
  io = FilelessIO.new(Base64.decode64(encoded_img))
  io.original_filename = filename_with_extension
  p = Picture.new(product: self, picture: io)
  p.save!
end

#find_or_build_all_answersObject



53
54
55
# File 'app/models/product.rb', line 53

def find_or_build_all_answers
  CustomField.all.each { |f| find_or_build_answer_for_field(f) }
end

#find_or_build_answer_for_field(field) ⇒ Object



48
49
50
51
# File 'app/models/product.rb', line 48

def find_or_build_answer_for_field(field)
  custom_field_answers.detect {|t| t.custom_field_id.to_s == field.id.to_s } ||
    custom_field_answers.build(custom_field_id: field.id)
end

#initialize_statusObject



70
71
72
# File 'app/models/product.rb', line 70

def initialize_status
  self.status ||= 'active'
end

#pictureObject



28
29
30
# File 'app/models/product.rb', line 28

def picture
  pictures.first
end