acts_as_relation 0.0.1
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/README.markdown +66 -0
- data/init.rb +1 -0
- data/lib/active_record/acts/as_relation.rb +83 -0
- data/lib/acts_as_relation.rb +2 -0
- metadata +57 -0
data/README.markdown
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
Acts As Realation
|
2
|
+
=================
|
3
|
+
|
4
|
+
Easy multi-table inheritance for rails.
|
5
|
+
With `acts_as_relation` models inherit parent model:
|
6
|
+
|
7
|
+
* columns
|
8
|
+
* validations
|
9
|
+
* methods
|
10
|
+
|
11
|
+
Multi-table inheritance
|
12
|
+
-----------------------
|
13
|
+
|
14
|
+
Multi-table inheritance happens when each model in the hierarchy is a model all by itself
|
15
|
+
that corresponds to its own database table and can be queried and created individually.
|
16
|
+
The inheritance relationship introduces links between the child model and each of its
|
17
|
+
parents (via an automatically-created `has_one` associations).
|
18
|
+
|
19
|
+
Example
|
20
|
+
-------
|
21
|
+
|
22
|
+
Required columns on parent model name `parent1` are
|
23
|
+
|
24
|
+
1. `parent1_type`
|
25
|
+
2. `parent1_id`
|
26
|
+
|
27
|
+
generate models
|
28
|
+
|
29
|
+
$ rails g model product name:string price:float product_type:string product_id:integer
|
30
|
+
$ rails g model pen color:string
|
31
|
+
|
32
|
+
add some validations and instance methods
|
33
|
+
|
34
|
+
class Product < ActiveRecord::Base
|
35
|
+
validates_presence_of :name, :price
|
36
|
+
|
37
|
+
def hello
|
38
|
+
puts "Hello, My name is '#{name}', my price is $#{price}."
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
pen inherits from product
|
43
|
+
|
44
|
+
class Pen < ActiveRecord::Base
|
45
|
+
acts_as :product
|
46
|
+
end
|
47
|
+
|
48
|
+
pen inhetits products validations and colomns
|
49
|
+
|
50
|
+
p = Pen.new
|
51
|
+
p.valid? => false
|
52
|
+
p.errors => {:name=>["can't be blank"], :price=>["can't be blank"]}
|
53
|
+
|
54
|
+
pen inherits proudct methods
|
55
|
+
|
56
|
+
pen = Pen.new(:name=>"Red Pen", :color=>:red, :price=>0.99)
|
57
|
+
pen.hello => Hello, My name is 'Red Pen', my price is $0.99.
|
58
|
+
|
59
|
+
we can make queries on both models
|
60
|
+
|
61
|
+
Product.where("price <= 1")
|
62
|
+
Pen.where("color = ?", color)
|
63
|
+
|
64
|
+
---
|
65
|
+
|
66
|
+
Copyright (c) 2011 Hassan Zamani, released under the MIT license.
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'acts_as_relation'
|
@@ -0,0 +1,83 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module Acts
|
3
|
+
module AsRelation
|
4
|
+
def self.included(base)
|
5
|
+
base.extend(ClassMethods)
|
6
|
+
end
|
7
|
+
|
8
|
+
module AccessMethods
|
9
|
+
def define_acts_as_accessors(attribs, model_name)
|
10
|
+
attribs.each do |attrib|
|
11
|
+
class_eval <<-EndClass
|
12
|
+
def #{attrib}
|
13
|
+
#{model_name}.#{attrib}
|
14
|
+
end
|
15
|
+
|
16
|
+
def #{attrib}=(value)
|
17
|
+
self.#{model_name}.#{attrib} = value
|
18
|
+
end
|
19
|
+
|
20
|
+
def #{attrib}?
|
21
|
+
self.#{model_name}.#{attrib}?
|
22
|
+
end
|
23
|
+
EndClass
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
module ClassMethods
|
29
|
+
|
30
|
+
def acts_as(model_name)
|
31
|
+
name = model_name.to_s.underscore.singularize
|
32
|
+
|
33
|
+
# Create A AsModel module
|
34
|
+
as_model = Module.new
|
35
|
+
Object.const_set("As#{name.camelcase}", as_model)
|
36
|
+
|
37
|
+
as_model.module_eval <<-EndModule
|
38
|
+
def self.included(base)
|
39
|
+
base.has_one :#{name}, :as => :#{name}, :autosave => true, :validate => false
|
40
|
+
base.validate :#{name}_must_be_valid
|
41
|
+
base.alias_method_chain :#{name}, :autobuild
|
42
|
+
|
43
|
+
base.extend ActiveRecord::Acts::AsRelation::AccessMethods
|
44
|
+
all_attributes = #{name.camelcase.constantize}.content_columns.map(&:name)
|
45
|
+
ignored_attributes = ["created_at", "updated_at", "#{name}_type"]
|
46
|
+
attributes_to_delegate = all_attributes - ignored_attributes
|
47
|
+
base.define_acts_as_accessors(attributes_to_delegate, "#{name}")
|
48
|
+
end
|
49
|
+
|
50
|
+
def #{name}_with_autobuild
|
51
|
+
#{name}_without_autobuild || build_#{name}
|
52
|
+
end
|
53
|
+
|
54
|
+
def method_missing(method, *arg, &block)
|
55
|
+
#{name}.send(method, *arg, &block)
|
56
|
+
rescue NoMethodError
|
57
|
+
super
|
58
|
+
end
|
59
|
+
|
60
|
+
def respond_to?(method, include_private_methods = false)
|
61
|
+
super || self.#{name}.respond_to?(method, include_private_methods)
|
62
|
+
end
|
63
|
+
|
64
|
+
protected
|
65
|
+
|
66
|
+
def #{name}_must_be_valid
|
67
|
+
unless #{name}.valid?
|
68
|
+
#{name}.errors.each do |att, message|
|
69
|
+
errors.add(att, message)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
EndModule
|
74
|
+
|
75
|
+
class_eval do
|
76
|
+
include "As#{name.camelcase}".constantize
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
metadata
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: acts_as_relation
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.1
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Hassan Zamani
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-06-12 00:00:00 Z
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email: hsn.zamani@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- lib/acts_as_relation.rb
|
26
|
+
- lib/active_record/acts/as_relation.rb
|
27
|
+
- init.rb
|
28
|
+
- README.markdown
|
29
|
+
homepage: https://github.com/hzamani/acts_as_relation/
|
30
|
+
licenses: []
|
31
|
+
|
32
|
+
post_install_message:
|
33
|
+
rdoc_options: []
|
34
|
+
|
35
|
+
require_paths:
|
36
|
+
- lib
|
37
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: "0"
|
43
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
44
|
+
none: false
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: "0"
|
49
|
+
requirements: []
|
50
|
+
|
51
|
+
rubyforge_project:
|
52
|
+
rubygems_version: 1.8.5
|
53
|
+
signing_key:
|
54
|
+
specification_version: 3
|
55
|
+
summary: Easy multi-table inheritance for rails
|
56
|
+
test_files: []
|
57
|
+
|