flavors 0.2.0

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
+ log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in flavors.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Polydice, Inc.
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,68 @@
1
+ # Flavors
2
+
3
+ Simple and flexible preferences integration for ActiveRecord models.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'flavors'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install flavors
18
+
19
+ ## Usage
20
+
21
+ To add preferences to your ActiveRecord model, in your model file:
22
+
23
+ ```
24
+ class User < ActiveRecord::Base
25
+ preference :receive_email, :default => true
26
+ end
27
+ ```
28
+
29
+ Then you can then use the methods provided by flavors to read / write preferences.
30
+
31
+ ```
32
+ Loading development environment (Rails 3.2.12)
33
+ irb(main):001:0> u = User.create(:email => "foo@bar.com")
34
+ irb(main):002:0> u.notification
35
+ => true
36
+ irb(main):003:0> u.notification = false
37
+ => false
38
+ irb(main):004:0> u.notification
39
+ => false
40
+ ```
41
+
42
+ From 0.2.0, Flavors also supports callback block for preference setter.
43
+
44
+ ```
45
+ class User < ActiveRecord::Base
46
+ preference :receive_email, :default => true do |object, value|
47
+ puts "#{object.name} sets preference to #{value}"
48
+ end
49
+ end
50
+ ```
51
+
52
+ When you set preference of instances, the callback block will be invoked.
53
+
54
+ ```
55
+ Loading development environment (Rails 3.2.12)
56
+ irb(main):001:0> u = User.create(:name => "foo", :email => "foo@bar.com")
57
+ irb(main):002:0> u.notification = true
58
+ foo sets preference to true
59
+ => true
60
+ ```
61
+
62
+ ## Contributing
63
+
64
+ 1. Fork it
65
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
66
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
67
+ 4. Push to the branch (`git push origin my-new-feature`)
68
+ 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
data/flavors.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'flavors/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "flavors"
8
+ gem.version = Flavors::VERSION
9
+ gem.authors = ["David Yun", "Richard Lee"]
10
+ gem.email = ["david.yun@polydice.com", "rl@polydice.com"]
11
+ gem.description = %q{Simple and flexible preferences integration for ActiveRecord models.}
12
+ gem.summary = %q{Add preferences to ActiveRecord models.}
13
+ gem.homepage = "https://github.com/polydice/flavors"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency "railties", "~> 3.1"
21
+ gem.add_dependency "activesupport", "~> 3.2"
22
+ gem.add_dependency "activerecord", "~> 3.2"
23
+
24
+ gem.add_development_dependency "rspec", "~> 2.12.0"
25
+ gem.add_development_dependency "rake", "~> 10.0"
26
+ gem.add_development_dependency "shoulda-matchers", "~> 1.4"
27
+ gem.add_development_dependency "sqlite3"
28
+ end
@@ -0,0 +1,12 @@
1
+ require 'flavors/preferences/preferences.rb'
2
+ require 'flavors/preferences/preference.rb'
3
+
4
+ require 'rails'
5
+
6
+ module Flavors
7
+ class Engine < ::Rails::Engine
8
+ initializer "flavors" do
9
+ ::ActiveRecord::Base.send :include, Flavors::Preferences
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,7 @@
1
+ require 'active_record'
2
+
3
+ module Flavors
4
+ class Preference < ::ActiveRecord::Base
5
+ belongs_to :prefered, :polymorphic => true
6
+ end
7
+ end
@@ -0,0 +1,37 @@
1
+ require 'active_support/concern'
2
+
3
+ module Flavors
4
+ module Preferences
5
+ extend ::ActiveSupport::Concern
6
+
7
+ module ClassMethods
8
+ def preference(name, options = {}, &callback)
9
+ has_many :preferences, :as => :prefered, :class_name => "::Flavors::Preference"
10
+
11
+ define_method(name) do
12
+ read_preference(name, options[:default])
13
+ end
14
+
15
+ define_method("#{name}=") do |value|
16
+ write_preference(name, value)
17
+ callback.call(self, value) if callback
18
+ end
19
+ end
20
+ end
21
+
22
+ def read_preference(name, default = nil)
23
+ if p = self.preferences.where(:name => name).first
24
+ p.value
25
+ elsif default.present?
26
+ default
27
+ else
28
+ nil
29
+ end
30
+ end
31
+
32
+ def write_preference(name, value)
33
+ p = self.preferences.find_or_create_by_name(name)
34
+ p.update_attribute(:value, value)
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,3 @@
1
+ module Flavors
2
+ VERSION = "0.2.0"
3
+ end
data/lib/flavors.rb ADDED
@@ -0,0 +1,7 @@
1
+ require "flavors/version"
2
+
3
+ module Flavors
4
+ module Rails
5
+ require "flavors/engine"
6
+ end
7
+ end
@@ -0,0 +1,22 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/migration'
3
+
4
+ module Flavors
5
+ class MigrationGenerator < ::Rails::Generators::Base
6
+ include ::Rails::Generators::Migration
7
+ source_root File.expand_path('../templates', __FILE__)
8
+
9
+ def self.next_migration_number( dirname )
10
+ next_migration_number = current_migration_number(dirname) + 1
11
+ if ActiveRecord::Base.timestamped_migrations
12
+ [Time.now.utc.strftime("%Y%m%d%H%M%S%6N"), "%.20d" % next_migration_number].max
13
+ else
14
+ "%.3d" % next_migration_number
15
+ end
16
+ end
17
+
18
+ def create_model_file
19
+ migration_template "create_preferences.rb", "db/migrate/create_preferences.rb"
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,16 @@
1
+ class CreatePreferences < ActiveRecord::Migration
2
+ def change
3
+ create_table :preferences do |t|
4
+ t.string :name
5
+ t.boolean :value
6
+ t.integer :prefered_id
7
+ t.string :prefered_type
8
+
9
+ t.timestamps
10
+ end
11
+
12
+ add_index :preferences, :prefered_id
13
+ add_index :preferences, :prefered_type
14
+ add_index :preferences, :name
15
+ end
16
+ end
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe Flavors::Preference do
4
+
5
+ it { should belong_to(:prefered) }
6
+
7
+ end
@@ -0,0 +1,60 @@
1
+ require 'spec_helper'
2
+ require 'flavors'
3
+
4
+ describe Flavors::Preferences do
5
+ before do
6
+ User.class_eval do
7
+ preference :notification, :default => true
8
+ end
9
+
10
+ Post.class_eval do
11
+ preference :sticky, :default => false
12
+ end
13
+ end
14
+
15
+ subject { User.create }
16
+
17
+ it "should have a default value" do
18
+ subject.notification.should == true
19
+ end
20
+
21
+ it "should update preference" do
22
+ subject.notification = false
23
+ subject.notification.should == false
24
+ end
25
+
26
+ it "should scope preferences for different classes" do
27
+ expect {
28
+ subject.sticky
29
+ }.to raise_error NoMethodError
30
+ end
31
+
32
+ it "should scope default value for different classes" do
33
+ Post.class_eval do
34
+ preference :notification, :default => false
35
+ end
36
+
37
+ subject.notification.should be_true
38
+ end
39
+
40
+ it "should return if nil if no default value defined" do
41
+ User.class_eval do
42
+ preference :foo
43
+ end
44
+
45
+ subject.foo.should be_nil
46
+ end
47
+
48
+ it "should invoke callback block" do
49
+ User.class_eval do
50
+ def buz; end
51
+
52
+ preference :bar do |object, value|
53
+ object.buz
54
+ end
55
+ end
56
+
57
+ subject.should_receive(:buz)
58
+ subject.bar = true
59
+ end
60
+ end
@@ -0,0 +1,28 @@
1
+ require 'rspec'
2
+ require 'flavors'
3
+ require 'shoulda-matchers'
4
+
5
+ require "rails/test_unit/railtie"
6
+
7
+ RSpec.configure do |config|
8
+ config.color_enabled = true
9
+ config.formatter = 'documentation'
10
+ end
11
+
12
+ # Define the application and configuration
13
+ module Config
14
+ class Application < ::Rails::Application
15
+ end
16
+ end
17
+
18
+ Config::Application.initialize!
19
+
20
+ # Setup test environment for ActiveRecord
21
+ ActiveRecord::Base.establish_connection(
22
+ :adapter => 'sqlite3',
23
+ :database => ':memory:'
24
+ )
25
+
26
+ load File.dirname(__FILE__) + '/support/schema.rb'
27
+ load File.dirname(__FILE__) + '/support/user.rb'
28
+ load File.dirname(__FILE__) + '/support/post.rb'
@@ -0,0 +1,3 @@
1
+ class Post < ActiveRecord::Base
2
+
3
+ end
@@ -0,0 +1,42 @@
1
+ # This file is auto-generated from the current state of the database. Instead
2
+ # of editing this file, please use the migrations feature of Active Record to
3
+ # incrementally modify your database, and then regenerate this schema definition.
4
+ #
5
+ # Note that this schema.rb definition is the authoritative source for your
6
+ # database schema. If you need to create the application database on another
7
+ # system, you should be using db:schema:load, not running all the migrations
8
+ # from scratch. The latter is a flawed and unsustainable approach (the more migrations
9
+ # you'll amass, the slower it'll run and the greater likelihood for issues).
10
+ #
11
+ # It's strongly recommended to check this file into your version control system.
12
+
13
+ ActiveRecord::Schema.define(:version => 20130103134512441314) do
14
+
15
+ create_table "preferences", :force => true do |t|
16
+ t.string "name"
17
+ t.boolean "value"
18
+ t.integer "prefered_id"
19
+ t.string "prefered_type"
20
+ t.datetime "created_at", :null => false
21
+ t.datetime "updated_at", :null => false
22
+ end
23
+
24
+ add_index "preferences", ["name"], :name => "index_preferences_on_name"
25
+ add_index "preferences", ["prefered_id"], :name => "index_preferences_on_prefered_id"
26
+ add_index "preferences", ["prefered_type"], :name => "index_preferences_on_prefered_type"
27
+
28
+ create_table "users", :force => true do |t|
29
+ t.string "email"
30
+ t.string "username"
31
+ t.datetime "created_at", :null => false
32
+ t.datetime "updated_at", :null => false
33
+ end
34
+
35
+ create_table "posts", :force => true do |t|
36
+ t.string "title"
37
+ t.text "body"
38
+ t.datetime "created_at", :null => false
39
+ t.datetime "updated_at", :null => false
40
+ end
41
+
42
+ end
@@ -0,0 +1,3 @@
1
+ class User < ActiveRecord::Base
2
+
3
+ end
metadata ADDED
@@ -0,0 +1,190 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: flavors
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - David Yun
9
+ - Richard Lee
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2013-02-21 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: railties
17
+ requirement: !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ~>
21
+ - !ruby/object:Gem::Version
22
+ version: '3.1'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ~>
29
+ - !ruby/object:Gem::Version
30
+ version: '3.1'
31
+ - !ruby/object:Gem::Dependency
32
+ name: activesupport
33
+ requirement: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ~>
37
+ - !ruby/object:Gem::Version
38
+ version: '3.2'
39
+ type: :runtime
40
+ prerelease: false
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ~>
45
+ - !ruby/object:Gem::Version
46
+ version: '3.2'
47
+ - !ruby/object:Gem::Dependency
48
+ name: activerecord
49
+ requirement: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '3.2'
55
+ type: :runtime
56
+ prerelease: false
57
+ version_requirements: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ~>
61
+ - !ruby/object:Gem::Version
62
+ version: '3.2'
63
+ - !ruby/object:Gem::Dependency
64
+ name: rspec
65
+ requirement: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ~>
69
+ - !ruby/object:Gem::Version
70
+ version: 2.12.0
71
+ type: :development
72
+ prerelease: false
73
+ version_requirements: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ~>
77
+ - !ruby/object:Gem::Version
78
+ version: 2.12.0
79
+ - !ruby/object:Gem::Dependency
80
+ name: rake
81
+ requirement: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ~>
85
+ - !ruby/object:Gem::Version
86
+ version: '10.0'
87
+ type: :development
88
+ prerelease: false
89
+ version_requirements: !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ~>
93
+ - !ruby/object:Gem::Version
94
+ version: '10.0'
95
+ - !ruby/object:Gem::Dependency
96
+ name: shoulda-matchers
97
+ requirement: !ruby/object:Gem::Requirement
98
+ none: false
99
+ requirements:
100
+ - - ~>
101
+ - !ruby/object:Gem::Version
102
+ version: '1.4'
103
+ type: :development
104
+ prerelease: false
105
+ version_requirements: !ruby/object:Gem::Requirement
106
+ none: false
107
+ requirements:
108
+ - - ~>
109
+ - !ruby/object:Gem::Version
110
+ version: '1.4'
111
+ - !ruby/object:Gem::Dependency
112
+ name: sqlite3
113
+ requirement: !ruby/object:Gem::Requirement
114
+ none: false
115
+ requirements:
116
+ - - ! '>='
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ type: :development
120
+ prerelease: false
121
+ version_requirements: !ruby/object:Gem::Requirement
122
+ none: false
123
+ requirements:
124
+ - - ! '>='
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ description: Simple and flexible preferences integration for ActiveRecord models.
128
+ email:
129
+ - david.yun@polydice.com
130
+ - rl@polydice.com
131
+ executables: []
132
+ extensions: []
133
+ extra_rdoc_files: []
134
+ files:
135
+ - .gitignore
136
+ - Gemfile
137
+ - LICENSE.txt
138
+ - README.md
139
+ - Rakefile
140
+ - flavors.gemspec
141
+ - lib/flavors.rb
142
+ - lib/flavors/engine.rb
143
+ - lib/flavors/preferences/preference.rb
144
+ - lib/flavors/preferences/preferences.rb
145
+ - lib/flavors/version.rb
146
+ - lib/generators/flavors/migration_generator.rb
147
+ - lib/generators/flavors/templates/create_preferences.rb
148
+ - spec/preference_spec.rb
149
+ - spec/preferences_spec.rb
150
+ - spec/spec_helper.rb
151
+ - spec/support/post.rb
152
+ - spec/support/schema.rb
153
+ - spec/support/user.rb
154
+ homepage: https://github.com/polydice/flavors
155
+ licenses: []
156
+ post_install_message:
157
+ rdoc_options: []
158
+ require_paths:
159
+ - lib
160
+ required_ruby_version: !ruby/object:Gem::Requirement
161
+ none: false
162
+ requirements:
163
+ - - ! '>='
164
+ - !ruby/object:Gem::Version
165
+ version: '0'
166
+ segments:
167
+ - 0
168
+ hash: -1685997534619687224
169
+ required_rubygems_version: !ruby/object:Gem::Requirement
170
+ none: false
171
+ requirements:
172
+ - - ! '>='
173
+ - !ruby/object:Gem::Version
174
+ version: '0'
175
+ segments:
176
+ - 0
177
+ hash: -1685997534619687224
178
+ requirements: []
179
+ rubyforge_project:
180
+ rubygems_version: 1.8.23
181
+ signing_key:
182
+ specification_version: 3
183
+ summary: Add preferences to ActiveRecord models.
184
+ test_files:
185
+ - spec/preference_spec.rb
186
+ - spec/preferences_spec.rb
187
+ - spec/spec_helper.rb
188
+ - spec/support/post.rb
189
+ - spec/support/schema.rb
190
+ - spec/support/user.rb