Module: Scoreable::Generator::ClassMethods
- Defined in:
- lib/scoreable/generator/active_record.rb
Instance Method Summary collapse
-
#generate_score(args) ⇒ Object
(also: #generate_#{Scoreable.score_term})
Scoreable.score_term = ‘score’ use to configure score generation scheme Example: give 5 points to user generate_score create: 5, for: :user generate_score create: 5 (need to set global score_reciever for this call using score_receiver= :receiver_object, :receiver_object could be a method that will return receiver.
Instance Method Details
#generate_score(args) ⇒ Object Also known as: generate_#{Scoreable.score_term}
Scoreable.score_term = ‘score’ use to configure score generation scheme
Example:
give 5 points to user
generate_score create: {score: 5, for: :user}
generate_score create: 5 (need to set global score_reciever for this call using score_receiver= :receiver_object, :receiver_object could be a method that will return receiver
give 5 point to "user" on "create" and call "report_point" as callback
generate_score create: { :score: 5, for: :user, callback: :report_score}
You can configure score for multiple method in single call
generate_score update: 4, create: 5, upvote: {score: 5, for: :upvoter}
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/scoreable/generator/active_record.rb', line 22 def generate_score(args) include Scoreable::Generator::InstanceMethods args.keys.each do |action| score_config = args[action] if score_config.is_a? Hash score = score_config[Scoreable.score_term.to_sym] callback_method = score_config[:callback] receivers = score_config[:for] || score_receiver force_save = score_config[:save] else callback_method = nil receivers = score_receiver score = score_config force_save = false end receivers = [receivers] unless receivers.is_a? Array actions = [action] unless action.is_a? Array actions.each do |action| action = action.to_s method_without_score_feature = action + "_without_#{Scoreable.score_term}" method_with_score_feature = action + "_with_#{Scoreable.score_term}" Scoreable::Generator::GeneratedMethods.class_eval do define_method method_with_score_feature.to_sym do send method_without_score_feature receivers.each do |receiver| send(receiver).save! if force_save score_obj= log_score score,action, receiver send callback_method,score_obj if callback_method score_obj end end end include Scoreable::Generator::GeneratedMethods alias_method_chain action, Scoreable.score_term end end end |