Advertisement
Guest User

Untitled

a guest
Jun 13th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. # == Schema Information
  2. #
  3. # Table name: brands
  4. #
  5. # id :integer not null, primary key
  6. # name :string
  7. # created_at :datetime not null
  8. # updated_at :datetime not null
  9. # path :string
  10. # logo_file_name :string
  11. # logo_content_type :string
  12. # logo_file_size :integer
  13. # logo_updated_at :datetime
  14. # home_page :boolean default(FALSE), not null
  15. #
  16.  
  17. class Brand < ActiveRecord::Base
  18. has_many :donor_products
  19. has_many :products
  20. has_many :coupons
  21.  
  22. attr_accessor :delete_logo
  23.  
  24. has_attached_file :logo,
  25. styles: { large: '600x380>', medium: '380x240>', thumb: '120x120>' },
  26. processors: [:thumbnail, :paperclip_optimizer],
  27. default_url: '/images/:style/missing.png'
  28. validates_attachment_content_type :logo, content_type: /\Aimage\/.*\Z/
  29.  
  30. # Update ElasticSearch index.
  31. # update_index 'products#brand', :self
  32.  
  33. validates :name, presence: true, uniqueness: true
  34. validates :path, presence: true, uniqueness: true
  35.  
  36. before_validation :normalize_name, on: :create
  37. before_validation :process_path, on: :create
  38.  
  39. before_update :remove_logo
  40.  
  41. ALLOWED = " 1234567890qwertyuiopasdfghjklzxcvbnmกขคฆจฉชฌตฏฐฑฒถทธปผพภบฎดซศษสฟฝหฮลฬรยญวอนณมงเแไโใ๑๒๓๔๕๖๗๘๙๐QWERTYUIOPLKJHGFDSAZXCVBNM"
  42.  
  43. def process_path
  44. path = ''
  45. self.name.each_char {|c| path << c if Brand::ALLOWED.include?(c)}
  46. self.path = path.strip.gsub(/ /, '-').downcase
  47. self.path = "#{self.path}-#{SecureRandom.urlsafe_base64.to_s}".downcase until Brand.where(path: self.path).blank?
  48. self.path.downcase!
  49. end
  50.  
  51. def remove_logo
  52. self.logo = self.delete_logo = nil unless self.delete_logo.nil?
  53. end
  54.  
  55. def normalize_name
  56. self.name = self.name.downcase.squish
  57. end
  58.  
  59. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement