matthewtodd-default_association_attributes 0.1.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/MIT-LICENSE +20 -0
- data/README.rdoc +31 -0
- data/lib/default_association_attributes.rb +13 -0
- data/test/default_association_attributes_test.rb +31 -0
- data/test/test_helper.rb +28 -0
- metadata +63 -0
data/MIT-LICENSE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Copyright (c) 2009 Matthew Todd
|
|
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.rdoc
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
=DefaultAssociationAttributes
|
|
2
|
+
|
|
3
|
+
Because sometimes all you want to say is
|
|
4
|
+
|
|
5
|
+
@subscriber.invitations.build
|
|
6
|
+
|
|
7
|
+
For more motivation, see the blog post, http://matthewtodd.org/2009/03/02/default-association-attributes-in-active_record.html
|
|
8
|
+
|
|
9
|
+
==Installation
|
|
10
|
+
|
|
11
|
+
As a gem:
|
|
12
|
+
|
|
13
|
+
config.gem 'matthewtodd-default_association_attributes', :lib => 'default_association_attributes', :source => 'http://gems.github.com'
|
|
14
|
+
|
|
15
|
+
As a plugin:
|
|
16
|
+
|
|
17
|
+
script/plugin install git://github.com/matthewtodd/default_association_attributes.git
|
|
18
|
+
|
|
19
|
+
==Usage
|
|
20
|
+
|
|
21
|
+
In your model:
|
|
22
|
+
|
|
23
|
+
class Subscriber < ActiveRecord::Base
|
|
24
|
+
has_many :invitations, :extend => DefaultAssociationAttributes do
|
|
25
|
+
def default_attributes
|
|
26
|
+
{ :email => proxy_owner.email }
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
Copyright (c) 2009 Matthew Todd, released under the MIT license
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
module DefaultAssociationAttributes
|
|
2
|
+
def build_record(attrs, &block)
|
|
3
|
+
super(with_default_attributes(attrs), &block)
|
|
4
|
+
end
|
|
5
|
+
|
|
6
|
+
def create_record(attrs, &block)
|
|
7
|
+
super(with_default_attributes(attrs), &block)
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def with_default_attributes(attrs)
|
|
11
|
+
default_attributes.merge((attrs || {}).symbolize_keys)
|
|
12
|
+
end
|
|
13
|
+
end
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'test_helper')
|
|
2
|
+
|
|
3
|
+
class DefaultAssociationAttributesTest < Test::Unit::TestCase
|
|
4
|
+
context 'given an existing Subscriber' do
|
|
5
|
+
setup { @subscriber = Subscriber.create(:email => 'developer@example.com') }
|
|
6
|
+
|
|
7
|
+
[:build, :create].each do |method|
|
|
8
|
+
context "#{method} Invitation" do
|
|
9
|
+
setup { @invitation = @subscriber.invitations.send(method) }
|
|
10
|
+
|
|
11
|
+
should 'set email to the Subscriber email' do
|
|
12
|
+
assert_equal(@subscriber.email, @invitation.email)
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
should "not set email on #{method} Invitation with a specific email address as a symbol" do
|
|
17
|
+
invitation = @subscriber.invitations.send(method, :email => 'someone.else@example.com')
|
|
18
|
+
assert_equal('someone.else@example.com', invitation.email)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
should "not set email on #{method} Invitation with a specific email address as a string" do
|
|
22
|
+
invitation = @subscriber.invitations.send(method, 'email' => 'someone.else@example.com')
|
|
23
|
+
assert_equal('someone.else@example.com', invitation.email)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
should "not blow up on #{method} Invitation with nil parameters" do
|
|
27
|
+
assert_nothing_raised { @subscriber.invitations.send(method, nil) }
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
data/test/test_helper.rb
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
require 'rubygems'
|
|
2
|
+
require 'test/unit'
|
|
3
|
+
require 'shoulda'
|
|
4
|
+
require 'mocha'
|
|
5
|
+
require 'active_record'
|
|
6
|
+
require File.join(File.dirname(__FILE__), '..', 'lib', 'default_association_attributes.rb')
|
|
7
|
+
|
|
8
|
+
ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
|
|
9
|
+
|
|
10
|
+
ActiveRecord::Base.connection.create_table :subscribers, :force => true do |table|
|
|
11
|
+
table.string :email
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
ActiveRecord::Base.connection.create_table :invitations, :force => true do |table|
|
|
15
|
+
table.references :source, :polymorphic => true
|
|
16
|
+
table.string :email
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
class Subscriber < ActiveRecord::Base
|
|
20
|
+
has_many :invitations, :as => :source, :extend => DefaultAssociationAttributes do
|
|
21
|
+
def default_attributes
|
|
22
|
+
{ :email => proxy_owner.email }
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
class Invitation < ActiveRecord::Base
|
|
28
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: matthewtodd-default_association_attributes
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Matthew Todd
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
|
|
12
|
+
date: 2009-03-02 00:00:00 -08:00
|
|
13
|
+
default_executable:
|
|
14
|
+
dependencies: []
|
|
15
|
+
|
|
16
|
+
description:
|
|
17
|
+
email: matthew.todd@gmail.com
|
|
18
|
+
executables: []
|
|
19
|
+
|
|
20
|
+
extensions: []
|
|
21
|
+
|
|
22
|
+
extra_rdoc_files:
|
|
23
|
+
- README.rdoc
|
|
24
|
+
files:
|
|
25
|
+
- README.rdoc
|
|
26
|
+
- MIT-LICENSE
|
|
27
|
+
- init.rb
|
|
28
|
+
- lib/default_association_attributes.rb
|
|
29
|
+
- test/default_association_attributes_test.rb
|
|
30
|
+
- test/test_helper.rb
|
|
31
|
+
has_rdoc: true
|
|
32
|
+
homepage:
|
|
33
|
+
post_install_message:
|
|
34
|
+
rdoc_options:
|
|
35
|
+
- --main
|
|
36
|
+
- README.rdoc
|
|
37
|
+
- --title
|
|
38
|
+
- default_association_attributes-0.1.0
|
|
39
|
+
- --inline-source
|
|
40
|
+
- --line-numbers
|
|
41
|
+
require_paths:
|
|
42
|
+
- lib
|
|
43
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - ">="
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: "0"
|
|
48
|
+
version:
|
|
49
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
50
|
+
requirements:
|
|
51
|
+
- - ">="
|
|
52
|
+
- !ruby/object:Gem::Version
|
|
53
|
+
version: "0"
|
|
54
|
+
version:
|
|
55
|
+
requirements: []
|
|
56
|
+
|
|
57
|
+
rubyforge_project:
|
|
58
|
+
rubygems_version: 1.2.0
|
|
59
|
+
signing_key:
|
|
60
|
+
specification_version: 2
|
|
61
|
+
summary: ""
|
|
62
|
+
test_files: []
|
|
63
|
+
|