Skip to content

Commit

Permalink
creating badges implementaiton architecture
Browse files Browse the repository at this point in the history
  • Loading branch information
joaomdmoura committed Aug 3, 2016
1 parent 3a7b30b commit b6353f4
Show file tree
Hide file tree
Showing 11 changed files with 189 additions and 16 deletions.
3 changes: 3 additions & 0 deletions Gemfile
@@ -1,2 +1,5 @@
source "https://rubygems.org"
gemspec

gem "rails", "~> 5.0.0"
gem "rspec", "~> 3.5.0"
103 changes: 102 additions & 1 deletion Gemfile.lock
Expand Up @@ -6,7 +6,93 @@ PATH
GEM
remote: https://rubygems.org/
specs:
actioncable (5.0.0)
actionpack (= 5.0.0)
nio4r (~> 1.2)
websocket-driver (~> 0.6.1)
actionmailer (5.0.0)
actionpack (= 5.0.0)
actionview (= 5.0.0)
activejob (= 5.0.0)
mail (~> 2.5, >= 2.5.4)
rails-dom-testing (~> 2.0)
actionpack (5.0.0)
actionview (= 5.0.0)
activesupport (= 5.0.0)
rack (~> 2.0)
rack-test (~> 0.6.3)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.0, >= 1.0.2)
actionview (5.0.0)
activesupport (= 5.0.0)
builder (~> 3.1)
erubis (~> 2.7.0)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.0, >= 1.0.2)
activejob (5.0.0)
activesupport (= 5.0.0)
globalid (>= 0.3.6)
activemodel (5.0.0)
activesupport (= 5.0.0)
activerecord (5.0.0)
activemodel (= 5.0.0)
activesupport (= 5.0.0)
arel (~> 7.0)
activesupport (5.0.0)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (~> 0.7)
minitest (~> 5.1)
tzinfo (~> 1.1)
arel (7.0.0)
builder (3.2.2)
concurrent-ruby (1.0.2)
diff-lcs (1.2.5)
erubis (2.7.0)
globalid (0.3.6)
activesupport (>= 4.1.0)
i18n (0.7.0)
loofah (2.0.3)
nokogiri (>= 1.5.9)
mail (2.6.4)
mime-types (>= 1.16, < 4)
method_source (0.8.2)
mime-types (3.1)
mime-types-data (~> 3.2015)
mime-types-data (3.2016.0521)
mini_portile2 (2.1.0)
minitest (5.9.0)
nio4r (1.2.1)
nokogiri (1.6.8)
mini_portile2 (~> 2.1.0)
pkg-config (~> 1.1.7)
pkg-config (1.1.7)
rack (2.0.1)
rack-test (0.6.3)
rack (>= 1.0)
rails (5.0.0)
actioncable (= 5.0.0)
actionmailer (= 5.0.0)
actionpack (= 5.0.0)
actionview (= 5.0.0)
activejob (= 5.0.0)
activemodel (= 5.0.0)
activerecord (= 5.0.0)
activesupport (= 5.0.0)
bundler (>= 1.3.0, < 2.0)
railties (= 5.0.0)
sprockets-rails (>= 2.0.0)
rails-dom-testing (2.0.1)
activesupport (>= 4.2.0, < 6.0)
nokogiri (~> 1.6.0)
rails-html-sanitizer (1.0.3)
loofah (~> 2.0)
railties (5.0.0)
actionpack (= 5.0.0)
activesupport (= 5.0.0)
method_source
rake (>= 0.8.7)
thor (>= 0.18.1, < 2.0)
rake (11.2.2)
rspec (3.5.0)
rspec-core (~> 3.5.0)
rspec-expectations (~> 3.5.0)
Expand All @@ -20,13 +106,28 @@ GEM
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.5.0)
rspec-support (3.5.0)
sprockets (3.6.3)
concurrent-ruby (~> 1.0)
rack (> 1, < 3)
sprockets-rails (3.1.1)
actionpack (>= 4.0)
activesupport (>= 4.0)
sprockets (>= 3.0.0)
thor (0.19.1)
thread_safe (0.3.5)
tzinfo (1.2.2)
thread_safe (~> 0.1)
websocket-driver (0.6.4)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.2)

PLATFORMS
ruby

DEPENDENCIES
gioco!
rspec
rails (~> 5.0.0)
rspec (~> 3.5.0)

BUNDLED WITH
1.12.5
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -17,7 +17,7 @@ With **Gioco** you are able to implement gamification logic such as badges, leve
adding it in Gemfile:

```ruby
gem 'gioco'
gem "gioco"
```

and running the bundler:
Expand Down
3 changes: 3 additions & 0 deletions circle.yml
@@ -0,0 +1,3 @@
machine:
ruby:
version: 2.2.2
24 changes: 14 additions & 10 deletions gioco.gemspec
@@ -1,13 +1,17 @@
# -*- encoding: utf-8 -*-
$:.push File.expand_path("../lib", __FILE__)
require "gioco/version"

Gem::Specification.new do |s|
s.name = 'gioco'
s.version = File.read(File.dirname(__FILE__) + '/VERSION').strip
s.date = '2016-07-13'
s.summary = 'A gamification gem for Ruby on Rails applications.'
s.description = 'Gioco is a gem to easly implement gamification.'
s.name = "gioco"
s.version = Gioco::VERSION.dup
s.date = "2016-07-13"
s.platform = Gem::Platform::RUBY
s.summary = "A gamification gem for Ruby on Rails applications."
s.description = "Gioco is a gem to easly implement gamification."
s.authors = ["João Moura"]
s.email = 'joaomdmoura@gmail.com'
s.files = Dir[ 'lib/*' ]
s.homepage = 'https://github.com/GiocoApp/gioco'
s.license = 'MIT'
s.add_development_dependency 'rspec'
s.email = "joaomdmoura@gmail.com"
s.files = Dir["lib"]
s.homepage = "https://github.com/joaomdmoura/gioco"
s.licenses = ["MIT"]
end
5 changes: 4 additions & 1 deletion lib/generators/gioco_generator.rb
Expand Up @@ -10,7 +10,10 @@ class GiocoGenerator < Rails::Generators::Base
desc "Setup gioco for a specific model"
argument :model, type: :string

class_option :badges, aliases: :b, type: :boolean, default: false, desc: "Setup a badges architecture"

# Main generator method responsible for gioco's setup
def execute
setup_models
setup_models(options)
end
end
31 changes: 31 additions & 0 deletions lib/generators/helpers/model_generator_helper.rb
@@ -0,0 +1,31 @@
##
# ModelGeneratorHelper is a helper that contains all method and logic related
# the the model generation and relationships definitions when running gioco generator.

module ModelGeneratorHelper

# Responsible for creating the new models and adding relationships.
#
# ==== Attributes
#
# * +options+ - A Hash containing the generators options passed on GiocoGenerator
#
def setup_models(options)
setup_badges if options[:badges]
end

private

# Creates all badges related model, migrations and injections.
def setup_badges
generate("model", "badge name:string")
generate("migration", "create_badges_and_#{model.pluralize} badge:references #{model}:references")
add_relationship("badge", model.pluralize, "has_and_belongs_to_many")
add_relationship(model, "badges", "has_and_belongs_to_many")
end

# Helper method for easly inject relationships into models classes.
def add_relationship (model, related, relation)
inject_into_class "app/models/#{model}.rb", model.capitalize, "#{relation} :#{related}\n"
end
end
2 changes: 1 addition & 1 deletion lib/gioco.rb
@@ -1,5 +1,5 @@
##
# This is Gioco main class, it's the first loaded when requiring gioco

class Gioco
module Gioco
end
3 changes: 3 additions & 0 deletions lib/gioco/version.rb
@@ -0,0 +1,3 @@
module Gioco
VERSION = "2.0.0".freeze
end
22 changes: 22 additions & 0 deletions spec/lib/generators/gioco_generator_spec.rb
@@ -0,0 +1,22 @@
require "generators/gioco_generator"

RSpec.describe GiocoGenerator do
describe "#execute" do
let(:model) { 'user'.freeze }

context 'setting up gioco with badges architecture' do
let(:subject) { GiocoGenerator.new([ model ], badges: true) }

it "generates the badges model and inject the relationships" do
allow(subject).to receive(:inject_into_class)

expect(subject).to receive(:generate).with("model", "badge name:string")
expect(subject).to receive(:generate).with("migration", "create_badges_and_#{model.pluralize} badge:references #{model}:references")
expect(subject).to receive(:inject_into_class).with("app/models/badge.rb", 'Badge', "has_and_belongs_to_many :users\n")
expect(subject).to receive(:inject_into_class).with("app/models/#{model}.rb", model.capitalize, "has_and_belongs_to_many :badges\n")

subject.execute
end
end
end
end
7 changes: 5 additions & 2 deletions spec/spec_helper.rb
@@ -1,3 +1,6 @@
require 'rails'
require 'rails/generators'

# This file was generated by the `rspec --init` command. Conventionally, all
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
# The generated `.rspec` file contains `--require spec_helper` which will cause
Expand Down Expand Up @@ -69,7 +72,7 @@
# - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
config.disable_monkey_patching!
# This setting enables warnings. It's recommended, but in some cases may
# This setting enables warnings. it's recommended, but in some cases may
# be too noisy due to issues in dependencies.
config.warnings = true
Expand All @@ -80,7 +83,7 @@
# Use the documentation formatter for detailed output,
# unless a formatter has already been configured
# (e.g. via a command-line flag).
config.default_formatter = 'doc'
config.default_formatter = "doc"
end
# Print the 10 slowest examples and example groups at the
Expand Down

0 comments on commit b6353f4

Please sign in to comment.