stipend 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Jeff Felchner
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,4 @@
1
+ stipend
2
+ =======
3
+
4
+ STIpend is a quick way to allow assignment and modification of Single Table Inheritance in Rails
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,37 @@
1
+ module Stipend
2
+ module Transposable
3
+ module ClassMethods
4
+ def update_type_and_attributes(options = {})
5
+ object, new_type, attrs = options[:object], options[:new_type], options[:attributes]
6
+
7
+ if new_type.present?
8
+ affected_object_attributes = object.attributes.except('type').merge(attrs)
9
+ affected_object = new_type.constantize.new
10
+ else
11
+ affected_object_attributes = object.attributes.merge(attrs)
12
+ affected_object = object.class.new
13
+ end
14
+
15
+ affected_object.id = object.id
16
+ affected_object.attributes = affected_object_attributes
17
+
18
+ if affected_object.valid?
19
+ object.before_update_type_and_attributes_delete
20
+ object.delete
21
+ affected_object.save
22
+ else
23
+ affected_object.instance_variable_set(:@new_record, object.new_record?)
24
+ end
25
+
26
+ affected_object
27
+ end
28
+ end
29
+
30
+ def self.included(base)
31
+ base.extend ClassMethods
32
+ end
33
+
34
+ def before_update_type_and_attributes_delete
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,9 @@
1
+ module Stipend
2
+ module Unprotectable
3
+ def attributes_protected_by_default
4
+ super.tap do |protected_columns|
5
+ protected_columns.delete inheritance_column
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module Stipend
2
+ VERSION = '0.0.1'
3
+ end
data/lib/stipend.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'stipend/transposable'
2
+ require 'stipend/unprotectable'
3
+ require 'stipend/version'
4
+
5
+ module Stipend
6
+ def self.included(base)
7
+ base.send(:include, ::Stipend::Transposable)
8
+ base.send(:extend, ::Stipend::Unprotectable)
9
+ end
10
+ end
@@ -0,0 +1,36 @@
1
+ require 'sqlite3'
2
+ require 'active_record'
3
+ require 'stipend'
4
+
5
+ Dir[File.expand_path('../support/**/*.rb', __FILE__)].each { |f| require f }
6
+
7
+ RSpec.configure do |config|
8
+ config.before(:suite) do
9
+ SQLite3::Database.new 'tmp/test.db'
10
+
11
+ ActiveRecord::Base.establish_connection(
12
+ :adapter => 'sqlite3',
13
+ :database => 'tmp/test.db'
14
+ )
15
+
16
+ class SetupTests < ActiveRecord::Migration
17
+ def up
18
+ create_table :test_types do |t|
19
+ t.string :type
20
+ t.string :foo
21
+ t.string :bar
22
+ end
23
+ end
24
+ end
25
+
26
+ SetupTests.new.migrate(:up)
27
+ end
28
+
29
+ config.before(:each) do
30
+ ActiveRecord::Base.connection.execute 'DELETE FROM test_types'
31
+ end
32
+
33
+ config.after(:suite) do
34
+ `rm -f ./tmp/test.db`
35
+ end
36
+ end
@@ -0,0 +1,7 @@
1
+ RSpec.configure do |config|
2
+ # Configure RSpec to run focused specs, and also respect the alias 'fit' for focused specs
3
+ config.treat_symbols_as_metadata_keys_with_true_values = true
4
+ config.filter_run :focused => true
5
+ config.alias_example_to :fit, :focused => true
6
+ config.run_all_when_everything_filtered = true
7
+ end
@@ -0,0 +1,4 @@
1
+ RSpec.configure do |config|
2
+ # Configure RSpec to respect the alias 'pit' for pending specs
3
+ config.alias_example_to :pit, :pending => true
4
+ end
@@ -0,0 +1,73 @@
1
+ require 'spec_helper'
2
+
3
+ class TestType < ActiveRecord::Base
4
+ include Stipend
5
+
6
+ validates_numericality_of :foo
7
+ end
8
+
9
+ class AlphaTestType < TestType
10
+ end
11
+
12
+ class BetaTestType < TestType
13
+ end
14
+
15
+ describe Stipend::Transposable do
16
+ describe '.update_type_and_attributes' do
17
+ let(:alpha_type) { AlphaTestType.create(:foo => 1, :bar => 'qux') }
18
+ let(:beta_type) { BetaTestType.create( :foo => 1, :bar => 'qux') }
19
+
20
+ context 'can start with one type' do
21
+ it 'can set attributes without changing the type' do
22
+ converted_type = TestType.update_type_and_attributes(:object => alpha_type,
23
+ :new_type => '',
24
+ :attributes => {:foo => 1})
25
+
26
+ TestType.find(alpha_type.id).should eql converted_type
27
+
28
+ converted_type.should be_a AlphaTestType
29
+ converted_type.foo.should eql 1
30
+ end
31
+
32
+ it 'converts it to another type if it has valid parameters' do
33
+ converted_type = TestType.update_type_and_attributes( :object => alpha_type,
34
+ :new_type => 'BetaTestType',
35
+ :attributes => {:foo => 1})
36
+
37
+ TestType.find(alpha_type.id).should eql converted_type
38
+
39
+ converted_type.should be_a BetaTestType
40
+ converted_type.foo.should eql 1
41
+ end
42
+
43
+ it 'does not convert it to another type if the resulting object is not valid' do
44
+ converted_type = TestType.update_type_and_attributes( :object => alpha_type,
45
+ :new_type => 'BetaTestType',
46
+ :attributes => {:foo => 'bar'})
47
+
48
+ stored_type = TestType.find(alpha_type.id)
49
+ stored_type.should be_a AlphaTestType
50
+ stored_type.foo.should eql '1'
51
+ end
52
+
53
+ it 'is the invalid object if the updated object is not valid' do
54
+ converted_type = TestType.update_type_and_attributes( :object => alpha_type,
55
+ :new_type => 'BetaTestType',
56
+ :attributes => {:foo => 'bar'})
57
+
58
+ converted_type.should be_a BetaTestType
59
+ converted_type.foo.should eql 'bar'
60
+ end
61
+
62
+ it 'leaves attributes other than those passed in alone' do
63
+ converted_type = TestType.update_type_and_attributes( :object => alpha_type,
64
+ :new_type => 'BetaTestType',
65
+ :attributes => {:foo => 2})
66
+
67
+ converted_type.should be_a BetaTestType
68
+ converted_type.foo.should eql 2
69
+ converted_type.bar.should eql 'qux'
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+
3
+ class TestType < ActiveRecord::Base
4
+ include Stipend
5
+ end
6
+
7
+ class AlphaTestType < TestType
8
+ end
9
+
10
+ class BetaTestType < TestType
11
+ end
12
+
13
+ describe Stipend::Unprotectable do
14
+ describe 'attributes_protected_by_default' do
15
+ let(:alpha_type) { AlphaTestType.create(:foo => 1, :bar => 'qux') }
16
+
17
+ it 'allows the type attribute to be mass assigned' do
18
+ alpha_type.attributes = {:type => 'BetaTestType', :foo => 2}
19
+
20
+ alpha_type.foo.should eql 2
21
+ alpha_type.type.should eql 'BetaTestType'
22
+ end
23
+ end
24
+ end
metadata ADDED
@@ -0,0 +1,194 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: stipend
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - jfelchner
9
+ - m5rk
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2012-10-07 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: activerecord
17
+ requirement: !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ~>
21
+ - !ruby/object:Gem::Version
22
+ version: 3.1.8
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.8
31
+ - !ruby/object:Gem::Dependency
32
+ name: rspec
33
+ requirement: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ~>
37
+ - !ruby/object:Gem::Version
38
+ version: '2.11'
39
+ type: :development
40
+ prerelease: false
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ~>
45
+ - !ruby/object:Gem::Version
46
+ version: '2.11'
47
+ - !ruby/object:Gem::Dependency
48
+ name: fuubar
49
+ requirement: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ~>
61
+ - !ruby/object:Gem::Version
62
+ version: '1.0'
63
+ - !ruby/object:Gem::Dependency
64
+ name: guard
65
+ requirement: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ~>
69
+ - !ruby/object:Gem::Version
70
+ version: 1.4.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: 1.4.0
79
+ - !ruby/object:Gem::Dependency
80
+ name: guard-rspec
81
+ requirement: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ~>
85
+ - !ruby/object:Gem::Version
86
+ version: 2.0.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: 2.0.0
95
+ - !ruby/object:Gem::Dependency
96
+ name: rb-fsevent
97
+ requirement: !ruby/object:Gem::Requirement
98
+ none: false
99
+ requirements:
100
+ - - ~>
101
+ - !ruby/object:Gem::Version
102
+ version: 0.9.1
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: 0.9.1
111
+ - !ruby/object:Gem::Dependency
112
+ name: awesome_print
113
+ requirement: !ruby/object:Gem::Requirement
114
+ none: false
115
+ requirements:
116
+ - - ~>
117
+ - !ruby/object:Gem::Version
118
+ version: 1.1.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: 1.1.0
127
+ - !ruby/object:Gem::Dependency
128
+ name: sqlite3
129
+ requirement: !ruby/object:Gem::Requirement
130
+ none: false
131
+ requirements:
132
+ - - ~>
133
+ - !ruby/object:Gem::Version
134
+ version: 1.3.6
135
+ type: :development
136
+ prerelease: false
137
+ version_requirements: !ruby/object:Gem::Requirement
138
+ none: false
139
+ requirements:
140
+ - - ~>
141
+ - !ruby/object:Gem::Version
142
+ version: 1.3.6
143
+ description: ''
144
+ email: support@chirrpy.com
145
+ executables: []
146
+ extensions: []
147
+ extra_rdoc_files:
148
+ - README.md
149
+ - LICENSE
150
+ files:
151
+ - lib/stipend/transposable.rb
152
+ - lib/stipend/unprotectable.rb
153
+ - lib/stipend/version.rb
154
+ - lib/stipend.rb
155
+ - Rakefile
156
+ - README.md
157
+ - LICENSE
158
+ - spec/spec_helper.rb
159
+ - spec/support/focused.rb
160
+ - spec/support/pending.rb
161
+ - spec/transposable_spec.rb
162
+ - spec/unprotectable_spec.rb
163
+ homepage: https://github.com/chirrpy/stipend
164
+ licenses: []
165
+ post_install_message:
166
+ rdoc_options:
167
+ - --charset = UTF-8
168
+ require_paths:
169
+ - lib
170
+ required_ruby_version: !ruby/object:Gem::Requirement
171
+ none: false
172
+ requirements:
173
+ - - ! '>='
174
+ - !ruby/object:Gem::Version
175
+ version: '0'
176
+ required_rubygems_version: !ruby/object:Gem::Requirement
177
+ none: false
178
+ requirements:
179
+ - - ! '>='
180
+ - !ruby/object:Gem::Version
181
+ version: '0'
182
+ requirements: []
183
+ rubyforge_project: stipend
184
+ rubygems_version: 1.8.24
185
+ signing_key:
186
+ specification_version: 3
187
+ summary: STIpend is a quick way to allow assignment and modification of Single Table
188
+ Inheritance in Rails.
189
+ test_files:
190
+ - spec/spec_helper.rb
191
+ - spec/support/focused.rb
192
+ - spec/support/pending.rb
193
+ - spec/transposable_spec.rb
194
+ - spec/unprotectable_spec.rb