Guest User

Untitled

a guest
Apr 27th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. class Restricted < ActiveRecord::Base
  2. after_update :save_issuers, :save_ventures, :save_nonrestricteds, :save_additions, :save_deletions
  3.  
  4. has_many :issuers
  5. has_many :ventures
  6. has_many :nonrestricteds
  7. has_many :additions
  8. has_many :deletions
  9.  
  10. children = ['issuer', 'venture', 'nonrestricted', 'addition', 'deletion']
  11.  
  12. children.each do |child|
  13. src = <<-end_src
  14.  
  15. def #{child}_attributes=(#{child}_attributes)
  16. #{child}_attributes.each do |attributes|
  17. #{child.pluralize}.build(attributes)
  18. end
  19. end
  20.  
  21. def existing_#{child}_attributes=(#{child}_attributes)
  22. #{child.pluralize}.reject(&:new_record?).each do |#{child}|
  23. attributes = #{child}_attributes[#{child}.id.to_s]
  24. if attributes
  25. #{child}.attributes = attributes
  26. else
  27. #{child.pluralize}.delete(issuer)
  28. end
  29. end
  30. end
  31.  
  32. def save_#{child.pluralize}
  33. #{child.pluralize}.each do |#{child}|
  34. #{child}.save(false)
  35. end
  36. end
  37.  
  38. end_src
  39. class_eval src, __FILE__, __LINE__
  40. end
  41.  
  42. def self.all_most_recent_first
  43. restricteds = Restricted.find(:all, :order => 'created_at DESC')
  44. end
  45.  
  46. def self.published_most_recent_first
  47. restricteds = Restricted.find(:all, :conditions => ["published = 1"], :order => 'created_at DESC')
  48. end
  49.  
  50. def self.drafts_most_recent_first
  51. restricteds = Restricted.find(:all, :conditions => ["published = 0"], :order => 'created_at DESC')
  52. end
  53.  
  54. end
Add Comment
Please, Sign In to add comment