acts_as_starable 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +19 -0
  3. data/Gemfile +4 -0
  4. data/LICENSE +22 -0
  5. data/README.md +204 -0
  6. data/Rakefile +9 -0
  7. data/acts_as_starable.gemspec +30 -0
  8. data/lib/acts_as_starable.rb +10 -0
  9. data/lib/acts_as_starable/railtie.rb +15 -0
  10. data/lib/acts_as_starable/star_scopes.rb +29 -0
  11. data/lib/acts_as_starable/starable.rb +62 -0
  12. data/lib/acts_as_starable/starer.rb +77 -0
  13. data/lib/acts_as_starable/starer_lib.rb +33 -0
  14. data/lib/acts_as_starable/version.rb +3 -0
  15. data/lib/generators/USAGE +5 -0
  16. data/lib/generators/acts_as_starable_generator.rb +30 -0
  17. data/lib/generators/templates/migration.rb +12 -0
  18. data/lib/generators/templates/model.rb +9 -0
  19. data/test/acts_as_starable_test.rb +85 -0
  20. data/test/acts_as_starer_test.rb +112 -0
  21. data/test/dummy40/Gemfile +5 -0
  22. data/test/dummy40/Rakefile +7 -0
  23. data/test/dummy40/app/models/band.rb +4 -0
  24. data/test/dummy40/app/models/user.rb +5 -0
  25. data/test/dummy40/config.ru +4 -0
  26. data/test/dummy40/config/application.rb +23 -0
  27. data/test/dummy40/config/boot.rb +4 -0
  28. data/test/dummy40/config/database.yml +7 -0
  29. data/test/dummy40/config/environment.rb +5 -0
  30. data/test/dummy40/config/environments/development.rb +25 -0
  31. data/test/dummy40/config/environments/test.rb +34 -0
  32. data/test/dummy40/config/initializers/backtrace_silencers.rb +7 -0
  33. data/test/dummy40/config/initializers/inflections.rb +10 -0
  34. data/test/dummy40/config/initializers/secret_token.rb +7 -0
  35. data/test/dummy40/config/initializers/session_store.rb +8 -0
  36. data/test/dummy40/config/locales/en.yml +5 -0
  37. data/test/dummy40/config/routes.rb +2 -0
  38. data/test/factories/bands.rb +9 -0
  39. data/test/factories/users.rb +9 -0
  40. data/test/schema.rb +20 -0
  41. data/test/test_helper.rb +10 -0
  42. metadata +179 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f55b16c60ecf748b275e8f95bbaec06d431f528c
