Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Restricted < ActiveRecord::Base
- after_update :save_issuers, :save_ventures, :save_nonrestricteds, :save_additions, :save_deletions
- has_many :issuers
- has_many :ventures
- has_many :nonrestricteds
- has_many :additions
- has_many :deletions
- children = ['issuer', 'venture', 'nonrestricted', 'addition', 'deletion']
- children.each do |child|
- src = <<-end_src
- def #{child}_attributes=(#{child}_attributes)
- #{child}_attributes.each do |attributes|
- #{child.pluralize}.build(attributes)
- end
- end
- def existing_#{child}_attributes=(#{child}_attributes)
- #{child.pluralize}.reject(&:new_record?).each do |#{child}|
- attributes = #{child}_attributes[#{child}.id.to_s]
- if attributes
- #{child}.attributes = attributes
- else
- #{child.pluralize}.delete(issuer)
- end
- end
- end
- def save_#{child.pluralize}
- #{child.pluralize}.each do |#{child}|
- #{child}.save(false)
- end
- end
- end_src
- class_eval src, __FILE__, __LINE__
- end
- def self.all_most_recent_first
- restricteds = Restricted.find(:all, :order => 'created_at DESC')
- end
- def self.published_most_recent_first
- restricteds = Restricted.find(:all, :conditions => ["published = 1"], :order => 'created_at DESC')
- end
- def self.drafts_most_recent_first
- restricteds = Restricted.find(:all, :conditions => ["published = 0"], :order => 'created_at DESC')
- end
- end
Add Comment
Please, Sign In to add comment