acts_as 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +13 -0
- data/LICENSE.txt +22 -0
- data/README.md +95 -0
- data/Rakefile +1 -0
- data/acts_as.gemspec +23 -0
- data/lib/acts_as/version.rb +3 -0
- data/lib/acts_as.rb +77 -0
- data/spec/acts_for_spec.rb +80 -0
- data/spec/spec_helper.rb +9 -0
- data/spec/support/active_record.rb +31 -0
- metadata +86 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ff4949b83af1313de02dd223cfa667b8914d03c2
|
4
|
+
data.tar.gz: 950527f10b751bcbeeda69c7816a51924a1de69a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b024ee340ef12fa6bc5aaa3380de1993ba5fbc349fec88f2a93f39632a0c39ca7a030da2905f45eae69764d4269bca0db46901ceaf18881a4e9832bc2f7ece72
|
7
|
+
data.tar.gz: 186fc903daca73350d57ab4a30ecfd2454a4a567153735a437f478033d892d0ed524b7f4fc62a9508a50bb746c4a340e3f1ada2068bfb5ccdad2d53ec497bfc4
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 winfred
|
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,95 @@
|
|
1
|
+
# ActsAs
|
2
|
+
|
3
|
+
ActiveRecord extension for easy 1:1 composition delegation
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'acts_as'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install acts_as
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
|
23
|
+
# table :users
|
24
|
+
# name :string
|
25
|
+
#
|
26
|
+
class User
|
27
|
+
include ActsAs
|
28
|
+
end
|
29
|
+
|
30
|
+
class Rebel < User
|
31
|
+
has_one :profile, class_name: 'RebelProfile', autosave: true
|
32
|
+
belongs_to :clan, autosave: true
|
33
|
+
acts_as :profile
|
34
|
+
acts_as :clan, prefix: %w( name ), whitelist: %w( delegate_at_will )
|
35
|
+
end
|
36
|
+
|
37
|
+
# table :clans
|
38
|
+
# name :string
|
39
|
+
# strength :integer
|
40
|
+
# cool :boolean
|
41
|
+
#
|
42
|
+
class Clan < ActiveRecord::Base
|
43
|
+
has_many :rebels
|
44
|
+
|
45
|
+
def delegate_at_will
|
46
|
+
'10'
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
# table :rebel_profiles
|
51
|
+
# rebel_id :integer
|
52
|
+
# serial_data :string
|
53
|
+
#
|
54
|
+
class RebelProfile < ActiveRecord::Base
|
55
|
+
belongs_to :rebel
|
56
|
+
end
|
57
|
+
|
58
|
+
```
|
59
|
+
|
60
|
+
Now a whole slew of methods related to ActiveRecord attributes are available for the fields being delegated to another table
|
61
|
+
|
62
|
+
# Fully Proxied Setters/Getters
|
63
|
+
rebel.strength = 10
|
64
|
+
rebel.clan.strength = 20
|
65
|
+
rebel.strength #=> 20
|
66
|
+
|
67
|
+
# ActiveModel::Dirty helpers
|
68
|
+
rebel.strength_was #=> 10
|
69
|
+
|
70
|
+
# Shorthand for prefix-delegating specific fields
|
71
|
+
rebel.clan_name #=> rebel.clan.name
|
72
|
+
|
73
|
+
# Automagic boolean helpers
|
74
|
+
rebel.cool? #=> rebel.clan.cool?
|
75
|
+
|
76
|
+
# Any method you want
|
77
|
+
rebel.delegate_at_will #=> '10'
|
78
|
+
|
79
|
+
|
80
|
+
## To be considered
|
81
|
+
|
82
|
+
How does the active record join hash-parsing stuff work? EX-
|
83
|
+
|
84
|
+
Rebel.joins(:clan).where(clan: {cool: true)
|
85
|
+
|
86
|
+
Can we make this work for ruby-sql autojoins? Is that even a good idea?
|
87
|
+
Rebel.where(cool: true)
|
88
|
+
|
89
|
+
## Contributing
|
90
|
+
|
91
|
+
1. Fork it
|
92
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
93
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
94
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
95
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/acts_as.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
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/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "acts_as"
|
8
|
+
spec.version = ActsAs::VERSION
|
9
|
+
spec.authors = ["winfred"]
|
10
|
+
spec.email = ["winfred@developerauction.com"]
|
11
|
+
spec.description = %q{ActiveRecord extension for easy STI Delegation}
|
12
|
+
spec.summary = %q{ delegate an entire 1:1 association worth of active record field-related helpers }
|
13
|
+
spec.homepage = "http://github.com/wnadeau/acts_as"
|
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
|
+
end
|
data/lib/acts_as.rb
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
require "acts_as/version"
|
2
|
+
|
3
|
+
module ActsAs
|
4
|
+
class ActsAs::ActiveRecordOnly < StandardError; end
|
5
|
+
|
6
|
+
PREFIX = %w(id created_at updated_at)
|
7
|
+
ACTING_FOR = {}
|
8
|
+
|
9
|
+
def self.included(base)
|
10
|
+
base.extend ClassMethods
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def acts_as_field_match?(method)
|
16
|
+
@association_match = ACTING_FOR.select do |association, fields|
|
17
|
+
fields.select { |f| method.to_s.include?(f) }.any?
|
18
|
+
end.keys.first
|
19
|
+
@association_match && send(@association_match).respond_to?(method)
|
20
|
+
end
|
21
|
+
|
22
|
+
module ClassMethods
|
23
|
+
def acts_as(one_association, prefix: [], whitelist: [])
|
24
|
+
define_method(one_association) do |*args|
|
25
|
+
super(*args) || send("build_#{one_association}", *args)
|
26
|
+
end
|
27
|
+
|
28
|
+
|
29
|
+
association_class = new.send(one_association).class
|
30
|
+
if association_class.table_exists?
|
31
|
+
|
32
|
+
whitelist_and_delegate_fields(association_class, one_association, prefix, whitelist)
|
33
|
+
|
34
|
+
define_method :method_missing do |method, *args, &block|
|
35
|
+
if acts_as_field_match?(method) then
|
36
|
+
send(@association_match).send(method, *args, &block)
|
37
|
+
else
|
38
|
+
super(method, *args, &block)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
define_method :respond_to? do |method, *args, &block|
|
43
|
+
acts_as_field_match?(method) || super(method, *args, &block)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
|
49
|
+
private
|
50
|
+
|
51
|
+
def whitelist_and_delegate_fields(association_class, one_association, prefix, whitelist)
|
52
|
+
association_fields = association_class.columns.map(&:name) - PREFIX - prefix + whitelist
|
53
|
+
|
54
|
+
build_prefix_methods(one_association, prefix)
|
55
|
+
|
56
|
+
attr_accessible *association_fields
|
57
|
+
attr_accessible *prefix.map { |field| "#{one_association}_#{field}" }
|
58
|
+
|
59
|
+
delegate(*(association_fields + association_fields.map { |field| "#{field}=" }), to: one_association)
|
60
|
+
|
61
|
+
#TODO: This feels like a weird place to remember delegated fields
|
62
|
+
ACTING_FOR[one_association] = association_fields + prefix
|
63
|
+
end
|
64
|
+
|
65
|
+
def build_prefix_methods(one_association, prefix)
|
66
|
+
prefix.each do |field|
|
67
|
+
define_method("#{one_association}_#{field}") do |*args|
|
68
|
+
send(one_association).send(field, *args)
|
69
|
+
end
|
70
|
+
|
71
|
+
define_method("#{one_association}_#{field}=") do |*args|
|
72
|
+
send(one_association).send("#{field}=", *args)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'support/active_record'
|
3
|
+
|
4
|
+
class User < ActiveRecord::Base
|
5
|
+
include ActsAs
|
6
|
+
end
|
7
|
+
|
8
|
+
class RebelProfile < ActiveRecord::Base
|
9
|
+
belongs_to :rebel
|
10
|
+
end
|
11
|
+
|
12
|
+
class ImperialProfile < ActiveRecord::Base
|
13
|
+
belongs_to :imperial
|
14
|
+
end
|
15
|
+
|
16
|
+
class Clan < ActiveRecord::Base
|
17
|
+
has_many :rebels
|
18
|
+
|
19
|
+
def delegate_at_will
|
20
|
+
'10'
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
class Rebel < User
|
25
|
+
has_one :profile, class_name: 'RebelProfile', autosave: true
|
26
|
+
belongs_to :clan, autosave: true
|
27
|
+
acts_as :profile
|
28
|
+
acts_as :clan, prefix: %w( name ), whitelist: %w( delegate_at_will )
|
29
|
+
end
|
30
|
+
|
31
|
+
class Imperial < User
|
32
|
+
has_one :profile, class_name: 'ImperialProfile'
|
33
|
+
acts_as :profile
|
34
|
+
end
|
35
|
+
|
36
|
+
|
37
|
+
|
38
|
+
describe ActsAs do
|
39
|
+
|
40
|
+
let(:rebel) { Rebel.create(name: "Leia", clan_name: "Organa") }
|
41
|
+
subject { rebel }
|
42
|
+
|
43
|
+
describe 'whitelist' do
|
44
|
+
it { should respond_to(:delegate_at_will) }
|
45
|
+
its(:delegate_at_will) { should == rebel.clan.delegate_at_will }
|
46
|
+
end
|
47
|
+
|
48
|
+
describe 'proxied getters and setters' do
|
49
|
+
it { should respond_to(:strength) }
|
50
|
+
its(:strength) { should == rebel.clan.strength }
|
51
|
+
it { should respond_to(:strength=) }
|
52
|
+
it 'defines setters as well' do
|
53
|
+
expect { rebel.strength += 50 }.to change{ rebel.clan.strength }.by(50)
|
54
|
+
end
|
55
|
+
|
56
|
+
context 'for imperial class' do
|
57
|
+
subject { Imperial.create(name: "Darth Vader", analog_data: "CHhhawww phheerrrrr") }
|
58
|
+
it { should respond_to(:analog_data) }
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
describe 'prefix fields' do
|
63
|
+
it { should respond_to(:clan_name) }
|
64
|
+
its(:clan_name) { should == rebel.clan.name }
|
65
|
+
end
|
66
|
+
|
67
|
+
describe 'dirty helpers' do
|
68
|
+
before { rebel.strength = 10 }
|
69
|
+
it { should respond_to(:strength_was) }
|
70
|
+
its(:strength_was) { should equal(rebel.clan.strength_was) }
|
71
|
+
its(:strength_was) { should equal(50) }
|
72
|
+
its(:strength) { should equal(rebel.clan.strength) }
|
73
|
+
its(:strength) { should equal(10) }
|
74
|
+
end
|
75
|
+
|
76
|
+
describe 'boolean helpers' do
|
77
|
+
it { should respond_to(:cool?)}
|
78
|
+
specify { rebel.should_not be_cool }
|
79
|
+
end
|
80
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
|
3
|
+
ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:"
|
4
|
+
|
5
|
+
ActiveRecord::Migrator.up "db/migrate"
|
6
|
+
|
7
|
+
ActiveRecord::Migration.create_table :users do |t|
|
8
|
+
t.string :name
|
9
|
+
t.string :type
|
10
|
+
t.integer :clan_id
|
11
|
+
t.timestamps
|
12
|
+
end
|
13
|
+
|
14
|
+
ActiveRecord::Migration.create_table :rebel_profiles do |t|
|
15
|
+
t.integer :rebel_id
|
16
|
+
t.string :serial_data
|
17
|
+
t.timestamps
|
18
|
+
end
|
19
|
+
|
20
|
+
ActiveRecord::Migration.create_table :imperial_profiles do |t|
|
21
|
+
t.integer :imperial_id
|
22
|
+
t.string :analog_data
|
23
|
+
t.timestamps
|
24
|
+
end
|
25
|
+
|
26
|
+
ActiveRecord::Migration.create_table :clans do |t|
|
27
|
+
t.string :name
|
28
|
+
t.integer :strength, default: 50
|
29
|
+
t.boolean :cool
|
30
|
+
t.timestamps
|
31
|
+
end
|
metadata
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: acts_as
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- winfred
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-04-28 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: ActiveRecord extension for easy STI Delegation
|
42
|
+
email:
|
43
|
+
- winfred@developerauction.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- .gitignore
|
49
|
+
- Gemfile
|
50
|
+
- LICENSE.txt
|
51
|
+
- README.md
|
52
|
+
- Rakefile
|
53
|
+
- acts_as.gemspec
|
54
|
+
- lib/acts_as.rb
|
55
|
+
- lib/acts_as/version.rb
|
56
|
+
- spec/acts_for_spec.rb
|
57
|
+
- spec/spec_helper.rb
|
58
|
+
- spec/support/active_record.rb
|
59
|
+
homepage: http://github.com/wnadeau/acts_as
|
60
|
+
licenses:
|
61
|
+
- MIT
|
62
|
+
metadata: {}
|
63
|
+
post_install_message:
|
64
|
+
rdoc_options: []
|
65
|
+
require_paths:
|
66
|
+
- lib
|
67
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - '>='
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
requirements: []
|
78
|
+
rubyforge_project:
|
79
|
+
rubygems_version: 2.0.0
|
80
|
+
signing_key:
|
81
|
+
specification_version: 4
|
82
|
+
summary: delegate an entire 1:1 association worth of active record field-related helpers
|
83
|
+
test_files:
|
84
|
+
- spec/acts_for_spec.rb
|
85
|
+
- spec/spec_helper.rb
|
86
|
+
- spec/support/active_record.rb
|