4
+ data.tar.gz: 09dc24f6972faf8cc15a9b42f870e171e4805e46
5
+ SHA512:
6
+ metadata.gz: 148a073e5dacea55a2de1ac632bfa5fb91c81239f21a1020f941aaf7c36a5e8a3c851297af9a84670a906d7d4938975da2b8d627db67d56287139d32ff226214
7
+ data.tar.gz: ac5d75e45dcf06c38f85a3d6aa42d2c409565f29cd21682a1f77b6c7013da6c431e5b996f95c670b532b79117991168c47c021dc22bb0355432a3e2864a26ddb
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ test/dummy40/log/test.log
19
+ test/debug.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in acts_as_starable.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Dominic Giglio
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,204 @@
1
+ # acts_as_starable
2
+
3
+ This gem is based on the structure, functionality, and features of the [acts_as_follower](https://github.com/tcocca/acts_as_follower) gem. It provides the files and methods necessary to enable one ActiveRecord model to star, or to be starred, by another.
4
+
5
+ ## Compatibility
6
+
7
+ `acts_as_starable` was developed on and tested against Rails `~>4.0.0`. Your mileage will vary on prior versions. It is fairly dependent on both Rails and ActiveRecord but there shouldn't be too much trouble getting it to work on any Rack based application. If anyone is interested in contributing support for Rails versions prior to `4.0.0` please see the [contributing](https://github.com/littlstar/acts_as_starable#contributing) section below.
8
+
9
+
10
+ ## Installation
11
+
12
+ Add this line to your Gemfile:
13
+
14
+ ```ruby
15
+ gem 'acts_as_starable'
16
+ ```
17
+
18
+ And then execute:
19
+
20
+ ```bash
21
+ $ bundle
22
+ ```
23
+
24
+ Run the generator:
25
+
26
+ ```bash
27
+ $ rails generate acts_as_starable
28
+ ```
29
+
30
+ This will generate a migration file as well as a model called Star.
31
+
32
+ Run the migration:
33
+
34
+ ```bash
35
+ $ rails db:migrate
36
+ ```
37
+
38
+ ## Usage
39
+
40
+ There are three parts to enabling and using this gem. First you have to enable the required functionality on the models that you would like to be able to star other models or those that can be starred. After that you can use the methods defined in the `ActsAsStarable::Starer` module to work with a model that is able to star other models; or those defined in the `ActsAsStarable::Starable` module to work with models that are going to be starred by other models.
41
+
42
+ ### Setup
43
+
44
+ To enable one model to star other models, add the `acts_as_starer` method to that model:
45
+
46
+ ```ruby
47
+ class User < ActiveRecord::Base
48
+ ...
49
+ acts_as_starer
50
+ ...
51
+ end
52
+ ```
53
+
54
+ To enable one model to be starred by other models, add the `acts_as_starable` method to that model:
55
+
56
+ ```ruby
57
+ class Band < ActiveRecord::Base
58
+ ...
59
+ acts_as_starable
60
+ ...
61
+ end
62
+ ```
63
+
64
+ Once this functionality has been defined you can use the methods in each module to query your models for the state of the stars in your application.
65
+
66
+ ### acts_as_starer methods
67
+
68
+ To have one object star another:
69
+
70
+ ```ruby
71
+ # grab a starer and a starable
72
+ user = User.first
73
+ band = Band.first
74
+
75
+ # Create a record for the user as the starer and the band as the starable
76
+ user.star(band)
77
+ ```
78
+
79
+ To remove the previously created star:
80
+
81
+ ```ruby
82
+ user.unstar(band)
83
+ ```
84
+
85
+ To find out if a starer model has starred a starable model:
86
+
87
+ ```ruby
88
+ user.starred?(band)
89
+ ```
90
+
91
+ To get a count of stars created by a starer model:
92
+
93
+ ```ruby
94
+ user.stars_count
95
+ ```
96
+
97
+ To get a collection of star objects from the Star table by type:
98
+
99
+ ```ruby
100
+ user.stars_by_type('Band')
101
+
102
+ # also accepts ActiveRecord options
103
+ user.stars_by_type('Band', limit: 5)
104
+ ```
105
+
106
+ To get a collection of the actual starable model records by type:
107
+
108
+ ```ruby
109
+ user.starred_by_type('Band')
110
+
111
+ # also accepts ActiveRecord options
112
+ user.starred_by_type('Band', limit: 5)
113
+ ```
114
+
115
+ To get a collection of all star objects from the Star table:
116
+
117
+ ```ruby
118
+ user.all_stars
119
+
120
+ # also accepts ActiveRecord options
121
+ user.all_stars(limit: 5)
122
+ ```
123
+
124
+ To get a collection of all of the actual starable model records:
125
+
126
+ ```ruby
127
+ user.all_starred
128
+
129
+ # also accepts ActiveRecord options
130
+ user.all_starred(limit: 5)
131
+ ```
132
+
133
+ ### acts_as_starable methods
134
+
135
+ To find out if a starable model has been starred by a starer model:
136
+
137
+ ```ruby
138
+ band.starred_by?(user)
139
+ ```
140
+
141
+ To get a count of the number of times a starable model has been starred:
142
+
143
+ ```ruby
144
+ band.starings_count
145
+ ```
146
+
147
+ To get a collection of star objects from the Star table by type:
148
+
149
+ ```ruby
150
+ band.starings_by_type('User')
151
+
152
+ # also accepts ActiveRecord options
153
+ band.starings_by_type('User', limit: 5)
154
+ ```
155
+
156
+ To get a collection of the actual starer model records by type:
157
+
158
+ ```ruby
159
+ band.starers_by_type('User')
160
+
161
+ # also accepts ActiveRecord options
162
+ band.starers_by_type('User', limit: 5)
163
+ ```
164
+
165
+ To get a collection of all star objects from the Star table:
166
+
167
+ ```ruby
168
+ band.all_starings
169
+
170
+ # also accepts ActiveRecord options
171
+ band.all_starings(limit: 5)
172
+ ```
173
+
174
+ To get a collection of all of the actual starer model records:
175
+
176
+ ```ruby
177
+ band.all_starers
178
+
179
+ # also accepts ActiveRecord options
180
+ band.all_starers(limit: 5)
181
+ ```
182
+
183
+ ## Tests
184
+
185
+ Testing works as usual:
186
+
187
+ ```bash
188
+ git clone https://github.com/littlstar/acts_as_starable.git
189
+ cd acts_as_starable
190
+ bundle install
191
+ rake
192
+ ```
193
+
194
+ ## Contributing
195
+
196
+ 1. Fork it
197
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
198
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
199
+ 4. Push to the branch (`git push origin my-new-feature`)
200
+ 5. Create new Pull Request
201
+
202
+ ## Attribution
203
+
204
+ I can only take a small amount of credit for the creation of this gem. Its structure, functionality, and features are based in large part on the excellent work of @tcocca (and other [contributors](https://github.com/tcocca/acts_as_follower#contributers)) on the [acts_as_follower](https://github.com/tcocca/acts_as_follower) gem.
@@ -0,0 +1,9 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << 'test'
6
+ t.test_files = FileList['test/*_test.rb']
7
+ end
8
+
9
+ task default: :test
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ require 'acts_as_starable/version'
6
+
7
+ Gem::Specification.new do |gem|
8
+ gem.name = 'acts_as_starable'
9
+ gem.version = ActsAsStarable::VERSION
10
+ gem.authors = ['Dominic Giglio']
11
+ gem.email = ['dom@littlstar.com']
12
+ gem.homepage = 'https://github.com/littlstar/acts_as_starable'
13
+ gem.summary = %q{A Rubygem to add staring functionality to ActiveRecord models}
14
+ gem.description = %q{acts_as_starable is a gem that allows a model to act as a starer of other models or one that can be starred.)}
15
+ gem.license = 'MIT'
16
+
17
+ gem.rubyforge_project = 'acts_as_starable'
18
+
19
+ gem.files = `git ls-files`.split("\n")
20
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
21
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
22
+ gem.require_paths = ['lib']
23
+
24
+ #-- development dependencies
25
+ gem.add_development_dependency 'sqlite3'
26
+ gem.add_development_dependency 'rails', '~> 4.0.0'
27
+ gem.add_development_dependency 'factory_girl'
28
+ gem.add_development_dependency 'minitest'
29
+ gem.add_development_dependency 'turn'
30
+ end
@@ -0,0 +1,10 @@
1
+ require 'acts_as_starable/version'
2
+
3
+ module ActsAsStarable
4
+ autoload :Starer, 'acts_as_starable/starer'
5
+ autoload :Starable, 'acts_as_starable/starable'
6
+ autoload :StarerLib, 'acts_as_starable/starer_lib'
7
+ autoload :StarScopes, 'acts_as_starable/star_scopes'
8
+
9
+ require 'acts_as_starable/railtie' if defined?(Rails) && Rails::VERSION::MAJOR >= 4
10
+ end
@@ -0,0 +1,15 @@
1
+ require 'acts_as_starable'
2
+ require 'rails'
3
+
4
+ module ActsAsStarable
5
+ class Railtie < Rails::Railtie
6
+
7
+ initializer "acts_as_starable.active_record" do |app|
8
+ ActiveSupport.on_load :active_record do
9
+ include ActsAsStarable::Starer
10
+ include ActsAsStarable::Starable
11
+ end
12
+ end
13
+
14
+ end
15
+ end
@@ -0,0 +1,29 @@
1
+ module ActsAsStarable #:nodoc:
2
+ module StarScopes
3
+
4
+ def for_starer(starer)
5
+ where(starer_id: starer.id, starer_type: parent_class_name(starer))
6
+ end
7
+
8
+ def for_starable(starable)
9
+ where(starable_id: starable.id, starable_type: parent_class_name(starable))
10
+ end
11
+
12
+ def for_starer_type(starer_type)
13
+ where(starer_type: starer_type)
14
+ end
15
+
16
+ def for_starable_type(starable_type)
17
+ where(starable_type: starable_type)
18
+ end
19
+
20
+ def recent(from)
21
+ where(["created_at > ?", (from || 2.weeks.ago).to_s(:db)])
22
+ end
23
+
24
+ def descending
25
+ order("stars.created_at DESC")
26
+ end
27
+
28
+ end
29
+ end
@@ -0,0 +1,62 @@
1
+ module ActsAsStarable #:nodoc:
2
+ module Starable
3
+
4
+ def self.included(base)
5
+ base.extend ClassMethods
6
+ end
7
+
8
+ module ClassMethods
9
+ def acts_as_starable
10
+ has_many :starings, as: :starable, dependent: :destroy, class_name: 'Star'
11
+ include ActsAsStarable::Starable::InstanceMethods
12
+ include ActsAsStarable::StarerLib
13
+ end
14
+ end
15
+
16
+ module InstanceMethods
17
+
18
+ # Returns true if the current instance has been starred by the passed record
19
+ def starred_by?(starer)
20
+ self.starings.for_starer(starer).first.present?
21
+ end
22
+
23
+ # Returns the number of objects that have starred this instance.
24
+ def starings_count
25
+ self.starings.count
26
+ end
27
+
28
+ # returns the star records to the current instance
29
+ def starings_scoped
30
+ self.starings.includes(:starer)
31
+ end
32
+
33
+ # Returns the star records related to this instance by type.
34
+ def starings_by_type(starer_type, options={})
35
+ starings_scope = starings_scoped.for_starer_type(starer_type)
36
+ starings_scope = apply_options_to_scope(starings_scope, options)
37
+ end
38
+
39
+ # Returns the actual records of a particular type which have starred this record.
40
+ def starers_by_type(starer_type, options={})
41
+ starings_by_type(starer_type, options).collect { |f| f.starer }
42
+ end
43
+
44
+ # Returns the star records related to this instance with the starer included.
45
+ def all_starings(options={})
46
+ apply_options_to_scope(starings_scoped, options)
47
+ end
48
+
49
+ # Returns the actual records which have starred this instance.
50
+ def all_starers(options={})
51
+ all_starings(options).collect { |f| f.starer }
52
+ end
53
+
54
+ # Returns a star record for the current instance and starer object.
55
+ def get_star_for(starer)
56
+ self.starings.for_starer(starer).first
57
+ end
58
+
59
+ end
60
+
61
+ end
62
+ end
@@ -0,0 +1,77 @@
1
+ module ActsAsStarable #:nodoc:
2
+ module Starer
3
+
4
+ def self.included(base)
5
+ base.extend ClassMethods
6
+ end
7
+
8
+ module ClassMethods
9
+ def acts_as_starer
10
+ has_many :stars, as: :starer, dependent: :destroy
11
+ include ActsAsStarable::Starer::InstanceMethods
12
+ include ActsAsStarable::StarerLib
13
+ end
14
+ end
15
+
16
+ module InstanceMethods
17
+
18
+ # Creates a new star record for this instance to star the passed object.
19
+ # Does not allow duplicate records to be created.
20
+ def star(starable)
21
+ if self != starable
22
+ self.stars.find_or_create_by(starable_id: starable.id, starable_type: parent_class_name(starable))
23
+ end
24
+ end
25
+
26
+ # Deletes the star record if it exists.
27
+ def unstar(starable)
28
+ if star = get_star_from(starable)
29
+ star.destroy
30
+ end
31
+ end
32
+
33
+ # Returns true if this instance has starred the object passed as an argument.
34
+ def starred?(starable)
35
+ self.stars.for_starable(starable).first.present?
36
+ end
37
+
38
+ # Returns the number of objects this instance has starred.
39
+ def stars_count
40
+ self.stars.count
41
+ end
42
+
43
+ # returns the star records to the current instance
44
+ def stars_scoped
45
+ self.stars.includes(:starable)
46
+ end
47
+
48
+ # Returns the star records related to this instance by type.
49
+ def stars_by_type(starable_type, options={})
50
+ stars_scope = stars_scoped.for_starable_type(starable_type)
51
+ stars_scope = apply_options_to_scope(stars_scope, options)
52
+ end
53
+
54
+ # Returns the actual records of a particular type which this record has starred.
55
+ def starred_by_type(starable_type, options={})
56
+ stars_by_type(starable_type, options).collect { |f| f.starable }
57
+ end
58
+
59
+ # Returns the star records related to this instance with the starable included.
60
+ def all_stars(options={})
61
+ apply_options_to_scope(stars_scoped, options)
62
+ end
63
+
64
+ # Returns the actual records which this instance has starred.
65
+ def all_starred(options={})
66
+ all_stars(options).collect { |f| f.starable }
67
+ end
68
+
69
+ # Returns a star record for the current instance and starable object.
70
+ def get_star_from(starable)
71
+ self.stars.for_starable(starable).first
72
+ end
73
+
74
+ end
75
+
76
+ end
77
+ end
@@ -0,0 +1,33 @@
1
+ module ActsAsStarable
2
+ module StarerLib
3
+
4
+ private
5
+
6
+ # Retrieves the parent class name if using STI.
7
+ def parent_class_name(obj)
8
+ if obj.class.superclass != ActiveRecord::Base
9
+ return obj.class.superclass.name
10
+ end
11
+ return obj.class.name
12
+ end
13
+
14
+ def apply_options_to_scope(scope, options = {})
15
+ if options.has_key?(:limit)
16
+ scope = scope.limit(options[:limit])
17
+ end
18
+ if options.has_key?(:includes)
19
+ scope = scope.includes(options[:includes])
20
+ end
21
+ if options.has_key?(:joins)
22
+ scope = scope.joins(options[:joins])
23
+ end
24
+ if options.has_key?(:where)
25
+ scope = scope.where(options[:where])
26
+ end
27
+ if options.has_key?(:order)
28
+ scope = scope.order(options[:order])
29
+ end
30
+ scope
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,3 @@
1
+ module ActsAsStarable
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,5 @@
1
+ Description:
2
+ rails generate acts_as_starable
3
+
4
+ no need to specify a name after acts_as_starable as you can not change the model name from Star
5
+ the acts_as_starable_migration file will be created in db/migrate
@@ -0,0 +1,30 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/migration'
3
+
4
+ class ActsAsStarableGenerator < Rails::Generators::Base
5
+
6
+ include Rails::Generators::Migration
7
+
8
+ def self.source_root
9
+ @source_root ||= File.join(File.dirname(__FILE__), 'templates')
10
+ end
11
+
12
+ # Implement the required interface for Rails::Generators::Migration.
13
+ # taken from http://github.com/rails/rails/blob/master/activerecord/lib/generators/active_record.rb
14
+ def self.next_migration_number(dirname)
15
+ if ActiveRecord::Base.timestamped_migrations
16
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
17
+ else
18
+ "%.3d" % (current_migration_number(dirname) + 1)
19
+ end
20
+ end
21
+
22
+ def create_migration_file
23
+ migration_template 'migration.rb', 'db/migrate/acts_as_starable_migration.rb'
24
+ end
25
+
26
+ def create_model
27
+ template 'model.rb', File.join('app/models', 'star.rb')
28
+ end
29
+
30
+ end
@@ -0,0 +1,12 @@
1
+ class ActsAsStarableMigration < ActiveRecord::Migration
2
+ def change
3
+ create_table :stars, force: true do |t|
4
+ t.references :starer, polymorphic: true, null: false
5
+ t.references :starable, polymorphic: true, null: false
6
+ t.timestamps
7
+ end
8
+
9
+ add_index :stars, ["starable_id", "starable_type"]
10
+ add_index :stars, ["starer_id", "starer_type"]
11
+ end
12
+ end
@@ -0,0 +1,9 @@
1
+ class Star < ActiveRecord::Base
2
+
3
+ extend ActsAsStarable::StarerLib
4
+ extend ActsAsStarable::StarScopes
5
+
6
+ belongs_to :starable, polymorphic: true
7
+ belongs_to :starer, polymorphic: true
8
+
9
+ end
@@ -0,0 +1,85 @@
1
+ require 'test_helper'
2
+
3
+ class ActsAsStarableTest < ActiveSupport::TestCase
4
+
5
+ def setup
6
+ @dom = FactoryGirl.create :dom
7
+ @joe = FactoryGirl.create :joe
8
+ @oar = FactoryGirl.create :oar
9
+ end
10
+
11
+ test 'instance methods defined' do
12
+ assert @oar.respond_to? :starred_by?
13
+ assert @oar.respond_to? :starings_count
14
+ assert @oar.respond_to? :starings_scoped
15
+ assert @oar.respond_to? :starings_by_type
16
+ assert @oar.respond_to? :starers_by_type
17
+ assert @oar.respond_to? :all_starings
18
+ assert @oar.respond_to? :all_starers
19
+ assert @oar.respond_to? :get_star_for
20
+ end
21
+
22
+ test 'returns starred by status' do
23
+ @dom.star @oar
24
+ assert @oar.starred_by? @dom
25
+ end
26
+
27
+ test 'returns starings count' do
28
+ @dom.star @oar
29
+ assert_equal 1, @oar.starings_count
30
+ end
31
+
32
+ test 'returns starings by type' do
33
+ @dom.star @oar
34
+ assert_equal [Star.first], @oar.starings_by_type('User')
35
+ @joe.star @dom
36
+ assert_equal [Star.last], @dom.starings_by_type('User')
37
+
38
+ # also accepts AR options
39
+ @joe.star @oar
40
+ assert_equal 1, @oar.starings_by_type('User', limit: 1).count
41
+ end
42
+
43
+ test 'returns starers by type' do
44
+ @dom.star @oar
45
+ assert_equal [User.first], @oar.starers_by_type('User')
46
+
47
+ # also accepts AR options
48
+ @joe.star @oar
49
+ assert_equal 1, @oar.starers_by_type('User', limit: 1).count
50
+ end
51
+
52
+ test 'returns all starings' do
53
+ assert_equal [], @oar.all_starings
54
+ @dom.star @oar
55
+ @joe.star @oar
56
+ assert_equal Star.all.to_a, @oar.all_starings
57
+
58
+ # also accepts AR options
59
+ assert_equal 1, @oar.all_starings(limit: 1).count
60
+ end
61
+
62
+ test 'returns all starers' do
63
+ assert_equal [], @oar.all_starers
64
+ @dom.star @oar
65
+ @joe.star @oar
66
+ assert_equal User.all.to_a, @oar.all_starers
67
+
68
+ # also accepts AR options
69
+ assert_equal 1, @oar.all_starers(limit: 1).count
70
+ end
71
+
72
+ test 'dependent destroy' do
73
+ @dom.star @oar
74
+ assert_difference 'Star.count', -1 do
75
+ @dom.destroy
76
+ end
77
+ end
78
+
79
+ def teardown
80
+ User.destroy_all
81
+ Band.destroy_all
82
+ Star.destroy_all
83
+ end
84
+
85
+ end
@@ -0,0 +1,112 @@
1
+ require 'test_helper'
2
+
3
+ class ActsAsStarerTest < ActiveSupport::TestCase
4
+
5
+ def setup
6
+ @dom = FactoryGirl.create :dom
7
+ @joe = FactoryGirl.create :joe
8
+ @oar = FactoryGirl.create :oar
9
+ @stp = FactoryGirl.create :stp
10
+ end
11
+
12
+ test 'instance methods defined' do
13
+ assert @dom.respond_to? :star
14
+ assert @dom.respond_to? :unstar
15
+ assert @dom.respond_to? :starred?
16
+ assert @dom.respond_to? :stars_count
17
+ assert @dom.respond_to? :stars_scoped
18
+ assert @dom.respond_to? :stars_by_type
19
+ assert @dom.respond_to? :starred_by_type
20
+ assert @dom.respond_to? :all_stars
21
+ assert @dom.respond_to? :all_starred
22
+ assert @dom.respond_to? :get_star_from
23
+ end
24
+
25
+ test 'starring and unstarring' do
26
+
27
+ # star
28
+ assert_difference 'Star.count', 1 do
29
+ @dom.star @oar
30
+ end
31
+
32
+ # can't star self
33
+ assert_difference 'Star.count', 0 do
34
+ @dom.star @dom
35
+ end
36
+
37
+ # unstar
38
+ assert_difference 'Star.count', -1 do
39
+ @dom.unstar @oar
40
+ end
41
+
42
+ end
43
+
44
+ test 'returns starred status' do
45
+ @dom.star @oar
46
+ assert @dom.starred? @oar
47
+ end
48
+
49
+ test 'returns stars count' do
50
+ @dom.star @oar
51
+ assert_equal 1, @dom.stars_count
52
+ end
53
+
54
+ test 'returns stars by type' do
55
+ @dom.star @oar
56
+ assert_equal [Star.first], @dom.stars_by_type('Band')
57
+ assert_equal [], @dom.stars_by_type('User')
58
+ @joe.star @dom
59
+ assert_equal [Star.last], @joe.stars_by_type('User')
60
+ assert_equal [], @joe.stars_by_type('Band')
61
+
62
+ # also accepts AR options
63
+ @dom.star @stp
64
+ assert_equal 1, @dom.stars_by_type('Band', limit: 1).count
65
+ end
66
+
67
+ test 'returns starred by type' do
68
+ @dom.star @oar
69
+ assert_equal [Band.first], @dom.starred_by_type('Band')
70
+ @joe.star @dom
71
+ assert_equal [User.first], @joe.starred_by_type('User')
72
+
73
+ # also accepts AR options
74
+ assert_equal 1, @dom.starred_by_type('Band', limit: 1).count
75
+ end
76
+
77
+ test 'returns all stars' do
78
+ assert_equal [], @dom.all_stars
79
+ @dom.star @oar
80
+ @dom.star @stp
81
+ @dom.star @joe
82
+ assert_equal Star.all.to_a, @dom.all_stars
83
+
84
+ # also accepts AR options
85
+ assert_equal 1, @dom.all_stars(limit: 1).count
86
+ end
87
+
88
+ test 'returns all starred' do
89
+ assert_equal [], @dom.all_starred
90
+ @dom.star @oar
91
+ @dom.star @stp
92
+ @dom.star @joe
93
+ assert_equal Band.all.to_a + [User.last], @dom.all_starred
94
+
95
+ # also accepts AR options
96
+ assert_equal 1, @dom.all_starred(limit: 1).count
97
+ end
98
+
99
+ test 'dependent destroy' do
100
+ @dom.star @oar
101
+ assert_difference 'Star.count', -1 do
102
+ @dom.destroy
103
+ end
104
+ end
105
+
106
+ def teardown
107
+ User.destroy_all
108
+ Band.destroy_all
109
+ Star.destroy_all
110
+ end
111
+
112
+ end
@@ -0,0 +1,5 @@
1
+ gem 'rails', '~>4.0.0'
2
+ gem 'sqlite3'
3
+ gem 'minitest'
4
+ gem 'factory_girl'
5
+ gem 'turn'
@@ -0,0 +1,7 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require File.expand_path('../config/application', __FILE__)
5
+ require 'rake'
6
+
7
+ Dummy::Application.load_tasks
@@ -0,0 +1,4 @@
1
+ class Band < ActiveRecord::Base
2
+ validates_presence_of :name
3
+ acts_as_starable
4
+ end
@@ -0,0 +1,5 @@
1
+ class User < ActiveRecord::Base
2
+ validates_presence_of :name
3
+ acts_as_starer
4
+ acts_as_starable
5
+ end
@@ -0,0 +1,4 @@
1
+ # This file is used by Rack-based servers to start the application.
2
+
3
+ require ::File.expand_path('../config/environment', __FILE__)
4
+ run Dummy::Application
@@ -0,0 +1,23 @@
1
+ require File.expand_path('../boot', __FILE__)
2
+
3
+ require "active_model/railtie"
4
+ require "active_record/railtie"
5
+
6
+ Bundler.require
7
+ require "acts_as_starable"
8
+
9
+ module Dummy
10
+ class Application < Rails::Application
11
+ # Settings in config/environments/* take precedence over those specified here.
12
+ # Application configuration should go into files in config/initializers
13
+ # -- all .rb files in that directory are automatically loaded.
14
+
15
+ # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
16
+ # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
17
+ #config.time_zone = 'Eastern Time (US & Canada)'
18
+
19
+ # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
20
+ # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
21
+ # config.i18n.default_locale = :de
22
+ end
23
+ end
@@ -0,0 +1,4 @@
1
+ # Set up gems listed in the Gemfile.
2
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
3
+
4
+ require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE'])
@@ -0,0 +1,7 @@
1
+ test:
2
+ adapter: sqlite3
3
+ database: ':memory:'
4
+ development:
5
+ adapter: sqlite3
6
+ database: ':memory:'
7
+
@@ -0,0 +1,5 @@
1
+ # Load the rails application
2
+ require File.expand_path('../application', __FILE__)
3
+
4
+ # Initialize the rails application
5
+ Dummy::Application.initialize!
@@ -0,0 +1,25 @@
1
+ Dummy::Application.configure do
2
+ # Settings specified here will take precedence over those in config/application.rb.
3
+
4
+ # In the development environment your application's code is reloaded on
5
+ # every request. This slows down response time but is perfect for development
6
+ # since you don't have to restart the web server when you make code changes.
7
+ config.cache_classes = false
8
+
9
+ # Do not eager load code on boot.
10
+ config.eager_load = false
11
+
12
+ # Show full error reports and disable caching.
13
+ config.consider_all_requests_local = true
14
+ config.action_controller.perform_caching = false
15
+
16
+ # Print deprecation notices to the Rails logger.
17
+ config.active_support.deprecation = :log
18
+
19
+ # Debug mode disables concatenation and preprocessing of assets.
20
+ # This option may cause significant delays in view rendering with a large
21
+ # number of complex assets.
22
+ config.assets.debug = true
23
+
24
+ end
25
+
@@ -0,0 +1,34 @@
1
+ Dummy::Application.configure do
2
+ # Settings specified here will take precedence over those in config/application.rb.
3
+
4
+ # The test environment is used exclusively to run your application's
5
+ # test suite. You never need to work with it otherwise. Remember that
6
+ # your test database is "scratch space" for the test suite and is wiped
7
+ # and recreated between test runs. Don't rely on the data there!
8
+ config.cache_classes = true
9
+
10
+ # Do not eager load code on boot. This avoids loading your whole application
11
+ # just for the purpose of running a single test. If you are using a tool that
12
+ # preloads Rails for running tests, you may have to set it to true.
13
+ config.eager_load = false
14
+
15
+ # Configure static asset server for tests with Cache-Control for performance.
16
+ config.serve_static_assets = true
17
+ config.static_cache_control = "public, max-age=3600"
18
+
19
+ # Show full error reports and disable caching.
20
+ config.consider_all_requests_local = true
21
+ config.action_controller.perform_caching = false
22
+
23
+ # Raise exceptions instead of rendering exception templates.
24
+ config.action_dispatch.show_exceptions = false
25
+
26
+ # Disable request forgery protection in test environment.
27
+ config.action_controller.allow_forgery_protection = false
28
+
29
+ # Print deprecation notices to the stderr.
30
+ config.active_support.deprecation = :stderr
31
+
32
+ # skip validation of locale
33
+ I18n.enforce_available_locales = false
34
+ end
@@ -0,0 +1,7 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # You can add backtrace silencers for libraries that you're using but don't wish to see in your backtraces.
4
+ # Rails.backtrace_cleaner.add_silencer { |line| line =~ /my_noisy_library/ }
5
+
6
+ # You can also remove all the silencers if you're trying to debug a problem that might stem from framework code.
7
+ # Rails.backtrace_cleaner.remove_silencers!
@@ -0,0 +1,10 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # Add new inflection rules using the following format
4
+ # (all these examples are active by default):
5
+ # ActiveSupport::Inflector.inflections do |inflect|
6
+ # inflect.plural /^(ox)$/i, '\1en'
7
+ # inflect.singular /^(ox)en/i, '\1'
8
+ # inflect.irregular 'person', 'people'
9
+ # inflect.uncountable %w( fish sheep )
10
+ # end
@@ -0,0 +1,7 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # Your secret key for verifying the integrity of signed cookies.
4
+ # If you change this key, all old signed cookies will become invalid!
5
+ # Make sure the secret is at least 30 characters and all random,
6
+ # no regular words or you'll be exposed to dictionary attacks.
7
+ Dummy::Application.config.secret_token = '7b0ce915dc4c2ee60581c2769798abb5e4078292ad23670fc8d728953fc13aec19863558873234816b58a54f6a35be58b2b0a26749a7dfddcd2f02ee82d7e94f'
@@ -0,0 +1,8 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ Dummy::Application.config.session_store :cookie_store, :key => '_dummy_session'
4
+
5
+ # Use the database for sessions instead of the cookie-based default,
6
+ # which shouldn't be used to store highly confidential information
7
+ # (create the session table with "rails generate session_migration")
8
+ # Dummy::Application.config.session_store :active_record_store
@@ -0,0 +1,5 @@
1
+ # Sample localization file for English. Add more files in this directory for other locales.
2
+ # See http://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points.
3
+
4
+ en:
5
+ hello: "Hello world"
@@ -0,0 +1,2 @@
1
+ Dummy::Application.routes.draw do
2
+ end
@@ -0,0 +1,9 @@
1
+ FactoryGirl.define do
2
+ factory :oar, class: Band do |b|
3
+ b.name 'OAR'
4
+ end
5
+
6
+ factory :stp, class: Band do |b|
7
+ b.name 'STP'
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ FactoryGirl.define do
2
+ factory :dom, class: User do |u|
3
+ u.name 'Dom'
4
+ end
5
+
6
+ factory :joe, class: User do |u|
7
+ u.name 'Joe'
8
+ end
9
+ end
@@ -0,0 +1,20 @@
1
+ ActiveRecord::Schema.define version: 0 do
2
+
3
+ create_table :stars, force: true do |t|
4
+ t.integer "starable_id", null: false
5
+ t.string "starable_type", null: false
6
+ t.integer "starer_id", null: false
7
+ t.string "starer_type", null: false
8
+ t.datetime "created_at"
9
+ t.datetime "updated_at"
10
+ end
11
+
12
+ create_table :users, force: true do |t|
13
+ t.column :name, :string
14
+ end
15
+
16
+ create_table :bands, force: true do |t|
17
+ t.column :name, :string
18
+ end
19
+
20
+ end
@@ -0,0 +1,10 @@
1
+ # Configure Rails Envinronment
2
+ ENV['RAILS_ENV'] = 'test'
3
+ require File.expand_path('../dummy30/config/environment.rb', __FILE__)
4
+ require 'rails/test_help'
5
+ Turn.config.format = :progress
6
+ FactoryGirl.find_definitions
7
+ ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + '/debug.log')
8
+ ActiveRecord::Migration.verbose = false
9
+ load(File.dirname(__FILE__) + '/schema.rb')
10
+ require File.dirname(__FILE__) + '/../lib/generators/templates/model.rb'
metadata ADDED
@@ -0,0 +1,179 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acts_as_starable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Dominic Giglio
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sqlite3
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rails
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 4.0.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 4.0.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: factory_girl
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: turn
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: acts_as_starable is a gem that allows a model to act as a starer of other
84
+ models or one that can be starred.)
85
+ email:
86
+ - dom@littlstar.com
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - Gemfile
93
+ - LICENSE
94
+ - README.md
95
+ - Rakefile
96
+ - acts_as_starable.gemspec
97
+ - lib/acts_as_starable.rb
98
+ - lib/acts_as_starable/railtie.rb
99
+ - lib/acts_as_starable/star_scopes.rb
100
+ - lib/acts_as_starable/starable.rb
101
+ - lib/acts_as_starable/starer.rb
102
+ - lib/acts_as_starable/starer_lib.rb
103
+ - lib/acts_as_starable/version.rb
104
+ - lib/generators/USAGE
105
+ - lib/generators/acts_as_starable_generator.rb
106
+ - lib/generators/templates/migration.rb
107
+ - lib/generators/templates/model.rb
108
+ - test/acts_as_starable_test.rb
109
+ - test/acts_as_starer_test.rb
110
+ - test/dummy40/Gemfile
111
+ - test/dummy40/Rakefile
112
+ - test/dummy40/app/models/band.rb
113
+ - test/dummy40/app/models/user.rb
114
+ - test/dummy40/config.ru
115
+ - test/dummy40/config/application.rb
116
+ - test/dummy40/config/boot.rb
117
+ - test/dummy40/config/database.yml
118
+ - test/dummy40/config/environment.rb
119
+ - test/dummy40/config/environments/development.rb
120
+ - test/dummy40/config/environments/test.rb
121
+ - test/dummy40/config/initializers/backtrace_silencers.rb
122
+ - test/dummy40/config/initializers/inflections.rb
123
+ - test/dummy40/config/initializers/secret_token.rb
124
+ - test/dummy40/config/initializers/session_store.rb
125
+ - test/dummy40/config/locales/en.yml
126
+ - test/dummy40/config/routes.rb
127
+ - test/factories/bands.rb
128
+ - test/factories/users.rb
129
+ - test/schema.rb
130
+ - test/test_helper.rb
131
+ homepage: https://github.com/littlstar/acts_as_starable
132
+ licenses:
133
+ - MIT
134
+ metadata: {}
135
+ post_install_message:
136
+ rdoc_options: []
137
+ require_paths:
138
+ - lib
139
+ required_ruby_version: !ruby/object:Gem::Requirement
140
+ requirements:
141
+ - - ">="
142
+ - !ruby/object:Gem::Version
143
+ version: '0'
144
+ required_rubygems_version: !ruby/object:Gem::Requirement
145
+ requirements:
146
+ - - ">="
147
+ - !ruby/object:Gem::Version
148
+ version: '0'
149
+ requirements: []
150
+ rubyforge_project: acts_as_starable
151
+ rubygems_version: 2.0.14
152
+ signing_key:
153
+ specification_version: 4
154
+ summary: A Rubygem to add staring functionality to ActiveRecord models
155
+ test_files:
156
+ - test/acts_as_starable_test.rb
157
+ - test/acts_as_starer_test.rb
158
+ - test/dummy40/Gemfile
159
+ - test/dummy40/Rakefile
160
+ - test/dummy40/app/models/band.rb
161
+ - test/dummy40/app/models/user.rb
162
+ - test/dummy40/config.ru
163
+ - test/dummy40/config/application.rb
164
+ - test/dummy40/config/boot.rb
165
+ - test/dummy40/config/database.yml
166
+ - test/dummy40/config/environment.rb
167
+ - test/dummy40/config/environments/development.rb
168
+ - test/dummy40/config/environments/test.rb
169
+ - test/dummy40/config/initializers/backtrace_silencers.rb
170
+ - test/dummy40/config/initializers/inflections.rb
171
+ - test/dummy40/config/initializers/secret_token.rb
172
+ - test/dummy40/config/initializers/session_store.rb
173
+ - test/dummy40/config/locales/en.yml
174
+ - test/dummy40/config/routes.rb
175
+ - test/factories/bands.rb
176
+ - test/factories/users.rb
177
+ - test/schema.rb
178
+ - test/test_helper.rb
179
+ has_rdoc: