has_default 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README.markdown +81 -0
- data/Rakefile +38 -0
- data/VERSION +1 -0
- data/init.rb +3 -0
- data/lib/has_default.rb +40 -0
- data/spec/default.rb +65 -0
- data/tasks/acts_as_default_tasks.rake +4 -0
- metadata +61 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 [name of plugin creator]
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.markdown
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
# HasDefault
|
2
|
+
|
3
|
+
Specify a "default" instance that is to be returned for an ActiveRecord model when Model.default is called.
|
4
|
+
|
5
|
+
# Installation
|
6
|
+
|
7
|
+
Install the has_default gem
|
8
|
+
|
9
|
+
gem install bradgessler-has_default
|
10
|
+
|
11
|
+
Then configure in rails
|
12
|
+
|
13
|
+
config.gem 'bradgessler-has_default',
|
14
|
+
:lib => 'has_default',
|
15
|
+
:source => 'http://gems.github.com'
|
16
|
+
|
17
|
+
For whatever model you need to set a default for, run the a similar migration
|
18
|
+
|
19
|
+
class AddDefaultsToModels < ActiveRecord::Migration
|
20
|
+
def self.up
|
21
|
+
change_table :plans do |t|
|
22
|
+
t.boolean :default
|
23
|
+
end
|
24
|
+
add_index :plans, :default, :unique => true
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.down
|
28
|
+
remove_index :plans, :default
|
29
|
+
change_table :plans do |t|
|
30
|
+
t.remove :default
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
# Example
|
36
|
+
|
37
|
+
class Plan
|
38
|
+
has_many :accounts
|
39
|
+
has_default
|
40
|
+
end
|
41
|
+
|
42
|
+
class Account
|
43
|
+
belongs_to :plan
|
44
|
+
|
45
|
+
before_validate :set_default_plan, :unless => Proc.new{|a| a.plan.present? }
|
46
|
+
|
47
|
+
protected
|
48
|
+
def set_default_plan
|
49
|
+
self.plan = Plan.default
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
Plan.create(:name => 'Free', :default => true)
|
54
|
+
Plan.create(:name => 'Medium')
|
55
|
+
Plan.create(:name => 'High')
|
56
|
+
|
57
|
+
Account.new.plan.name # => 'Free'
|
58
|
+
Plan.find_by_name('High').update_attribute(:default, true)
|
59
|
+
Account.new.plan.name # => 'High'
|
60
|
+
|
61
|
+
# License
|
62
|
+
|
63
|
+
Copyright (c) 2009 Brad Gessler, released under the MIT license
|
64
|
+
|
65
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
66
|
+
of this software and associated documentation files (the "Software"), to deal
|
67
|
+
in the Software without restriction, including without limitation the rights
|
68
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
69
|
+
copies of the Software, and to permit persons to whom the Software is
|
70
|
+
furnished to do so, subject to the following conditions:
|
71
|
+
|
72
|
+
The above copyright notice and this permission notice shall be included in
|
73
|
+
all copies or substantial portions of the Software.
|
74
|
+
|
75
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
76
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
77
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
78
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
79
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
80
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
81
|
+
THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
|
5
|
+
desc 'Default: run unit tests.'
|
6
|
+
task :default => :spec
|
7
|
+
|
8
|
+
require 'rake'
|
9
|
+
require 'spec/rake/spectask'
|
10
|
+
|
11
|
+
desc "Run all examples"
|
12
|
+
Spec::Rake::SpecTask.new('spec') do |t|
|
13
|
+
t.spec_files = FileList['spec/*.rb']
|
14
|
+
t.fail_on_error = false
|
15
|
+
end
|
16
|
+
|
17
|
+
desc 'Generate documentation for the has_default plugin.'
|
18
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
19
|
+
rdoc.rdoc_dir = 'rdoc'
|
20
|
+
rdoc.title = 'HasDefault'
|
21
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
22
|
+
rdoc.rdoc_files.include('README')
|
23
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
24
|
+
end
|
25
|
+
|
26
|
+
begin
|
27
|
+
require 'jeweler'
|
28
|
+
Jeweler::Tasks.new do |gemspec|
|
29
|
+
gemspec.name = "has_default"
|
30
|
+
gemspec.summary = "Specify a default instance to be returned for an ActiveRecord model"
|
31
|
+
gemspec.description = ""
|
32
|
+
gemspec.email = "brad@conden.se"
|
33
|
+
gemspec.homepage = "http://github.com/bradgessler/has_default"
|
34
|
+
gemspec.authors = ["Brad Gessler"]
|
35
|
+
end
|
36
|
+
rescue LoadError
|
37
|
+
puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
|
38
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.5.0
|
data/init.rb
ADDED
data/lib/has_default.rb
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
module HasDefault
|
2
|
+
module ActiveRecord
|
3
|
+
def self.included(base)
|
4
|
+
base.send :extend, ActMacro
|
5
|
+
end
|
6
|
+
|
7
|
+
module ActMacro
|
8
|
+
def has_default(opts={})
|
9
|
+
write_inheritable_attribute :default_column, opts[:column] || 'default'
|
10
|
+
|
11
|
+
class_inheritable_reader :default_column
|
12
|
+
|
13
|
+
self.send :extend, ClassMethods
|
14
|
+
self.send :include, HasDefault::ActiveRecord::Callbacks
|
15
|
+
self.send :include, HasDefault::ActiveRecord::InstanceMethods
|
16
|
+
|
17
|
+
after_save :clear_default_scope
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
module ClassMethods
|
22
|
+
def default
|
23
|
+
first(:conditions => [ "`#{default_column}` = ?", true ]) || first
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
module Callbacks
|
28
|
+
private
|
29
|
+
# Clears default on everything except for the model being saved if its set to be the new default
|
30
|
+
def clear_default_scope
|
31
|
+
if self.send(self.class.default_column)
|
32
|
+
self.class.update_all([ "`#{self.class.default_column}` = ?", nil ], [ "#{self.class.primary_key} <> ? ", self.send(self.class.primary_key)])
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
module InstanceMethods
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
data/spec/default.rb
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'spec'
|
3
|
+
|
4
|
+
gem 'activerecord', '>= 1.15.4.7794'
|
5
|
+
require 'active_record'
|
6
|
+
|
7
|
+
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
|
8
|
+
|
9
|
+
require "#{File.dirname(__FILE__)}/../init"
|
10
|
+
|
11
|
+
def setup_db
|
12
|
+
ActiveRecord::Schema.define(:version => 1) do
|
13
|
+
create_table :drinks do |t|
|
14
|
+
t.column :name, :string
|
15
|
+
t.column :default, :boolean
|
16
|
+
t.column :created_at, :datetime
|
17
|
+
t.column :updated_at, :datetime
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def teardown_db
|
23
|
+
ActiveRecord::Base.connection.tables.each do |table|
|
24
|
+
ActiveRecord::Base.connection.drop_table(table)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
class Drink < ActiveRecord::Base
|
29
|
+
has_default
|
30
|
+
end
|
31
|
+
|
32
|
+
describe "has_default" do
|
33
|
+
before(:each) do
|
34
|
+
setup_db
|
35
|
+
Drink.create([
|
36
|
+
{:name => 'Small'},
|
37
|
+
{:name => 'Medium'},
|
38
|
+
{:name => 'Large'}
|
39
|
+
])
|
40
|
+
end
|
41
|
+
|
42
|
+
after(:each) do
|
43
|
+
teardown_db
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should have default" do
|
47
|
+
Drink.should respond_to(:default)
|
48
|
+
end
|
49
|
+
|
50
|
+
it "should give first record if no default" do
|
51
|
+
Drink.default.should eql(Drink.first)
|
52
|
+
end
|
53
|
+
|
54
|
+
it "should set default" do
|
55
|
+
Drink.last.update_attribute(:default, true)
|
56
|
+
Drink.last.should be_default
|
57
|
+
end
|
58
|
+
|
59
|
+
it "should only allow one default" do
|
60
|
+
Drink.last.update_attribute(:default, true)
|
61
|
+
Drink.first.update_attribute(:default, true)
|
62
|
+
Drink.all(:conditions => {:default => true}).should have(1).item
|
63
|
+
Drink.first.should be_default
|
64
|
+
end
|
65
|
+
end
|
metadata
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: has_default
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.5.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Brad Gessler
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-10-06 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: ""
|
17
|
+
email: brad@conden.se
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README.markdown
|
24
|
+
files:
|
25
|
+
- MIT-LICENSE
|
26
|
+
- README.markdown
|
27
|
+
- Rakefile
|
28
|
+
- VERSION
|
29
|
+
- init.rb
|
30
|
+
- lib/has_default.rb
|
31
|
+
- tasks/acts_as_default_tasks.rake
|
32
|
+
has_rdoc: true
|
33
|
+
homepage: http://github.com/bradgessler/has_default
|
34
|
+
licenses: []
|
35
|
+
|
36
|
+
post_install_message:
|
37
|
+
rdoc_options:
|
38
|
+
- --charset=UTF-8
|
39
|
+
require_paths:
|
40
|
+
- lib
|
41
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: "0"
|
46
|
+
version:
|
47
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: "0"
|
52
|
+
version:
|
53
|
+
requirements: []
|
54
|
+
|
55
|
+
rubyforge_project:
|
56
|
+
rubygems_version: 1.3.5
|
57
|
+
signing_key:
|
58
|
+
specification_version: 3
|
59
|
+
summary: Specify a default instance to be returned for an ActiveRecord model
|
60
|
+
test_files:
|
61
|
+
- spec/default.rb
|