dynamic_models 0.4.4 → 0.4.7
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 +2 -2
- data/lib/dynamic_models.rb +11 -14
- metadata +3 -3
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.4.
|
1
|
+
0.4.7
|
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.4.
|
8
|
+
s.version = "0.4.7"
|
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 = "2012-
|
12
|
+
s.date = "2012-10-18"
|
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 = [
|
data/lib/dynamic_models.rb
CHANGED
@@ -19,12 +19,16 @@ module DynamicModels
|
|
19
19
|
|
20
20
|
# model name from the controller or type parameter (for a model which is using STI)
|
21
21
|
def model_name
|
22
|
-
sti_model? ? params[:type].underscore :
|
22
|
+
sti_model? ? params[:type].underscore : base_model_class_name
|
23
23
|
end
|
24
24
|
|
25
25
|
# the model class, inferred from the controller
|
26
|
+
def base_model_class_name
|
27
|
+
params[:controller].split('/').last.singularize
|
28
|
+
end
|
29
|
+
|
26
30
|
def base_model_class
|
27
|
-
|
31
|
+
base_model_class_name.camelize.constantize
|
28
32
|
end
|
29
33
|
|
30
34
|
# 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
|
@@ -43,20 +47,13 @@ module DynamicModels
|
|
43
47
|
|
44
48
|
# returns a new model, it can be set with an optional hash
|
45
49
|
def new_model(defaults = {})
|
50
|
+
new_model = model_class.new(defaults)
|
51
|
+
# if there is a parent then associate it with the model
|
46
52
|
if parent_model
|
47
|
-
#
|
48
|
-
if parent_model.respond_to?(plural_model_name)
|
49
|
-
new_model = parent_model.send(plural_model_name).build(defaults)
|
50
|
-
# is is a has_one
|
51
|
-
elsif parent_model.respond_to?(model_name)
|
52
|
-
new_model = parent_model.send("build_#{model_name}", defaults)
|
53
|
-
else
|
54
|
-
raise "can't find association #{model_name} or #{plural_model_name} for #{parent_model.class.name}"
|
55
|
-
end
|
56
|
-
else
|
57
|
-
new_model = model_class.new(defaults)
|
53
|
+
new_model.send("#{parent_model.class.name.downcase}=", parent_model)
|
58
54
|
end
|
59
|
-
return
|
55
|
+
# return the new model
|
56
|
+
new_model
|
60
57
|
end
|
61
58
|
|
62
59
|
# returns a model using the id from the params
|
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.
|
4
|
+
version: 0.4.7
|
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-
|
12
|
+
date: 2012-10-18 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: shoulda
|
@@ -111,7 +111,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
111
111
|
version: '0'
|
112
112
|
segments:
|
113
113
|
- 0
|
114
|
-
hash:
|
114
|
+
hash: 4067818536583822020
|
115
115
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
116
116
|
none: false
|
117
117
|
requirements:
|