Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Account < ActiveRecord::Base
- composed_of :plan,
- :allow_nil => false,
- :mapping => [
- # Account Plan
- %w(plan_name name),
- %w(max_users max_users),
- %w(max_repos max_repos),
- %w(free_champagne free_champagne),
- ],
- :constructor => Proc.new {|plan_name, max_users, max_repos, free_champagne |
- relation = Plan.where(:name => plan_name,
- :max_users => max_users,
- :max_repos => max_repos,
- :free_champagne => free_champagne)
- relation.present? ? relation.first : relation.build
- },
- :converter => Proc.new {|value|
- Plan.where(:name => value.is_a?(Array) ? value.first : value).first
- }
- end
- # This allows me do stuff like:
- a = Account.new
- a.plan = Plan.first
- a.save
- # and have all the plan's attributes reflected over as specified,
- # but there be no explicit link in between the account instance and plan instance
- # that last bit is useful if you want to offer completely custom plans for a minority of users
Add Comment
Please, Sign In to add comment