dynamic_models 0.3.2 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/dynamic_models.gemspec +4 -3
- data/lib/dynamic_model_active_record_extensions.rb +29 -0
- data/lib/dynamic_models.rb +31 -5
- metadata +33 -12
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.4.0
|
data/dynamic_models.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "dynamic_models"
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.4.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Craig Ulliott"]
|
12
|
-
s.date = "
|
12
|
+
s.date = "2012-09-28"
|
13
13
|
s.description = "Methods to get and build models directly from parameters. Useful for DRYing up code, specifically very dynamic code for things like admin tools."
|
14
14
|
s.email = "craigulliott@gmail.com"
|
15
15
|
s.extra_rdoc_files = [
|
@@ -25,6 +25,7 @@ Gem::Specification.new do |s|
|
|
25
25
|
"Rakefile",
|
26
26
|
"VERSION",
|
27
27
|
"dynamic_models.gemspec",
|
28
|
+
"lib/dynamic_model_active_record_extensions.rb",
|
28
29
|
"lib/dynamic_models.rb",
|
29
30
|
"test/helper.rb",
|
30
31
|
"test/test_dynamic_models.rb"
|
@@ -32,7 +33,7 @@ Gem::Specification.new do |s|
|
|
32
33
|
s.homepage = "http://github.com/craigulliott/dynamic_models"
|
33
34
|
s.licenses = ["MIT"]
|
34
35
|
s.require_paths = ["lib"]
|
35
|
-
s.rubygems_version = "1.8.
|
36
|
+
s.rubygems_version = "1.8.24"
|
36
37
|
s.summary = "Adds some methods to ActionController to build models dynamically"
|
37
38
|
|
38
39
|
if s.respond_to? :specification_version then
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module DynamicModelActiveRecordExtensions
|
2
|
+
|
3
|
+
def self.included(base)
|
4
|
+
base.extend(ClassMethods)
|
5
|
+
end
|
6
|
+
|
7
|
+
def sti_model?
|
8
|
+
attribute_names.include? "type"
|
9
|
+
end
|
10
|
+
|
11
|
+
# for creating routes which are based on the parent class
|
12
|
+
def sti_parent_class
|
13
|
+
self.class.parent
|
14
|
+
end
|
15
|
+
|
16
|
+
module ClassMethods
|
17
|
+
|
18
|
+
# if this class has a type attribute, then it is a model which is using single table inheritance
|
19
|
+
def sti_model?
|
20
|
+
attribute_names.include? "type"
|
21
|
+
end
|
22
|
+
|
23
|
+
# for creating routes which are based on the parent class
|
24
|
+
def sti_parent_class
|
25
|
+
self.parent
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
end
|
data/lib/dynamic_models.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'dynamic_model_active_record_extensions'
|
2
|
+
|
1
3
|
module DynamicModels
|
2
4
|
|
3
5
|
# looks for object_id notation, and returns a new model
|
@@ -10,9 +12,28 @@ module DynamicModels
|
|
10
12
|
nil
|
11
13
|
end
|
12
14
|
|
13
|
-
#
|
15
|
+
# if we are using a type parameter, then we are dealing with an STI model
|
16
|
+
def sti_model?
|
17
|
+
params[:type].present?
|
18
|
+
end
|
19
|
+
|
20
|
+
# model name from the controller or type parameter (for a model which is using STI)
|
14
21
|
def model_name
|
15
|
-
params[:controller].split('/').last.singularize
|
22
|
+
sti_model? ? params[:type].camelize.constantize : params[:controller].split('/').last.singularize
|
23
|
+
end
|
24
|
+
|
25
|
+
# the model class, inferred from the controller
|
26
|
+
def base_model_class
|
27
|
+
params[:controller].split('/').last.singularize.camelize.constantize
|
28
|
+
end
|
29
|
+
|
30
|
+
# the class we are working with, if an STI model then it will fail loudly on a type which inst descendant from the class which corresponds to this controller
|
31
|
+
def model_class
|
32
|
+
klass = model_name.camelize.constantize
|
33
|
+
if sti_model?
|
34
|
+
raise "you can only pass a type which descends from #{params[:controller]}" unless klass.sti_model? and klass.parent == base_model_class
|
35
|
+
end
|
36
|
+
klass
|
16
37
|
end
|
17
38
|
|
18
39
|
# plural form of the model name from the controller
|
@@ -33,14 +54,14 @@ module DynamicModels
|
|
33
54
|
raise "can't find association #{model_name} or #{plural_model_name} for #{parent_model.class.name}"
|
34
55
|
end
|
35
56
|
else
|
36
|
-
new_model =
|
57
|
+
new_model = model_class.new(defaults)
|
37
58
|
end
|
38
59
|
return new_model
|
39
60
|
end
|
40
61
|
|
41
62
|
# returns a model using the id from the params
|
42
63
|
def fetch_model
|
43
|
-
|
64
|
+
model_class.find params[:id]
|
44
65
|
end
|
45
66
|
|
46
67
|
# returns an array of models (using the name of this controller)
|
@@ -48,7 +69,7 @@ module DynamicModels
|
|
48
69
|
if parent_model
|
49
70
|
return parent_model.send("#{model_name.pluralize.downcase}")
|
50
71
|
else
|
51
|
-
return
|
72
|
+
return model_class.find(:all)
|
52
73
|
end
|
53
74
|
end
|
54
75
|
|
@@ -57,3 +78,8 @@ end
|
|
57
78
|
class ActionController::Base
|
58
79
|
include DynamicModels
|
59
80
|
end
|
81
|
+
|
82
|
+
class ActiveRecord::Base
|
83
|
+
include DynamicModelActiveRecordExtensions
|
84
|
+
end
|
85
|
+
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dynamic_models
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2012-09-28 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: shoulda
|
16
|
-
requirement:
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,15 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements:
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
25
30
|
- !ruby/object:Gem::Dependency
|
26
31
|
name: bundler
|
27
|
-
requirement:
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
28
33
|
none: false
|
29
34
|
requirements:
|
30
35
|
- - ~>
|
@@ -32,10 +37,15 @@ dependencies:
|
|
32
37
|
version: 1.0.0
|
33
38
|
type: :development
|
34
39
|
prerelease: false
|
35
|
-
version_requirements:
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 1.0.0
|
36
46
|
- !ruby/object:Gem::Dependency
|
37
47
|
name: jeweler
|
38
|
-
requirement:
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
39
49
|
none: false
|
40
50
|
requirements:
|
41
51
|
- - ~>
|
@@ -43,10 +53,15 @@ dependencies:
|
|
43
53
|
version: 1.6.0
|
44
54
|
type: :development
|
45
55
|
prerelease: false
|
46
|
-
version_requirements:
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 1.6.0
|
47
62
|
- !ruby/object:Gem::Dependency
|
48
63
|
name: rcov
|
49
|
-
requirement:
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
50
65
|
none: false
|
51
66
|
requirements:
|
52
67
|
- - ! '>='
|
@@ -54,7 +69,12 @@ dependencies:
|
|
54
69
|
version: '0'
|
55
70
|
type: :development
|
56
71
|
prerelease: false
|
57
|
-
version_requirements:
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
58
78
|
description: Methods to get and build models directly from parameters. Useful for
|
59
79
|
DRYing up code, specifically very dynamic code for things like admin tools.
|
60
80
|
email: craigulliott@gmail.com
|
@@ -72,6 +92,7 @@ files:
|
|
72
92
|
- Rakefile
|
73
93
|
- VERSION
|
74
94
|
- dynamic_models.gemspec
|
95
|
+
- lib/dynamic_model_active_record_extensions.rb
|
75
96
|
- lib/dynamic_models.rb
|
76
97
|
- test/helper.rb
|
77
98
|
- test/test_dynamic_models.rb
|
@@ -90,7 +111,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
90
111
|
version: '0'
|
91
112
|
segments:
|
92
113
|
- 0
|
93
|
-
hash:
|
114
|
+
hash: -2137984501742172515
|
94
115
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
95
116
|
none: false
|
96
117
|
requirements:
|
@@ -99,7 +120,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
99
120
|
version: '0'
|
100
121
|
requirements: []
|
101
122
|
rubyforge_project:
|
102
|
-
rubygems_version: 1.8.
|
123
|
+
rubygems_version: 1.8.24
|
103
124
|
signing_key:
|
104
125
|
specification_version: 3
|
105
126
|
summary: Adds some methods to ActionController to build models dynamically
|