super_sti 0.2.1 → 0.3.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.
- checksums.yaml +7 -0
- data/.travis.yml +7 -0
- data/{README → README.md} +4 -3
- data/lib/super_sti/hook.rb +15 -6
- data/lib/super_sti/version.rb +1 -1
- data/spec/spec_helper.rb +14 -0
- data/spec/super_sti_spec.rb +102 -0
- data/spec/test_classes.rb +10 -3
- data/super_sti.gemspec +1 -1
- metadata +54 -55
- data/spec/super_sti.rb +0 -53
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 8b358a0cc85f0378b13d20b7a1fddb767b2f4c5e
|
4
|
+
data.tar.gz: e99e5cf78f3f343cbd2decb3455c95d8eec4dbba
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b5aed786eba00111e00cc5575c4d96ecb36770ab0bb7480e070baf06ba526e917f91429390020c3733c4e0b03b0633e69945c79a0b79350bfc6e00d623a28657
|
7
|
+
data.tar.gz: a8560039dff9a4e8b982cc3f7ef4ec593aec7087b80ba68e54730cf7ae56c4170ef76d28279af2b62b16d2b14c7c9daadfd41b6d441593838787c2e1b1b3162c
|
data/.travis.yml
ADDED
data/{README → README.md}
RENAMED
@@ -1,9 +1,9 @@
|
|
1
|
-
Adds an
|
1
|
+
Adds an has_extra_data method to ActiveRecord that invisibly includes an extra data table. Use with STI to keep your database clean. It removes the need to have lots of nullable columns.
|
2
2
|
|
3
3
|
Usage
|
4
4
|
---------
|
5
5
|
Use in Rails 3 app. Add to bundler:
|
6
|
-
gem "
|
6
|
+
gem 'super_sti', :git => "git://github.com/ihid/super_sti.git"
|
7
7
|
|
8
8
|
Tests
|
9
9
|
---------
|
@@ -31,6 +31,7 @@ Usage:
|
|
31
31
|
end
|
32
32
|
|
33
33
|
Classes:
|
34
|
+
|
34
35
|
class Account < ActiveRecord::Base
|
35
36
|
before_create :set_initial_balance
|
36
37
|
|
@@ -76,4 +77,4 @@ Database Schema:
|
|
76
77
|
|
77
78
|
create_table :banks do |t|
|
78
79
|
t.string :name, :null => false
|
79
|
-
end
|
80
|
+
end
|
data/lib/super_sti/hook.rb
CHANGED
@@ -1,4 +1,7 @@
|
|
1
1
|
module SuperSTI
|
2
|
+
|
3
|
+
class DataMissingError < ::StandardError;end
|
4
|
+
|
2
5
|
module Hook
|
3
6
|
|
4
7
|
######
|
@@ -17,23 +20,29 @@ module SuperSTI
|
|
17
20
|
|
18
21
|
table_name = options[:table_name] || "#{self.name.underscore.gsub("/", "_")}_data"
|
19
22
|
klass = Class.new(ActiveRecord::Base) do
|
20
|
-
|
23
|
+
self.table_name = table_name
|
21
24
|
end
|
22
25
|
klass.class_eval &block if block_given?
|
23
26
|
self.const_set "Data", klass
|
24
27
|
|
25
28
|
# Add a reference to a data object that gets created when this is created
|
26
|
-
has_one :data, :class_name => "#{self.name}::Data", :foreign_key => options[:foreign_key]
|
29
|
+
has_one :data, :class_name => "#{self.name}::Data", :foreign_key => options[:foreign_key], :readonly => false, :autosave => true, :dependent => :destroy
|
27
30
|
before_create :get_data
|
28
31
|
|
29
32
|
# A helper method which gets the existing data or builds a new object
|
30
33
|
define_method :get_data do
|
31
|
-
data
|
34
|
+
return data if data
|
35
|
+
return build_data if new_record?
|
36
|
+
raise SuperSTI::DataMissingError
|
32
37
|
end
|
33
38
|
|
34
39
|
# Override respond_to? to check both this object and its data object.
|
35
40
|
define_method "respond_to?" do |sym, include_private = false|
|
36
|
-
|
41
|
+
begin
|
42
|
+
super(sym, include_private) || get_data.respond_to?(sym, include_private)
|
43
|
+
rescue SuperSTI::DataMissingError
|
44
|
+
false
|
45
|
+
end
|
37
46
|
end
|
38
47
|
|
39
48
|
# Override method_missing to check both this object and it's data object for any methods or magic functionality.
|
@@ -41,8 +50,8 @@ module SuperSTI
|
|
41
50
|
# some magic piping that will return a result and then try the data object.
|
42
51
|
define_method :method_missing do |sym, *args|
|
43
52
|
begin
|
44
|
-
super(sym, args)
|
45
|
-
rescue
|
53
|
+
super(sym, *args)
|
54
|
+
rescue NoMethodError
|
46
55
|
get_data.send(sym, *args)
|
47
56
|
end
|
48
57
|
end
|
data/lib/super_sti/version.rb
CHANGED
data/spec/spec_helper.rb
CHANGED
@@ -19,6 +19,12 @@ ActiveRecord::Base.logger
|
|
19
19
|
ActiveRecord::Schema.define(:version => 1) do
|
20
20
|
create_table :accounts do |t|
|
21
21
|
t.float :balance
|
22
|
+
t.boolean :is_approved, :null => false
|
23
|
+
t.string :type, :null => false
|
24
|
+
end
|
25
|
+
|
26
|
+
create_table :basic_account_data do |t|
|
27
|
+
t.integer :basic_account_id, :null => false
|
22
28
|
end
|
23
29
|
|
24
30
|
create_table :bank_account_data do |t|
|
@@ -45,6 +51,14 @@ ActiveRecord::Schema.define(:version => 1) do
|
|
45
51
|
create_table :unusual_foreign_key_data do |t|
|
46
52
|
t.integer :unusual_foreign_key, :null => false
|
47
53
|
end
|
54
|
+
|
55
|
+
create_table :scoped_accounts do |t|
|
56
|
+
t.boolean :is_live, :null => false
|
57
|
+
end
|
58
|
+
|
59
|
+
create_table :scoped_account_data do |t|
|
60
|
+
t.boolean :scoped_account_id, :null => false
|
61
|
+
end
|
48
62
|
end
|
49
63
|
|
50
64
|
require 'test_classes'
|
@@ -0,0 +1,102 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "Super STI models with has_extra_data models" do
|
4
|
+
|
5
|
+
before :each do
|
6
|
+
@bank = Bank.create!(:name => "My Bank")
|
7
|
+
@bank_account = BankAccount.new
|
8
|
+
@valid_bank_account_attributes = {:account_number => "12345678", :sort_code => "12 34 56", :bank => @bank}
|
9
|
+
end
|
10
|
+
|
11
|
+
it "have the data method" do
|
12
|
+
@bank_account.should respond_to(:data)
|
13
|
+
end
|
14
|
+
|
15
|
+
it "can have variables set" do
|
16
|
+
@bank_account.account_number = "12345678"
|
17
|
+
@bank_account.sort_code = "12 34 56"
|
18
|
+
@bank_account.bank = @bank
|
19
|
+
@bank_account.save!
|
20
|
+
@bank_account.data.id.should_not == 0
|
21
|
+
end
|
22
|
+
|
23
|
+
it "creates data with the test class" do
|
24
|
+
@bank_account.attributes = @valid_bank_account_attributes
|
25
|
+
@bank_account.save!
|
26
|
+
@bank_account.data.id.should_not == 0
|
27
|
+
end
|
28
|
+
|
29
|
+
it "can read attributes" do
|
30
|
+
@bank_account.attributes = @valid_bank_account_attributes
|
31
|
+
@bank_account.save!
|
32
|
+
@bank_account = BankAccount.find(@bank_account.id)
|
33
|
+
@bank_account.account_number.should == "12345678"
|
34
|
+
end
|
35
|
+
|
36
|
+
it "can read associations" do
|
37
|
+
@bank_account.attributes = @valid_bank_account_attributes
|
38
|
+
@bank_account.save!
|
39
|
+
@bank_account = BankAccount.find(@bank_account.id)
|
40
|
+
@bank_account.bank.should == @bank
|
41
|
+
end
|
42
|
+
|
43
|
+
it "can have a specifc foreign_key" do
|
44
|
+
obj = UnusualForeignKey.create!
|
45
|
+
obj.data.should_not be_nil
|
46
|
+
end
|
47
|
+
|
48
|
+
it "can have any table name" do
|
49
|
+
obj = UnusualTableName.create!
|
50
|
+
obj.data.should_not be_nil
|
51
|
+
end
|
52
|
+
|
53
|
+
it "does not break scoped" do
|
54
|
+
ba1 = BasicAccount.create!(:is_approved => true)
|
55
|
+
ba2 = BasicAccount.create!(:is_approved => false)
|
56
|
+
ba1.is_approved?.should == true
|
57
|
+
ba2.is_approved?.should == false
|
58
|
+
BasicAccount.approved.count.should == 1
|
59
|
+
end
|
60
|
+
|
61
|
+
it "correctly gets parent id, not data id" do
|
62
|
+
ActiveRecord::Base.connection.execute("INSERT INTO basic_account_data('basic_account_id') VALUES (0)")
|
63
|
+
ba = BasicAccount.create!
|
64
|
+
ba.id.should_not == ba.data.id
|
65
|
+
|
66
|
+
ba2 = BasicAccount.find(ba.id)
|
67
|
+
ba2.id.should == ba.id
|
68
|
+
ba2.data.id.should == ba.data.id
|
69
|
+
ba2.id.should_not == ba2.data.id
|
70
|
+
|
71
|
+
ba3 = Account.find(ba.id)
|
72
|
+
ba3.id.should == ba.id
|
73
|
+
ba3.data.id.should == ba.data.id
|
74
|
+
ba3.id.should_not == ba3.data.id
|
75
|
+
end
|
76
|
+
|
77
|
+
it "if extra data is deleted, it still loads but can't load extra data" do
|
78
|
+
ba = BankAccount.create!(@valid_bank_account_attributes)
|
79
|
+
ActiveRecord::Base.connection.execute("DELETE FROM bank_account_data where id = #{ba.data.id}")
|
80
|
+
|
81
|
+
ba2 = BankAccount.find(ba.id)
|
82
|
+
ba2.id.should == ba.id
|
83
|
+
ba2.data.should be_nil
|
84
|
+
lambda{ba2.bank_id}.should raise_error(SuperSTI::DataMissingError)
|
85
|
+
end
|
86
|
+
|
87
|
+
it "saves data on updates" do
|
88
|
+
# Setup normal bank account
|
89
|
+
@bank_account.attributes = @valid_bank_account_attributes
|
90
|
+
@bank_account.save!
|
91
|
+
@bank_account.account_number.should == "12345678"
|
92
|
+
|
93
|
+
# Update attribute
|
94
|
+
@bank_account.account_number = "87654321"
|
95
|
+
@bank_account.save!
|
96
|
+
|
97
|
+
# Check the database has been updated
|
98
|
+
BankAccount.find(@bank_account.id).account_number.should == "87654321"
|
99
|
+
end
|
100
|
+
|
101
|
+
|
102
|
+
end
|
data/spec/test_classes.rb
CHANGED
@@ -1,13 +1,20 @@
|
|
1
1
|
class Account < ActiveRecord::Base
|
2
|
-
before_create :
|
2
|
+
before_create :set_defaults
|
3
|
+
|
4
|
+
scope :approved, where(:is_approved => true)
|
3
5
|
|
4
6
|
private
|
5
|
-
def
|
6
|
-
self.balance = 0
|
7
|
+
def set_defaults
|
8
|
+
self.balance = 0 unless balance
|
9
|
+
self.is_approved = 0 unless is_approved
|
7
10
|
true
|
8
11
|
end
|
9
12
|
end
|
10
13
|
|
14
|
+
class BasicAccount < Account
|
15
|
+
has_extra_data
|
16
|
+
end
|
17
|
+
|
11
18
|
class BankAccount < Account
|
12
19
|
has_extra_data do
|
13
20
|
belongs_to :bank
|
data/super_sti.gemspec
CHANGED
@@ -12,7 +12,7 @@ Gem::Specification.new do |s|
|
|
12
12
|
s.summary = %q{Ruby Rails - Add has_extra_data to SDI models with clean database tables.}
|
13
13
|
s.description = %q{Adds an add_extra_data method to ActiveRecord that invisibly includes an extra data table. Means you can use STI but keep your database clean.}
|
14
14
|
|
15
|
-
s.add_dependency "rails"
|
15
|
+
s.add_dependency "rails", "~> 3.2.0"
|
16
16
|
s.add_development_dependency "rspec-rails"
|
17
17
|
|
18
18
|
s.rubyforge_project = "has_extra_data"
|
metadata
CHANGED
@@ -1,90 +1,89 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: super_sti
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
version: 0.2.1
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.3.0
|
6
5
|
platform: ruby
|
7
|
-
authors:
|
6
|
+
authors:
|
8
7
|
- Jeremy Walker
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
- !ruby/object:Gem::Dependency
|
11
|
+
date: 2013-10-08 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
16
14
|
name: rails
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
- !ruby/object:Gem::Version
|
23
|
-
version: "0"
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 3.2.0
|
24
20
|
type: :runtime
|
25
|
-
version_requirements: *id001
|
26
|
-
- !ruby/object:Gem::Dependency
|
27
|
-
name: rspec-rails
|
28
21
|
prerelease: false
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 3.2.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec-rails
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
35
34
|
type: :development
|
36
|
-
|
37
|
-
|
38
|
-
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: Adds an add_extra_data method to ActiveRecord that invisibly includes
|
42
|
+
an extra data table. Means you can use STI but keep your database clean.
|
43
|
+
email:
|
39
44
|
- jez.walker@gmail.com
|
40
45
|
executables: []
|
41
|
-
|
42
46
|
extensions: []
|
43
|
-
|
44
47
|
extra_rdoc_files: []
|
45
|
-
|
46
|
-
files:
|
48
|
+
files:
|
47
49
|
- .gitignore
|
48
50
|
- .rspec
|
51
|
+
- .travis.yml
|
49
52
|
- Gemfile
|
50
|
-
- README
|
53
|
+
- README.md
|
51
54
|
- Rakefile
|
52
55
|
- lib/super_sti.rb
|
53
56
|
- lib/super_sti/hook.rb
|
54
57
|
- lib/super_sti/railtie.rb
|
55
58
|
- lib/super_sti/version.rb
|
56
59
|
- spec/spec_helper.rb
|
57
|
-
- spec/
|
60
|
+
- spec/super_sti_spec.rb
|
58
61
|
- spec/test_classes.rb
|
59
62
|
- super_sti.gemspec
|
60
|
-
homepage:
|
63
|
+
homepage: ''
|
61
64
|
licenses: []
|
62
|
-
|
65
|
+
metadata: {}
|
63
66
|
post_install_message:
|
64
67
|
rdoc_options: []
|
65
|
-
|
66
|
-
require_paths:
|
68
|
+
require_paths:
|
67
69
|
- lib
|
68
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
- !ruby/object:Gem::Version
|
79
|
-
version: "0"
|
70
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - '>='
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - '>='
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '0'
|
80
80
|
requirements: []
|
81
|
-
|
82
81
|
rubyforge_project: has_extra_data
|
83
|
-
rubygems_version:
|
82
|
+
rubygems_version: 2.0.0
|
84
83
|
signing_key:
|
85
|
-
specification_version:
|
84
|
+
specification_version: 4
|
86
85
|
summary: Ruby Rails - Add has_extra_data to SDI models with clean database tables.
|
87
|
-
test_files:
|
86
|
+
test_files:
|
88
87
|
- spec/spec_helper.rb
|
89
|
-
- spec/
|
88
|
+
- spec/super_sti_spec.rb
|
90
89
|
- spec/test_classes.rb
|
data/spec/super_sti.rb
DELETED
@@ -1,53 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe "Super STI models with has_extra_data models" do
|
4
|
-
|
5
|
-
before :each do
|
6
|
-
@bank = Bank.create!(:name => "My Bank")
|
7
|
-
@bank_account = BankAccount.new
|
8
|
-
@valid_bank_account_attributes = {:account_number => "12345678", :sort_code => "12 34 56", :bank => @bank}
|
9
|
-
end
|
10
|
-
|
11
|
-
it "have the data method" do
|
12
|
-
@bank_account.should respond_to(:data)
|
13
|
-
end
|
14
|
-
|
15
|
-
it "can have variables set" do
|
16
|
-
@bank_account.account_number = "12345678"
|
17
|
-
@bank_account.sort_code = "12 34 56"
|
18
|
-
@bank_account.bank = @bank
|
19
|
-
@bank_account.save!
|
20
|
-
@bank_account.data.id.should_not == 0
|
21
|
-
end
|
22
|
-
|
23
|
-
it "creates data with the test class" do
|
24
|
-
@bank_account.attributes = @valid_bank_account_attributes
|
25
|
-
@bank_account.save!
|
26
|
-
@bank_account.data.id.should_not == 0
|
27
|
-
end
|
28
|
-
|
29
|
-
it "can read attributes" do
|
30
|
-
@bank_account.attributes = @valid_bank_account_attributes
|
31
|
-
@bank_account.save!
|
32
|
-
@bank_account = BankAccount.find(@bank_account.id)
|
33
|
-
@bank_account.account_number.should == "12345678"
|
34
|
-
end
|
35
|
-
|
36
|
-
it "can read associations" do
|
37
|
-
@bank_account.attributes = @valid_bank_account_attributes
|
38
|
-
@bank_account.save!
|
39
|
-
@bank_account = BankAccount.find(@bank_account.id)
|
40
|
-
@bank_account.bank.should == @bank
|
41
|
-
end
|
42
|
-
|
43
|
-
it "can have a specifc foreign_key" do
|
44
|
-
obj = UnusualForeignKey.create!
|
45
|
-
obj.data.should_not be_nil
|
46
|
-
end
|
47
|
-
|
48
|
-
it "can have any table name" do
|
49
|
-
obj = UnusualTableName.create!
|
50
|
-
obj.data.should_not be_nil
|
51
|
-
end
|
52
|
-
|
53
|
-
end
|