Skip to content

Commit

Permalink
Merge 9b20a22 into 26c63cd
Browse files Browse the repository at this point in the history
  • Loading branch information
tute committed Apr 25, 2020
2 parents 26c63cd + 9b20a22 commit 0faac7d
Show file tree
Hide file tree
Showing 39 changed files with 155 additions and 88 deletions.

This file was deleted.

16 changes: 0 additions & 16 deletions lib/generators/merit/install_generator.rb

This file was deleted.

30 changes: 5 additions & 25 deletions lib/merit.rb
@@ -1,17 +1,4 @@
require 'merit/rule'
require 'merit/rules_badge_methods'
require 'merit/rules_points_methods'
require 'merit/rules_rank_methods'
require 'merit/rules_matcher'
require 'merit/controller_extensions'
require 'merit/model_additions'
require 'merit/judge'
require 'merit/reputation_change_observer'
require 'merit/sash_finder'
require 'merit/base_target_finder'
require 'merit/target_finder'
require 'merit/models/base/sash'
require 'merit/models/base/badges_sash'
require 'zeitwerk'

module Merit
def self.setup
Expand All @@ -24,7 +11,6 @@ def self.checks_on_each_request
@config.checks_on_each_request
end

# # Define ORM
def self.orm
@config.orm || :active_record
end
Expand Down Expand Up @@ -76,7 +62,6 @@ class Engine < Rails::Engine

initializer 'merit.controller' do |app|
extend_orm_with_has_merit
require_models
ActiveSupport.on_load(action_controller_hook) do
begin
# Load app rules on boot up
Expand All @@ -91,15 +76,6 @@ class Engine < Rails::Engine
end
end

def require_models
require 'merit/models/base/sash'
require 'merit/models/base/badges_sash'
require "merit/models/#{Merit.orm}/merit/activity_log"
require "merit/models/#{Merit.orm}/merit/badges_sash"
require "merit/models/#{Merit.orm}/merit/sash"
require "merit/models/#{Merit.orm}/merit/score"
end

def extend_orm_with_has_merit
ActiveRecord::Base.include(Merit)
end
Expand All @@ -113,3 +89,7 @@ def action_controller_hook
end
end
end

loader = Zeitwerk::Loader.for_gem
loader.setup
loader.eager_load
File renamed without changes.
File renamed without changes.
@@ -1,8 +1,8 @@
require 'rails/generators/active_record'

module ActiveRecord
module Generators
class InstallGenerator < Rails::Generators::Base
module Merit
module Generators::ActiveRecord
class InstallGenerator < ::Rails::Generators::Base
include Rails::Generators::Migration

source_root File.expand_path('../templates', __FILE__)
Expand All @@ -13,19 +13,19 @@ def self.next_migration_number(path)
end

def copy_migrations_and_model
migration_template 'create_merit_actions.rb',
migration_template 'create_merit_actions.erb',
'db/migrate/create_merit_actions.rb'

migration_template 'create_merit_activity_logs.rb',
migration_template 'create_merit_activity_logs.erb',
'db/migrate/create_merit_activity_logs.rb'

migration_template 'create_sashes.rb',
migration_template 'create_sashes.erb',
'db/migrate/create_sashes.rb'

migration_template 'create_badges_sashes.rb',
migration_template 'create_badges_sashes.erb',
'db/migrate/create_badges_sashes.rb'

migration_template 'create_scores_and_points.rb',
migration_template 'create_scores_and_points.erb',
'db/migrate/create_scores_and_points.rb'
end

Expand Down
@@ -1,8 +1,8 @@
require 'rails/generators/active_record'

module ActiveRecord
module Generators
class MeritGenerator < ActiveRecord::Generators::Base
module Merit
module Generators::ActiveRecord
class MeritGenerator < ::ActiveRecord::Generators::Base
include Rails::Generators::Migration

