<commit_before>module Merit
  # This module sets up an after_filter to update merit_actions table.
  # It executes on every action, and checks rules only if
  # 'controller_name#action_name' has defined badge or point rules
  module ControllerExtensions
    def self.included(base)
      base.after_filter do |controller|
        action      = "#{controller_name}\##{action_name}"
        badge_rules = BadgeRules.new
        point_rules = PointRules.new
        if badge_rules.defined_rules[action].present? || point_rules.actions_to_point[action].present?
          # TODO: target_object should be configurable (now it's singularized controller name)
          target_object = instance_variable_get(:"@#{controller_name.singularize}")
          # Set target_id from params or from current instance variable
          target_id = params[:id] || target_object.try(:id)
          # id nil, or string (friendly_id); target_object found
          if target_object.present? && (target_id.nil? || !(target_id =~ /^[0-9]+$/))
            target_id = target_object.id
          end

          # TODO: value should be configurable (now it's params[:value] set in the controller)
          merit_action_id = MeritAction.create(
            :user_id       => send(Merit.current_user_method).try(:id),
            :action_method => action_name,
            :action_value  => params[:value],
            :had_errors    => target_object.try(:errors).try(:present?),
            :target_model  => controller_name,
            :target_id     => target_id
          ).id

          # Check rules in after_filter?
          if Merit.checks_on_each_request
            badge_rules.check_new_actions

            # Show flash msg?
            if (log = MeritAction.find(merit_action_id).log)
              # Badges granted to current_user
              granted = log.split('|').select{|log| log =~ /badge_granted_to_action_user/ }
              granted.each do |badge|
                badge_id = badge.split(':').last.to_i
                flash[:merit] = t('merit.flashs.badge_granted', :badge => t("badges.#{Badge.find(badge_id).name}.name"))
              end
            end
          end
        end
      end
    end
  end
end
<commit_msg>Removing ugly/untested/not-configurable flash message code. fixes #27<commit_after>module Merit
  # This module sets up an after_filter to update merit_actions table.
  # It executes on every action, and checks rules only if
  # 'controller_name#action_name' has defined badge or point rules
  module ControllerExtensions
    def self.included(base)
      base.after_filter do |controller|
        action      = "#{controller_name}\##{action_name}"
        badge_rules = BadgeRules.new
        point_rules = PointRules.new
        if badge_rules.defined_rules[action].present? || point_rules.actions_to_point[action].present?
          # TODO: target_object should be configurable (now it's singularized controller name)
          target_object = instance_variable_get(:"@#{controller_name.singularize}")
          # Set target_id from params or from current instance variable
          target_id = params[:id] || target_object.try(:id)
          # id nil, or string (friendly_id); target_object found
          if target_object.present? && (target_id.nil? || !(target_id =~ /^[0-9]+$/))
            target_id = target_object.id
          end

          # TODO: value should be configurable (now it's params[:value] set in the controller)
          merit_action_id = MeritAction.create(
            :user_id       => send(Merit.current_user_method).try(:id),
            :action_method => action_name,
            :action_value  => params[:value],
            :had_errors    => target_object.try(:errors).try(:present?),
            :target_model  => controller_name,
            :target_id     => target_id
          ).id

          # Check rules in after_filter?
          if Merit.checks_on_each_request
            badge_rules.check_new_actions
          end
        end
      end
    end
  end
end

{'fraction_non_alphanumeric': 0.0968222442899702, 'fraction_numerical': 0.0014895729890764648, 'mean_word_length': 2.4318568994889267, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5267102', 'n_tokens_mistral': 1159, 'n_tokens_neox': 1133, 'n_words': 279}