Class: Dhatu::Promotion

Inherits:
ApplicationRecord show all
Includes:
Featureable, Publishable
Defined in:
app/models/dhatu/promotion.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.image_configurationObject

Image Configuration




116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'app/models/dhatu/promotion.rb', line 116

def self.image_configuration
  {
    "Image::CoverImage" => {
      max_upload_limit: 10485760,
      min_upload_limit: 1,
      resolutions: [1000, 500],
      form_upload_image_label: "Upload a new Image",
      form_title: "Upload an Image",
      form_sub_title: "Please read the instructions below:",
      form_instructions: [
        "the filename should be in .jpg / .jpeg or .png format",
        "the image resolutions should be <strong>1000 x 500 Pixels</strong>",
        "the file size should be greater than 100 Kb and or lesser than <strong>10 MB</strong>",
        "Note: most cameras and camera phones will produce images bigger than this"
      ]
     }  
  }
  
end

.save_row_data(hsh) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'app/models/dhatu/promotion.rb', line 34

def self.save_row_data(hsh)
  # Initializing error hash for displaying all errors altogether
  error_object = Kuppayam::Importer::ErrorHash.new

  return error_object if hsh[:title].to_s.strip.blank?

  promotion = Dhatu::Promotion.find_by_title(hsh[:title].to_s.strip) || Dhatu::Promotion.new

  promotion.title = hsh[:title].to_s.strip
  promotion.code = hsh[:code].to_s.strip
  promotion.short_description = hsh[:short_description].to_s.strip

  promotion.display_name = ["true", "t","1","yes","y"].include?(hsh[:display_name].to_s.downcase.strip)
  promotion.display_email = ["true", "t","1","yes","y"].include?(hsh[:display_email].to_s.downcase.strip)
  promotion.display_phone = ["true", "t","1","yes","y"].include?(hsh[:display_phone].to_s.downcase.strip)

  promotion.name_mandatory = ["true", "t","1","yes","y"].include?(hsh[:name_mandatory].to_s.downcase.strip)
  promotion.email_mandatory = ["true", "t","1","yes","y"].include?(hsh[:email_mandatory].to_s.downcase.strip)
  promotion.phone_mandatory = ["true", "t","1","yes","y"].include?(hsh[:phone_mandatory].to_s.downcase.strip)
  
  promotion.featured = ["true", "t","1","yes","y"].include?(hsh[:featured].to_s.downcase.strip)
  promotion.status = hsh[:status].to_s.strip.blank? ? PUBLISHED : hsh[:status].to_s.strip
  promotion.priority = hsh[:priority].to_s.strip.blank? ? 1000 : hsh[:priority].to_s.strip

  if promotion.valid?
    begin
      promotion.save!
    rescue Exception => e
      summary = "uncaught #{e} exception while handling connection: #{e.message}"
      details = "Stack trace: #{e.backtrace.map {|l| "  #{l}\n"}.join}"
      error_object.errors << { summary: summary, details: details }        
    end
  else
    summary = "Error while saving promotion: #{promotion.title}"
    details = "Error! #{promotion.errors.full_messages.to_sentence}"
    error_object.errors << { summary: summary, details: details }
  end
  return error_object
end

Instance Method Details

#can_be_deleted?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'app/models/dhatu/promotion.rb', line 103

def can_be_deleted?
  status?(:removed)
end

#can_be_edited?Boolean

Permission Methods


Returns:

  • (Boolean)


99
100
101
# File 'app/models/dhatu/promotion.rb', line 99

def can_be_edited?
  status?(:published) or status?(:unpublished)
end

#display_nameObject



84
85
86
# File 'app/models/dhatu/promotion.rb', line 84

def display_name
  "#{title_was}"
end

#generate_and_save_codeObject

Callback Methods




110
111
112
# File 'app/models/dhatu/promotion.rb', line 110

def generate_and_save_code
  self.update_attribute(:code, SecureRandom.hex(4))
end

#promotion_attributes_with_valuesObject



88
89
90
91
92
93
94
# File 'app/models/dhatu/promotion.rb', line 88

def promotion_attributes_with_values
  hsh = {}
  self.promotion_attributes.published.where("mandatory is TRUE").order("priority ASC, name ASC").each do |pa|
    hsh[pa.name] = { values: pa.values, selected: "selected", data_type: pa.data_type, mandatory: pa.mandatory}
  end
  hsh
end

#to_paramObject

Generic Methods




80
81
82
# File 'app/models/dhatu/promotion.rb', line 80

def to_param
  "#{id}-#{title.parameterize[0..32]}"
end