source_root File.expand_path('../templates', __FILE__)
Expand Down
@@ -1,8 +1,8 @@
require 'rails/generators/active_record'

module ActiveRecord
module Generators
class RemoveGenerator < ActiveRecord::Generators::Base
module Merit
module Generators::ActiveRecord
class RemoveGenerator < ::ActiveRecord::Generators::Base
include Rails::Generators::Migration

source_root File.expand_path('../templates', __FILE__)
Expand All @@ -13,11 +13,11 @@ def self.next_migration_number(path)
end

def copy_migrations_and_model
migration_template 'remove_merit_tables.rb',
migration_template 'remove_merit_tables.erb',
'db/migrate/remove_merit_tables.rb'

migration_template(
'remove_merit_fields_from_model.rb',
'remove_merit_fields_from_model.erb',
"db/migrate/remove_merit_fields_from_#{table_name}.rb"
)
end
Expand Down
18 changes: 18 additions & 0 deletions lib/merit/generators/install_generator.rb
@@ -0,0 +1,18 @@
require "rails/generators"

module Merit
module Generators
class InstallGenerator < ::Rails::Generators::Base
source_root File.expand_path('../templates', __FILE__)
hook_for :orm

desc 'Copy config and rules files'
def copy_migrations_and_model
template 'merit.erb', 'config/initializers/merit.rb'
template 'merit_badge_rules.erb', 'app/models/merit/badge_rules.rb'
template 'merit_point_rules.erb', 'app/models/merit/point_rules.rb'
template 'merit_rank_rules.erb', 'app/models/merit/rank_rules.rb'
end
end
end
end
@@ -1,6 +1,8 @@
require "rails/generators"

module Merit
module Generators
class MeritGenerator < Rails::Generators::NamedBase
class MeritGenerator < ::Rails::Generators::NamedBase
source_root File.expand_path('../templates', __FILE__)
hook_for :orm

Expand Down
@@ -1,6 +1,8 @@
require "rails/generators"

module Merit
module Generators
class RemoveGenerator < Rails::Generators::NamedBase
class RemoveGenerator < ::Rails::Generators::NamedBase
source_root File.expand_path('../templates', __FILE__)
hook_for :orm

Expand Down
File renamed without changes.
22 changes: 12 additions & 10 deletions app/models/merit/action.rb → lib/merit/models/action_concern.rb
@@ -1,5 +1,3 @@
require_dependency "merit/models/#{Merit.orm}/merit/action"

# Merit::Action general schema
# ______________________________________________________________
# source | action | target
Expand All @@ -12,27 +10,31 @@
# ______________________________________________________________
#
# Rules relate to merit_actions by action name ('controller#action' string)
module Merit
class Action
def self.check_unprocessed
where(processed: false).find_each(&:check_all_rules)
module Merit::Models
module ActionConcern
extend ActiveSupport::Concern

class_methods do
def check_unprocessed
where(processed: false).find_each(&:check_all_rules)
end
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
check_rules rules_matcher.select_from(Merit::AppBadgeRules), :badges
check_rules rules_matcher.select_from(Merit::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}"
judge = Merit::Judge.new(rule, action: self)
judge.public_send(:"apply_#{badges_or_points}")
end
end

Expand Down
@@ -1,7 +1,11 @@
module Merit
module Merit::Models::ActiveRecord
class Action < ActiveRecord::Base
include Merit::Models::ActionConcern

self.table_name = :merit_actions

has_many :activity_logs, class_name: 'Merit::ActivityLog'
end
end

class Merit::Action < Merit::Models::ActiveRecord::Action; end
@@ -1,4 +1,4 @@
module Merit
module Merit::Models::ActiveRecord
class ActivityLog < ActiveRecord::Base
self.table_name = :merit_activity_logs

Expand All @@ -7,3 +7,5 @@ class ActivityLog < ActiveRecord::Base
has_one :sash, through: :related_change
end
end

class Merit::ActivityLog < Merit::Models::ActiveRecord::ActivityLog; end
@@ -1,6 +1,6 @@
module Merit
module Merit::Models::ActiveRecord
class BadgesSash < ActiveRecord::Base
include Base::BadgesSash
include Merit::Models::BadgesSashConcern

has_many :activity_logs,
class_name: 'Merit::ActivityLog',
Expand All @@ -9,3 +9,5 @@ class BadgesSash < ActiveRecord::Base
validates_presence_of :badge_id, :sash
end
end

class Merit::BadgesSash < Merit::Models::ActiveRecord::BadgesSash; end
@@ -1,12 +1,13 @@
module Merit
module Merit::Models::ActiveRecord
# Sash is a container for reputation data for meritable models. It's an
# indirection between meritable models and badges and scores (one to one
# relationship).
#
# It's existence make join models like badges_users and scores_users
# unnecessary. It should be transparent at the application.
class Sash < ActiveRecord::Base
include Base::Sash
include Merit::Models::SashConcern

has_many :badges_sashes, dependent: :destroy
has_many :scores, dependent: :destroy, class_name: 'Merit::Score'

Expand All @@ -27,3 +28,5 @@ def score_points(options = {})
end
end
end

class Merit::Sash < Merit::Models::ActiveRecord::Sash; end
@@ -1,4 +1,4 @@
module Merit
module Merit::Models::ActiveRecord
class Score < ActiveRecord::Base
self.table_name = :merit_scores
belongs_to :sash
Expand All @@ -20,3 +20,6 @@ class Point < ActiveRecord::Base
end
end
end

class Merit::Score < Merit::Models::ActiveRecord::Score; end
class Merit::Score::Point < Merit::Models::ActiveRecord::Score::Point; end
13 changes: 13 additions & 0 deletions lib/merit/models/badges_sash_concern.rb
@@ -0,0 +1,13 @@
module Merit::Models
module BadgesSashConcern
extend ActiveSupport::Concern

included do
belongs_to :sash
end

def badge
Merit::Badge.find(badge_id)
end
end
end
2 changes: 1 addition & 1 deletion lib/merit/models/base/badges_sash.rb
@@ -1,5 +1,5 @@
module Merit
module Base
module Models::Base
module BadgesSash
extend ActiveSupport::Concern

Expand Down
2 changes: 1 addition & 1 deletion lib/merit/models/base/sash.rb
@@ -1,5 +1,5 @@
module Merit
module Base
module Models::Base
module Sash
def badges
badge_ids.map { |id| Merit::Badge.find id }
Expand Down
53 changes: 53 additions & 0 deletions lib/merit/models/sash_concern.rb
@@ -0,0 +1,53 @@
module Merit::Models
module SashConcern
def badges
badge_ids.map { |id| Merit::Badge.find id }
end

def badge_ids
badges_sashes.map(&:badge_id)
end

def add_badge(badge_id)
bs = Merit::BadgesSash.new(badge_id: badge_id.to_i)
badges_sashes << bs
bs
end

def rm_badge(badge_id)
badges_sashes.where(badge_id: badge_id.to_i).first.try(:destroy)
end

# Retrieve the number of points from a category
# By default all points are summed up
# @param category [String] The category
# @return [Integer] The number of points
def points(options = {})
if (category = options[:category])
scores.where(category: category).first.try(:points) || 0
else
scores.reduce(0) { |sum, score| sum + score.points }
end
end

def add_points(num_points, options = {})
point = Merit::Score::Point.new
point.num_points = num_points
scores
.where(category: options[:category] || 'default')
.first_or_create
.score_points << point
point
end

def subtract_points(num_points, options = {})
add_points(-num_points, options)
end

private

def create_scores
scores << Merit::Score.create
end
end
end
File renamed without changes.
File renamed without changes.

0 comments on commit 0faac7d

Please sign in to comment.