Sha256: 4b047be718fae912985e59736d87ba4addfc8cdd1744a44d866f7258f46ab7ad

Contents?: true

Size: 1.59 KB

Versions: 16

Compression:

Stored size: 1.59 KB

Contents

module Merit
  extend ActiveSupport::Concern

  module ClassMethods
    def has_merit(options = {})
      # 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

      _merit_delegate_methods_to_sash
      _merit_define_badge_related_entries_method
      _merit_sash_initializer
    end

    # Delegate methods from meritable models to their sash
    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

    def _merit_define_badge_related_entries_method
      meritable_class_name = name.demodulize
      Merit::Badge._define_related_entries_method(meritable_class_name)
    end

    # _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:
    # http://blog.hasmanythrough.com/2012/1/20/modularized-association-methods-in-rails-3-2
    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
  end
end

Version data entries

16 entries across 16 versions & 2 rubygems

Version Path
merit-4.0.3 lib/merit/class_methods.rb
qalam_merit-4.5.12 lib/merit/class_methods.rb
merit-4.0.2 lib/merit/class_methods.rb
qalam_merit-4.5.11 lib/merit/class_methods.rb
qalam_merit-4.5.10 lib/merit/class_methods.rb
qalam_merit-4.5.09 lib/merit/class_methods.rb
qalam_merit-4.5.05 lib/merit/class_methods.rb
qalam_merit-4.5.04 lib/merit/class_methods.rb
qalam_merit-4.5.03 lib/merit/class_methods.rb
qalam_merit-4.5.02 lib/merit/class_methods.rb
qalam_merit-4.5.01 lib/merit/class_methods.rb
qalam_merit-4.5.00 lib/merit/class_methods.rb
qalam_merit-4.0.4 lib/merit/class_methods.rb
qalam_merit-4.0.35 lib/merit/class_methods.rb
merit-4.0.1 lib/merit/class_methods.rb
merit-4.0.0 lib/merit/class_methods.rb