Guest User

Untitled

a guest
Jun 19th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. require 'open-magazine' # requirements
  2.  
  3. class Article
  4. include Any::Aweseome::Module # includes and extensions
  5.  
  6. belongs_to :author # relations to other models
  7.  
  8. attr_accessor :title, :pages # attribute definitions
  9.  
  10. RESERVED_TITLES = ['Coding Ruby'] # constants...
  11.  
  12. attr_accessible :title # ActiveRecord mass-assignment white-list and
  13. # other security related settings
  14.  
  15. scope :large, where(:pages.lg => 300) # class method macros like finders etc
  16. mount_uploader :cover_image, CoverImageUploader
  17.  
  18. validates_presence_of :title # validations
  19.  
  20. # put callbacks & hooks before any methods
  21. # it's important to be aware of them
  22. # since they are automagical
  23. before_save :calculate_price
  24. after_create :bill_book_stores
  25.  
  26. # put class methods first...
  27. def self.dust_books
  28. all.each(&:dust!)
  29. end
  30.  
  31. # then instance methods
  32. # prioritize setter methods above others - they are unexpected
  33. def title=(str)
  34. @title = str.strip.downcase
  35. end
  36.  
  37. # other methods
  38. def nice_title
  39. title.titleize
  40. end
  41.  
  42. # query methods
  43. def best_seller?
  44. Author.awarded.include?(author.name)
  45. end
  46.  
  47. # private & protected methods
  48. private
  49. def bill_book_stores
  50. Billing.create(self)
  51. end
  52. end
Add Comment
Please, Sign In to add comment