Module: Merit::ClassMethods
- Defined in:
- lib/merit/model_additions.rb
Instance Method Summary collapse
- #_merit_active_record_specific_config ⇒ Object
- #_merit_define_badge_related_entries_method ⇒ Object
-
#_merit_delegate_methods_to_sash ⇒ Object
Delegate methods from meritable models to their sash.
- #_merit_mongoid_specific_config ⇒ Object
-
#_merit_sash_initializer ⇒ Object
_sash initializes a sash if doesn’t have one yet.
- #find_by_id(id) ⇒ Object
- #has_merit(options = {}) ⇒ Object
Instance Method Details
#_merit_active_record_specific_config ⇒ Object
25 26 |
# File 'lib/merit/model_additions.rb', line 25 def _merit_active_record_specific_config end |
#_merit_define_badge_related_entries_method ⇒ Object
35 36 37 38 |
# File 'lib/merit/model_additions.rb', line 35 def meritable_class_name = name.demodulize Merit::Badge.(meritable_class_name) end |
#_merit_delegate_methods_to_sash ⇒ Object
Delegate methods from meritable models to their sash
19 20 21 22 23 |
# File 'lib/merit/model_additions.rb', line 19 def _merit_delegate_methods_to_sash methods = %w(badge_ids badges points add_badge rm_badge add_points subtract_points score_points) methods.each { |method| delegate method, to: :_sash } end |
#_merit_mongoid_specific_config ⇒ Object
28 29 30 31 32 33 |
# File 'lib/merit/model_additions.rb', line 28 def _merit_mongoid_specific_config field :level, type: Integer, default: 0 def find_by_id(id) where(_id: id).first end end |
#_merit_sash_initializer ⇒ Object
_sash initializes a sash if doesn’t have one yet. From Rails 3.2 we can override association methods to do so transparently, but merit supports Rails ~> 3.0.0. See: blog.hasmanythrough.com/2012/1/20/modularized-association-methods-in-rails-3-2
44 45 46 47 48 49 50 |
# File 'lib/merit/model_additions.rb', line 44 def _merit_sash_initializer define_method(:_sash) do # TODO: reload.sash is not regression tested sash || reload.sash || update(sash: Sash.create) sash end end |
#find_by_id(id) ⇒ Object
30 31 32 |
# File 'lib/merit/model_additions.rb', line 30 def find_by_id(id) where(_id: id).first end |
#has_merit(options = {}) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/merit/model_additions.rb', line 5 def has_merit( = {}) # MeritableModel#sash_id is more stable than Sash#meritable_model_id # That's why MeritableModel belongs_to Sash. Can't use # dependent: destroy as it may raise FK constraint exceptions. See: # https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/1079-belongs_to-dependent-destroy-should-destroy-self-before-assocation belongs_to :sash, class_name: 'Merit::Sash', inverse_of: nil, optional: true send :"_merit_#{Merit.orm}_specific_config" _merit_delegate_methods_to_sash _merit_sash_initializer end |