Advertisement
madrahimov

Untitled

Aug 15th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. # frozen_string_literal: true
  2.  
  3. class Market < ApplicationRecord
  4. audited
  5. after_create :create_market_currencies, :create_market_units
  6. strip_attributes collapse_spaces: true
  7.  
  8. validates :title, :timezone, presence: true
  9.  
  10. belongs_to :currency
  11. has_many :user_markets
  12. has_many :stocks
  13. has_many :categories
  14. has_many :market_units
  15. has_many :market_currencies
  16. has_many :currencies, -> { where 'market_currencies.is_active = true' },
  17. through: :market_currencies
  18. has_many :suppliers
  19. has_many :option_types
  20. has_many :option_values, -> { where 'option_types.is_active = true' },
  21. through: :option_types
  22. default_scope { order(title: :asc) }
  23.  
  24. def create_market_currencies
  25. Currency.all.each do |c|
  26. mc = MarketCurrency.new
  27. mc.market_id = id
  28. mc.currency_id = c.id
  29. mc.save
  30. end
  31. end
  32.  
  33. def create_market_units
  34. I18n.t('market_units').each do |u|
  35. mc = MarketUnit.new
  36. mc.market_id = id
  37. mc.title = u[1]
  38. mc.save
  39. end
  40. end
  41.  
  42. def top_categories
  43. categories.where(parent_id: nil)
  44. end
  45. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement