acts_as_relation 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -17,10 +17,12 @@ declare the product as a +supermodel+ and all types of it as +acts_as+ +:product
17
17
 
18
18
  class Product < ActiveRecord::Base
19
19
  acts_as_superclass
20
+ attr_accessible :name, :price
20
21
  end
21
22
 
22
23
  class Pen < ActiveRecord::Base
23
24
  acts_as :product
25
+ attr_accessible :color
24
26
  end
25
27
 
26
28
  class Book < ActiveRecord::Base
@@ -80,14 +82,14 @@ On the other hand you can always access a specific object from its parent by cal
80
82
  The +acts_as+ relation support these options:
81
83
 
82
84
  * +:as+
83
- * +:auto_include+
85
+ * +:auto_join+
84
86
  * +:class_name+
85
87
  * +:conditions+
86
88
  * +:dependent+
87
89
  * +:include+
88
90
 
89
91
  when +:auto_join+ option set to +true+ (which is by default), every query on child
90
- will automatically includes the parent. For example:
92
+ will automatically joins the parent table. For example:
91
93
 
92
94
  Pen.where("name = ?", "somename")
93
95
 
@@ -56,11 +56,13 @@ module ActiveRecord
56
56
  base.alias_method_chain :#{name}, :autobuild
57
57
 
58
58
  base.extend ActiveRecord::ActsAsRelation::AccessMethods
59
- all_attributes = #{class_name}.content_columns.map(&:name)
60
- ignored_attributes = ["created_at", "updated_at", "#{association_name}_id", "#{association_name}_type"]
61
- associations = #{class_name}.reflect_on_all_associations.map! { |assoc| assoc.name } - ["#{association_name}"]
62
- attributes_to_delegate = all_attributes - ignored_attributes + associations
59
+ attributes = #{class_name}.content_columns.map(&:name)
60
+ associations = #{class_name}.reflect_on_all_associations.map(&:name)
61
+ ignored = ["created_at", "updated_at", "#{association_name}_id", "#{association_name}_type", "#{association_name}"]
62
+ attributes_to_delegate = attributes + associations - ignored
63
63
  base.send :define_acts_as_accessors, attributes_to_delegate, "#{name}"
64
+
65
+ base.attr_accessible.update(#{class_name}.attr_accessible)
64
66
  end
65
67
 
66
68
  def #{name}_with_autobuild
@@ -97,9 +99,11 @@ module ActiveRecord
97
99
  end
98
100
 
99
101
  if options.fetch :auto_join, true
100
- class_eval "default_scope joins(:#{name}).readonly(false)"
102
+ class_eval "default_scope joins(:#{name})"
101
103
  end
102
104
 
105
+ class_eval "default_scope readonly(false)"
106
+
103
107
  code = <<-EndCode
104
108
  def acts_as_other_model?
105
109
  true
data/lib/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module ActiveRecord
2
2
  module ActsAsRelation
3
- VERSION = "0.1.1"
3
+ VERSION = "0.1.2"
4
4
  end
5
5
  end
@@ -20,11 +20,12 @@ describe "Submodel" do
20
20
  end
21
21
 
22
22
  it "inherits Supermodel associations" do
23
- store = Store.new
24
- pen = Pen.new
23
+ store = Store.create :name => 'Big Store'
24
+ pen = Pen.create :name => 'RedPen', :price => 0.8, :color => 'red'
25
25
  pen.store = store
26
- pen.store.should == store
27
- pen.product.store.should == store
26
+ pen.save
27
+ Pen.find(pen.id).store.should == store
28
+ Pen.find(pen.id).product.store.should == store
28
29
  end
29
30
 
30
31
  it "inherits Supermodel validations" do
@@ -70,6 +71,12 @@ describe "Submodel" do
70
71
  end
71
72
  end
72
73
 
74
+ it "have supermodel attr_accessibles as attr_accessibles" do
75
+ Pen.attr_accessible[:default].each do |a|
76
+ Pencil.attr_accessible[:default].should include(a)
77
+ end
78
+ end
79
+
73
80
  it "should be findable" do
74
81
  pen = Pen.create :name => 'RedPen', :price => 0.8, :color => 'red'
75
82
  pen = Pen.find(pen.id)
@@ -2,5 +2,7 @@ class Pen < ActiveRecord::Base
2
2
  acts_as_superclass
3
3
  acts_as :product, as: 'producible'
4
4
 
5
+ attr_accessible :name, :price, :color
6
+
5
7
  validates_presence_of :color
6
8
  end
@@ -3,6 +3,7 @@ class CreateProducts < ActiveRecord::Migration
3
3
  create_table :products, as_relation_superclass: 'producible' do |t|
4
4
  t.string :name
5
5
  t.float :price
6
+ t.integer :store_id
6
7
 
7
8
  t.timestamps
8
9
  end
@@ -31,6 +31,7 @@ ActiveRecord::Schema.define(:version => 4) do
31
31
  t.string "producible_type"
32
32
  t.string "name"
33
33
  t.float "price"
34
+ t.integer "store_id"
34
35
  t.datetime "created_at", :null => false
35
36
  t.datetime "updated_at", :null => false
36
37
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acts_as_relation
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-09 00:00:00.000000000 Z
12
+ date: 2012-07-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -206,53 +206,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
206
206
  version: '0'
207
207
  requirements: []
208
208
  rubyforge_project:
209
- rubygems_version: 1.8.21
209
+ rubygems_version: 1.8.24
210
210
  signing_key:
211
211
  specification_version: 3
212
212
  summary: Easy multi-table inheritance for rails
213
- test_files:
214
- - spec/acts_as_relation_spec.rb
215
- - spec/acts_as_superclass_spec.rb
216
- - spec/dummy/.rspec
217
- - spec/dummy/README.rdoc
218
- - spec/dummy/Rakefile
219
- - spec/dummy/app/assets/javascripts/application.js
220
- - spec/dummy/app/assets/stylesheets/application.css
221
- - spec/dummy/app/controllers/application_controller.rb
222
- - spec/dummy/app/helpers/application_helper.rb
223
- - spec/dummy/app/mailers/.gitkeep
224
- - spec/dummy/app/models/.gitkeep
225
- - spec/dummy/app/models/pen.rb
226
- - spec/dummy/app/models/pencil.rb
227
- - spec/dummy/app/models/product.rb
228
- - spec/dummy/app/models/store.rb
229
- - spec/dummy/app/views/layouts/application.html.erb
230
- - spec/dummy/config.ru
231
- - spec/dummy/config/application.rb
232
- - spec/dummy/config/boot.rb
233
- - spec/dummy/config/database.yml
234
- - spec/dummy/config/environment.rb
235
- - spec/dummy/config/environments/development.rb
236
- - spec/dummy/config/environments/production.rb
237
- - spec/dummy/config/environments/test.rb
238
- - spec/dummy/config/initializers/backtrace_silencers.rb
239
- - spec/dummy/config/initializers/inflections.rb
240
- - spec/dummy/config/initializers/mime_types.rb
241
- - spec/dummy/config/initializers/secret_token.rb
242
- - spec/dummy/config/initializers/session_store.rb
243
- - spec/dummy/config/initializers/wrap_parameters.rb
244
- - spec/dummy/config/locales/en.yml
245
- - spec/dummy/config/routes.rb
246
- - spec/dummy/db/migrate/001_create_stores.rb
247
- - spec/dummy/db/migrate/002_create_products.rb
248
- - spec/dummy/db/migrate/003_create_pens.rb
249
- - spec/dummy/db/migrate/004_create_pencils.rb
250
- - spec/dummy/db/schema.rb
251
- - spec/dummy/lib/assets/.gitkeep
252
- - spec/dummy/log/.gitkeep
253
- - spec/dummy/public/404.html
254
- - spec/dummy/public/422.html
255
- - spec/dummy/public/500.html
256
- - spec/dummy/public/favicon.ico
257
- - spec/dummy/script/rails
258
- - spec/spec_helper.rb
213
+ test_files: []