Sha256: 546e4628704fe0dd6efae07ff7547544f4213d4bd3dfbc1055834106e6c4ed29

Contents?: true

Size: 1.38 KB

Versions: 8

Compression:

Stored size: 1.38 KB

Contents

require_dependency "merit/models/#{Merit.orm}/merit/action"

# Merit::Action general schema
#   ______________________________________________________________
#   source  | action       | target
#   user_id | method,value | model,id | processed
#   ______________________________________________________________
#   1 | comment nil | List 8  | true
#   1 | vote 3      | List 12 | true
#   3 | follow nil  | User 1  | false
#   X | create nil  | User #{generated_id} | false
#   ______________________________________________________________
#
# Rules relate to merit_actions by action name ('controller#action' string)
module Merit
  class Action
    def self.check_unprocessed
      where(processed: false).map(&:check_all_rules)
    end

    # Check rules defined for a merit_action
    def check_all_rules
      mark_as_processed!
      return if had_errors

      check_rules rules_matcher.select_from(AppBadgeRules), :badges
      check_rules rules_matcher.select_from(AppPointRules), :points
    end

    private

    def check_rules(rules_array, badges_or_points)
      rules_array.each do |rule|
        judge = Judge.new rule, action: self
        judge.send :"apply_#{badges_or_points}"
      end
    end

    def mark_as_processed!
      self.processed = true
      save
    end

    def rules_matcher
      @rules_matcher ||= ::Merit::RulesMatcher.new(target_model, action_method)
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
merit-3.0.2 app/models/merit/action.rb
merit-3.0.1 app/models/merit/action.rb
merit-3.0.0 app/models/merit/action.rb
merit-2.4.0 app/models/merit/action.rb
merit-2.3.4 app/models/merit/action.rb
merit-2.3.3 app/models/merit/action.rb
merit-2.3.2 app/models/merit/action.rb
merit-2.3.1 app/models/merit/action.rb