Guest User

Untitled

a guest
Jul 20th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. class Account < ActiveRecord::Base
  2. composed_of :plan,
  3. :allow_nil => false,
  4. :mapping => [
  5. # Account Plan
  6. %w(plan_name name),
  7. %w(max_users max_users),
  8. %w(max_repos max_repos),
  9. %w(free_champagne free_champagne),
  10. ],
  11. :constructor => Proc.new {|plan_name, max_users, max_repos, free_champagne |
  12. relation = Plan.where(:name => plan_name,
  13. :max_users => max_users,
  14. :max_repos => max_repos,
  15. :free_champagne => free_champagne)
  16. relation.present? ? relation.first : relation.build
  17. },
  18. :converter => Proc.new {|value|
  19. Plan.where(:name => value.is_a?(Array) ? value.first : value).first
  20. }
  21.  
  22. end
  23.  
  24. # This allows me do stuff like:
  25. a = Account.new
  26. a.plan = Plan.first
  27. a.save
  28.  
  29. # and have all the plan's attributes reflected over as specified,
  30. # but there be no explicit link in between the account instance and plan instance
  31. # 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