acts_as_featureable 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
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
+ .project
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ ## v0.0.1
2
+
3
+ * initial release
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in featureable.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 kainage-home
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.
data/README.md ADDED
@@ -0,0 +1,114 @@
1
+ # Acts As Featureable
2
+
3
+ Add a polymorphic resource to your rails app for pulling content to a main/features page.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'acts_as_featureable'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install acts_as_featureable
20
+
21
+ Run the generator:
22
+
23
+ $ rails g features
24
+
25
+ This will create the migration file and an initializer.
26
+
27
+ Migrate the database:
28
+
29
+ $ rake db:migrate
30
+
31
+ ## Configuration
32
+
33
+ Edit the initializer file to set default settings for feature size and auto title & summary assigning:
34
+
35
+ ## Usage
36
+
37
+ ### Featureable Model
38
+
39
+ Add the appropriate line to the class you want to feature:
40
+
41
+ ```ruby
42
+ class Topic
43
+ acts_as_featurable
44
+ end
45
+ ```
46
+
47
+ ### Creating Features
48
+
49
+ Add a feature to a model. The _title_ and _summary_ (or whichever methods you add in the initializer file) will be assigned:
50
+
51
+ ```ruby
52
+ featureable = Topic.create(title: 'Title', summary: 'Summary')
53
+ featureable.features.create
54
+ #<Feature title: "Title", summary: "Summary">
55
+ ```
56
+
57
+ You can also override them directly:
58
+
59
+ ```ruby
60
+ featureable = Topic.create(title: 'Title', summary: 'Summary')
61
+ featureable.features.create(title: 'My New Title', summary 'My New Summary')
62
+ #<Feature title: "My New Title", summary: "My New Summary">
63
+ ```
64
+
65
+ The _position_ of the features can be specified:
66
+
67
+ ```ruby
68
+ featureable = Topic.create
69
+ featureable.features.create(position: 3)
70
+ #<Feature position: 3>
71
+ ```
72
+
73
+ If it is not specified, it will automatically be assigned the next, lowest _position_:
74
+
75
+ ```ruby
76
+ featureable = Topic.create
77
+ featureable.features.create
78
+ #<Feature position: 1>
79
+
80
+ featureable.features.create
81
+ #<Feature position: 2>
82
+ ```
83
+
84
+ If you try and assign a position which has already been taken, it will find the next, lowest available _position_:
85
+
86
+ ```ruby
87
+ featureable = Topic.create
88
+ featureable.features.create
89
+ #<Feature position: 1>
90
+
91
+ featureable.features.create(position: 1)
92
+ #<Feature position: 2>
93
+
94
+ featureable.features.create(position: 1)
95
+ #<Feature position: 3>
96
+ ```
97
+
98
+ ### Feature scope
99
+
100
+ Get all features ordered by thier _position_ ascending:
101
+
102
+ ```ruby
103
+ Feature.ordered
104
+ ```
105
+
106
+ Implementing the forms and views is left up to the developer.
107
+
108
+ ## Contributing
109
+
110
+ 1. Fork it
111
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
112
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
113
+ 4. Push to the branch (`git push origin my-new-feature`)
114
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'acts_as_featureable/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "acts_as_featureable"
8
+ spec.version = ActsAsFeatureable::VERSION
9
+ spec.authors = ["Kainage"]
10
+ spec.email = ["kainage@gmail.com"]
11
+ spec.description = %q{Add a feature resource to a rails app}
12
+ spec.summary = %q{Creates a new polymorphic resrouce, Feature, for pulling content to a main/features page}
13
+ spec.homepage = "https://github.com/kainage/acts_as_featureable"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "activerecord"
25
+ spec.add_development_dependency "sqlite3"
26
+ spec.add_development_dependency "shoulda-matchers"
27
+
28
+ spec.add_runtime_dependency "activerecord"
29
+ end
@@ -0,0 +1,62 @@
1
+ class Feature < ActiveRecord::Base
2
+ include ActsAsFeatureable::Feature
3
+
4
+ before_validation :assign_title, :assign_summary, :assign_position
5
+
6
+ validate :feature_limit_not_reached
7
+
8
+ validates_numericality_of :position
9
+
10
+ private
11
+
12
+ def feature_limit_not_reached
13
+ limit = ActsAsFeatureable.feature_limit
14
+ errors.add(:base,
15
+ "The feature limit of #{limit} has been reached. \
16
+ Please delete or change an existing feature."
17
+ ) if Feature.count >= limit
18
+ end
19
+
20
+ def assign_title
21
+ # If there is no title given, check the model.
22
+ # Will check for the attributes in ActsAsFeatureable.auto_title_assign_list.
23
+
24
+ unless self.title
25
+ ActsAsFeatureable.auto_title_assign_list.each do |attr|
26
+ if featureable.respond_to?(attr)
27
+ self.title = featureable.send(attr)
28
+ break
29
+ end
30
+ end
31
+ end
32
+ end
33
+
34
+ def assign_summary
35
+ # If there is no summary given, check the model.
36
+ # Will check for the attributes in ActsAsFeatureable.auto_summary_assign_list.
37
+
38
+ unless self.summary
39
+ ActsAsFeatureable.auto_summary_assign_list.each do |attr|
40
+ if featureable.respond_to?(attr)
41
+ self.summary = featureable.send(attr)
42
+ break
43
+ end
44
+ end
45
+ end
46
+ end
47
+
48
+ def assign_position
49
+ # If there is no position given, or the position given is already taken,
50
+ # assign the lowest open position. Otherwise assign it.
51
+ all_positions = Feature.select('features.position').map(&:position).sort
52
+
53
+ if !self.position || (self.position && all_positions.include?(self.position))
54
+ # Find lowest, non-taken position
55
+ (1..ActsAsFeatureable.feature_limit).each do |n|
56
+ next if all_positions.include? n
57
+ self.position = n
58
+ break
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,9 @@
1
+ module ActsAsFeatureable
2
+ module Feature
3
+ def self.included(feature_model)
4
+ feature_model.belongs_to :featureable, polymorphic: true
5
+
6
+ feature_model.scope :ordered, ->{ feature_model.order('position ASC') }
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,17 @@
1
+ module ActiveRecord
2
+ module Acts
3
+ module Featureable
4
+ def self.included(base)
5
+ base.extend ClassMethods
6
+ end
7
+
8
+ module ClassMethods
9
+ def acts_as_featureable
10
+ has_many :features, :as => :featureable, :dependent => :destroy
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
16
+
17
+ ActiveRecord::Base.send(:include, ActiveRecord::Acts::Featureable)
@@ -0,0 +1,3 @@
1
+ module ActsAsFeatureable
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,22 @@
1
+ require 'acts_as_featureable/version'
2
+ require 'acts_as_featureable/featureable_methods'
3
+ require 'acts_as_featureable/feature_methods'
4
+ require 'acts_as_featureable/feature'
5
+
6
+ module ActsAsFeatureable
7
+ # Set the maximum limit of features availible.
8
+ mattr_accessor :feature_limit
9
+ @@feature_limit = 10
10
+
11
+ # Set the order of auto title assign
12
+ mattr_accessor :auto_title_assign_list
13
+ @@auto_title_assign_list = [:title, :name]
14
+
15
+ # Set the order of auto summary assigning
16
+ mattr_accessor :auto_summary_assign_list
17
+ @@auto_summary_assign_list = [:summary, :caption, :tldr, :content, :text]
18
+
19
+ def self.setup
20
+ yield self
21
+ end
22
+ end
@@ -0,0 +1,16 @@
1
+ require 'rails/generators/migration'
2
+
3
+ class FeaturesGenerator < Rails::Generators::Base
4
+ include Rails::Generators::Migration
5
+
6
+ source_root File.expand_path("../templates", __FILE__)
7
+
8
+ def self.next_migration_number(path)
9
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
10
+ end
11
+
12
+ def create_featureable_file
13
+ copy_file "initializer.rb", "config/initializers/acts_as_featureable.rb"
14
+ migration_template "create_features.rb", "db/migrate/create_features.rb"
15
+ end
16
+ end
@@ -0,0 +1,13 @@
1
+ class CreateFeatures < ActiveRecord::Migration
2
+ def change
3
+ create_table :features do |t|
4
+ t.references :featureable, :polymorphic => true
5
+ t.string :title
6
+ t.text :summary
7
+ t.integer :position, :null => false
8
+ t.timestamps
9
+ end
10
+
11
+ add_index :features, [:featureable_type, :featureable_id]
12
+ end
13
+ end
@@ -0,0 +1,10 @@
1
+ ActsAsFeatureable.setup do |config|
2
+ # Set the maximum limit of features availible for your app.
3
+ config.feature_limit = 10
4
+
5
+ # Set the order of auto title assigning.
6
+ config.auto_title_assign_list = [:title, :name]
7
+
8
+ # Set the order of auto summary assigning.
9
+ config.auto_summary_assign_list = [:summary, :caption, :tldr, :content, :text]
10
+ end
@@ -0,0 +1,3 @@
1
+ class Topic < ActiveRecord::Base
2
+ acts_as_featureable
3
+ end
@@ -0,0 +1,16 @@
1
+ ActiveRecord::Schema.define :version => 0 do
2
+ create_table :features do |t|
3
+ t.references :featureable, :polymorphic => true
4
+ t.string :title
5
+ t.text :summary
6
+ t.integer :position, :null => false
7
+ t.timestamps
8
+ end
9
+
10
+ add_index :features, [:featureable_type, :featureable_id]
11
+
12
+ create_table :topics do |t|
13
+ t.string :title
14
+ t.text :summary
15
+ end
16
+ end
@@ -0,0 +1,71 @@
1
+ require 'spec_helper'
2
+
3
+ describe Feature do
4
+ before :each do
5
+ Feature.delete_all
6
+ end
7
+
8
+ subject { Feature.create! }
9
+
10
+ it { should be_valid }
11
+ it { should belong_to :featureable }
12
+ it { should validate_numericality_of :position }
13
+
14
+ ## LIMIT VALIDATION ##
15
+ it "should only allow the default number of features to be made" do
16
+ limit = ActsAsFeatureable.feature_limit
17
+
18
+ # Create the limit of features
19
+ limit.times do
20
+ Feature.create!
21
+ end
22
+
23
+ # Attempt to create an additional feature
24
+ expect { Feature.create! }.to raise_error ActiveRecord::RecordInvalid
25
+ end
26
+
27
+ ## TITLE ##
28
+ it "should assign associated model's title" do
29
+ @topic = Topic.new(:title => 'Title')
30
+ feature = Feature.create!(:featureable => @topic)
31
+
32
+ feature.title.should eql @topic.title
33
+ end
34
+
35
+ it "should overwrite associated model's title" do
36
+ @topic = Topic.new(:title => 'Title')
37
+ feature = Feature.create!(:featureable => @topic, :title => 'New Title')
38
+
39
+ feature.title.should eql 'New Title'
40
+ end
41
+
42
+ ## SUMMARY ##
43
+ it "should assign associated model's summary" do
44
+ @topic = Topic.new(:summary => 'Summary')
45
+ feature = Feature.create!(:featureable => @topic)
46
+
47
+ feature.summary.should eql @topic.summary
48
+ end
49
+
50
+ it "should overwrite associated model's summary" do
51
+ @topic = Topic.new(:summary => 'Summary')
52
+ feature = Feature.create!(:featureable => @topic, :summary => 'New Summary')
53
+
54
+ feature.summary.should eql 'New Summary'
55
+ end
56
+
57
+ ## POSITION ##
58
+ it "should assign a default position" do
59
+ subject.position.should_not be_nil
60
+ end
61
+
62
+ it "should assign a default position of 1 if there are none existing" do
63
+ subject.position.should eql 1
64
+ end
65
+
66
+ it "should assign the next position in line if it already exists" do
67
+ subject
68
+ Feature.create!(:position => 1).position.should eql 2
69
+ end
70
+ end
71
+
@@ -0,0 +1,8 @@
1
+ require 'spec_helper'
2
+
3
+ describe Topic do
4
+ subject { Topic.create! }
5
+
6
+ it { should be_valid }
7
+ it { should have_many :features }
8
+ end
@@ -0,0 +1,11 @@
1
+ require 'active_record'
2
+ require 'shoulda-matchers'
3
+ require 'acts_as_featureable'
4
+
5
+ ActiveRecord::Base.establish_connection(
6
+ :adapter => "sqlite3",
7
+ :database => ":memory:"
8
+ )
9
+
10
+ load(File.dirname(__FILE__) + '/extra/schema.rb')
11
+ load(File.dirname(__FILE__) + '/extra/model.rb')
metadata ADDED
@@ -0,0 +1,191 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acts_as_featureable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Kainage
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-05-13 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: activerecord
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: sqlite3
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: shoulda-matchers
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ - !ruby/object:Gem::Dependency
111
+ name: activerecord
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ description: Add a feature resource to a rails app
127
+ email:
128
+ - kainage@gmail.com
129
+ executables: []
130
+ extensions: []
131
+ extra_rdoc_files: []
132
+ files:
133
+ - .gitignore
134
+ - .rspec
135
+ - CHANGELOG.md
136
+ - Gemfile
137
+ - LICENSE.txt
138
+ - README.md
139
+ - Rakefile
140
+ - acts_as_featureable.gemspec
141
+ - lib/acts_as_featureable.rb
142
+ - lib/acts_as_featureable/feature.rb
143
+ - lib/acts_as_featureable/feature_methods.rb
144
+ - lib/acts_as_featureable/featureable_methods.rb
145
+ - lib/acts_as_featureable/version.rb
146
+ - lib/generators/features_generator.rb
147
+ - lib/generators/templates/create_features.rb
148
+ - lib/generators/templates/initializer.rb
149
+ - spec/extra/model.rb
150
+ - spec/extra/schema.rb
151
+ - spec/feature_spec.rb
152
+ - spec/featureable_spec.rb
153
+ - spec/spec_helper.rb
154
+ homepage: https://github.com/kainage/acts_as_featureable
155
+ licenses:
156
+ - MIT
157
+ post_install_message:
158
+ rdoc_options: []
159
+ require_paths:
160
+ - lib
161
+ required_ruby_version: !ruby/object:Gem::Requirement
162
+ none: false
163
+ requirements:
164
+ - - ! '>='
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ segments:
168
+ - 0
169
+ hash: 548961575401388843
170
+ required_rubygems_version: !ruby/object:Gem::Requirement
171
+ none: false
172
+ requirements:
173
+ - - ! '>='
174
+ - !ruby/object:Gem::Version
175
+ version: '0'
176
+ segments:
177
+ - 0
178
+ hash: 548961575401388843
179
+ requirements: []
180
+ rubyforge_project:
181
+ rubygems_version: 1.8.24
182
+ signing_key:
183
+ specification_version: 3
184
+ summary: Creates a new polymorphic resrouce, Feature, for pulling content to a main/features
185
+ page
186
+ test_files:
187
+ - spec/extra/model.rb
188
+ - spec/extra/schema.rb
189
+ - spec/feature_spec.rb
190
+ - spec/featureable_spec.rb
191
+ - spec/spec_helper.rb