badgeable 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore CHANGED
@@ -2,3 +2,4 @@ pkg
2
2
  .bundle
3
3
  */.DS_Store
4
4
  .DS_Store
5
+ tmp/*
@@ -1,9 +1,9 @@
1
- $LOAD_PATH.unshift(File.dirname(__FILE__))
2
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__),'..', '..', 'lib'))
1
+ $:.unshift(File.expand_path(File.join(File.dirname(__FILE__),'..', '..', 'lib')))
3
2
 
4
3
  require 'cucumber'
5
4
  require 'rspec/expectations'
6
5
  require 'badgeable'
6
+ require 'mongoid'
7
7
 
8
8
  Mongoid.configure do |config|
9
9
  config.master = Mongo::Connection.new.db("badgeable_test")
@@ -0,0 +1,38 @@
1
+ module Badgeable
2
+ module Adapters
3
+ module ActiveRecordAdapter
4
+ extend ActiveSupport::Concern
5
+
6
+ def self.included(receiver)
7
+ receiver.class_eval %Q{
8
+ has_many :badgings
9
+ has_many :badges, :through => :badgings
10
+ }
11
+
12
+ Badging.class_eval %Q{
13
+ belongs_to :#{receiver.to_s.underscore}
14
+ }
15
+
16
+ Badge.class_eval %Q{
17
+ has_many :badgings
18
+ has_many :#{receiver.to_s.tableize}, :through => :badgings
19
+ }
20
+ end
21
+ end
22
+ end
23
+ end
24
+
25
+
26
+ class Badging < ActiveRecord::Base
27
+ belongs_to :badge
28
+ end
29
+
30
+ class Badge < ActiveRecord::Base
31
+ def self.find_or_create_by_name(name)
32
+ Badge.where(:name => name).first || create(:name => name)
33
+ end
34
+
35
+ def icon
36
+ "/images/#{name}"
37
+ end
38
+ end
@@ -0,0 +1,35 @@
1
+ module Badgeable
2
+ module Adapters
3
+ module MongoidAdapter
4
+ def self.included(receiver)
5
+ receiver.class_eval %Q{
6
+ references_many :badges, :stored_as => :array, :class_name => "Badge", :inverse_of => :#{receiver.to_s.tableize}
7
+ }
8
+
9
+ Badge.class_eval %Q{
10
+ references_many :#{receiver.to_s.tableize}, :inverse_of => :badges, :stored_as => :array
11
+
12
+ def recipients
13
+ #{receiver}.where(:badge_ids => id)
14
+ end
15
+ }
16
+ end
17
+ end
18
+ end
19
+ end
20
+
21
+ class Badge
22
+ include Mongoid::Document
23
+ include Mongoid::Timestamps
24
+
25
+ field :name
26
+ index :name
27
+
28
+ def self.find_or_create_by_name(name)
29
+ criteria.where(:name => name).first || create(:name => name)
30
+ end
31
+
32
+ def icon
33
+ "/images/#{name}"
34
+ end
35
+ end
@@ -0,0 +1,8 @@
1
+ module Badgeable
2
+ module Adapters
3
+ def self.connect(klass)
4
+ klass.send(:include, Badgeable::Adapters::MongoidAdapter) if defined?(Mongoid) && klass.include?(::Mongoid::Document)
5
+ klass.send(:include, Badgeable::Adapters::ActiveRecordAdapter) if defined?(ActiveRecord) && klass.superclass == ActiveRecord::Base
6
+ end
7
+ end
8
+ end
@@ -3,7 +3,7 @@ module Badgeable
3
3
  # Award a named badge to this object. If the badge doesn't exist
4
4
  # in the database already, it's created by name.
5
5
  def award_badge(name)
6
- badge = Badgeable::Badge.find_or_create_by_name(name)
6
+ badge = Badge.find_or_create_by_name(name)
7
7
  badges << badge unless has_badge?(badge)
8
8
  end
9
9
 
@@ -16,17 +16,7 @@ module Badgeable
16
16
  end
17
17
 
18
18
  def self.included(receiver)
19
- receiver.class_eval %Q{
20
- references_many :badges, :stored_as => :array, :class_name => "Badgeable::Badge", :inverse_of => :#{receiver.to_s.tableize}
21
- }
22
-
23
- Badgeable::Badge.class_eval %Q{
24
- references_many :#{receiver.to_s.tableize}, :inverse_of => :badges, :stored_as => :array
25
-
26
- def recipients
27
- #{receiver}.where(:badge_ids => id)
28
- end
29
- }
19
+ Badgeable::Adapters.connect(receiver)
30
20
  end
31
21
  end
32
22
  end
@@ -1,3 +1,3 @@
1
1
  module Badgeable
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
data/lib/badgeable.rb CHANGED
@@ -1,13 +1,21 @@
1
1
  require 'active_support/core_ext'
2
2
  require 'active_support/callbacks'
3
3
  require 'active_support/inflector'
4
- require 'mongoid'
5
- require 'badgeable/award'
6
- require 'badgeable/badge'
7
- require 'badgeable/config'
8
- require 'badgeable/dsl'
9
- require 'badgeable/subject'
10
- require 'badgeable/version'
4
+ require 'active_support/dependencies'
5
+ require 'active_support/concern'
11
6
  require 'badgeable/railtie' if defined?(Rails)
12
7
 
8
+ module Badgeable
9
+ autoload :Adapters, 'badgeable/adapters'
10
+ autoload :Award, 'badgeable/award'
11
+ autoload :Config, 'badgeable/config'
12
+ autoload :Dsl, 'badgeable/dsl'
13
+ autoload :Subject, 'badgeable/subject'
14
+ autoload :Verson, 'badgeable/version'
15
+ module Adapters
16
+ autoload :ActiveRecordAdapter, "badgeable/adapters/active_record_adapter"
17
+ autoload :MongoidAdapter, "badgeable/adapters/mongoid_adapter"
18
+ end
19
+ end
20
+
13
21
  module Badgeable; end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 2
7
+ - 3
8
8
  - 0
9
- version: 0.2.0
9
+ version: 0.3.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Scott Burton
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-10-28 00:00:00 -07:00
17
+ date: 2010-11-01 00:00:00 -07:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -101,8 +101,10 @@ files:
101
101
  - features/support/test_models/review.rb
102
102
  - features/support/test_models/user.rb
103
103
  - lib/badgeable.rb
104
+ - lib/badgeable/adapters.rb
105
+ - lib/badgeable/adapters/active_record_adapter.rb
106
+ - lib/badgeable/adapters/mongoid_adapter.rb
104
107
  - lib/badgeable/award.rb
105
- - lib/badgeable/badge.rb
106
108
  - lib/badgeable/config.rb
107
109
  - lib/badgeable/dsl.rb
108
110
  - lib/badgeable/railtie.rb
@@ -1,18 +0,0 @@
1
- module Badgeable
2
- class Badge
3
- include Mongoid::Document
4
- include Mongoid::Timestamps
5
-
6
- field :name
7
- index :name
8
-
9
- def self.find_or_create_by_name(name)
10
- criteria.where(:name => name).first || create(:name => name)
11
- end
12
-
13
- def icon
14
- "/images/#{name}"
15
- end
16
- end
17
-
18
- end