Skip to content

Commit

Permalink
Merge 6b3474d into e67a411
Browse files Browse the repository at this point in the history
  • Loading branch information
jairovm committed Apr 8, 2021
2 parents e67a411 + 6b3474d commit dfda791
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 39 deletions.
16 changes: 10 additions & 6 deletions README.md
Expand Up @@ -58,12 +58,16 @@ Create badges in `config/initializers/merit.rb`
### Example

```ruby
Merit::Badge.create!(
id: 1,
name: "year-member",
description: "Active member for a year",
custom_fields: { difficulty: :silver }
)
# config/initializers/merit.rb

Rails.application.reloader.to_prepare do
Merit::Badge.create!(
id: 1,
name: "year-member",
description: "Active member for a year",
custom_fields: { difficulty: :silver }
)
end
```

## Defining Rules
Expand Down
36 changes: 13 additions & 23 deletions lib/merit.rb
Expand Up @@ -61,32 +61,22 @@ class Engine < Rails::Engine
config.app_generators.orm Merit.orm

initializer 'merit.controller' do |app|
extend_orm_with_has_merit
ActiveSupport.on_load(action_controller_hook) do
begin
# Load app rules on boot up
Merit::AppBadgeRules = Merit::BadgeRules.new.defined_rules
Merit::AppPointRules = Merit::PointRules.new.defined_rules
include Merit::ControllerExtensions
rescue NameError => e
# Trap NameError if installing/generating files
raise e unless
e.to_s =~ /uninitialized constant Merit::(BadgeRules|PointRules)/
config.to_prepare do
ActiveSupport.on_load(:active_record) { include Merit }
ActiveSupport.on_load(app.config.api_only ? :action_controller_api : :action_controller_base) do
begin
# Load app rules on boot up
Merit::AppBadgeRules = Merit::BadgeRules.new.defined_rules
Merit::AppPointRules = Merit::PointRules.new.defined_rules
include Merit::ControllerExtensions
rescue NameError => e
# Trap NameError if installing/generating files
raise e unless
e.to_s =~ /uninitialized constant Merit::(BadgeRules|PointRules)/
end
end
end
end

def extend_orm_with_has_merit
ActiveRecord::Base.include(Merit)
end

def action_controller_hook
if Rails.application.config.api_only
:action_controller_api
else
:action_controller_base
end
end
end
end

Expand Down
22 changes: 12 additions & 10 deletions lib/merit/generators/templates/merit.erb
Expand Up @@ -17,14 +17,16 @@ Merit.setup do |config|
end

# Create application badges (uses https://github.com/norman/ambry)
# badge_id = 0
# [{
# id: (badge_id = badge_id+1),
# name: 'just-registered'
# }, {
# id: (badge_id = badge_id+1),
# name: 'best-unicorn',
# custom_fields: { category: 'fantasy' }
# }].each do |attrs|
# Merit::Badge.create! attrs
# Rails.application.reloader.to_prepare do
# badge_id = 0
# [{
# id: (badge_id = badge_id+1),
# name: 'just-registered'
# }, {
# id: (badge_id = badge_id+1),
# name: 'best-unicorn',
# custom_fields: { category: 'fantasy' }
# }].each do |attrs|
# Merit::Badge.create! attrs
# end
# end

0 comments on commit dfda791

Please sign in to comment.