matid-resource_controller 0.6.7 → 0.7.0
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/VERSION.yml
CHANGED
|
@@ -2,23 +2,23 @@ module ResourceController
|
|
|
2
2
|
module Helpers
|
|
3
3
|
module CurrentObjects
|
|
4
4
|
protected
|
|
5
|
-
# Used internally to return the model for your resource.
|
|
5
|
+
# Used internally to return the model for your resource.
|
|
6
6
|
#
|
|
7
7
|
def model
|
|
8
8
|
model_name.to_s.camelize.constantize
|
|
9
9
|
end
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
|
|
12
12
|
# Used to fetch the collection for the index method
|
|
13
13
|
#
|
|
14
14
|
# In order to customize the way the collection is fetched, to add something like pagination, for example, override this method.
|
|
15
15
|
#
|
|
16
16
|
def collection
|
|
17
|
-
end_of_association_chain.
|
|
17
|
+
end_of_association_chain.all
|
|
18
18
|
end
|
|
19
|
-
|
|
19
|
+
|
|
20
20
|
# Returns the current param.
|
|
21
|
-
#
|
|
21
|
+
#
|
|
22
22
|
# Defaults to params[:id].
|
|
23
23
|
#
|
|
24
24
|
# Override this method if you'd like to use an alternate param name.
|
|
@@ -26,7 +26,17 @@ module ResourceController
|
|
|
26
26
|
def param
|
|
27
27
|
params[:id]
|
|
28
28
|
end
|
|
29
|
-
|
|
29
|
+
|
|
30
|
+
# Returns the model finder method.
|
|
31
|
+
#
|
|
32
|
+
# Defaults to :find_by_permalink if defined, otherwise :find
|
|
33
|
+
#
|
|
34
|
+
# Override this method if you'd like to use an alternate finder.
|
|
35
|
+
#
|
|
36
|
+
def finder
|
|
37
|
+
model.respond_to?(:find_by_permalink) ? :find_by_permalink : :permalink
|
|
38
|
+
end
|
|
39
|
+
|
|
30
40
|
# Used to fetch the current member object in all of the singular methods that operate on an existing member.
|
|
31
41
|
#
|
|
32
42
|
# Override this method if you'd like to fetch your objects in some alternate way, like using a permalink.
|
|
@@ -39,30 +49,30 @@ module ResourceController
|
|
|
39
49
|
# end
|
|
40
50
|
#
|
|
41
51
|
def object
|
|
42
|
-
@object ||= end_of_association_chain.
|
|
52
|
+
@object ||= end_of_association_chain.send(finder, param) unless param.nil?
|
|
43
53
|
@object
|
|
44
54
|
end
|
|
45
|
-
|
|
55
|
+
|
|
46
56
|
# Used internally to load the member object in to an instance variable @#{model_name} (i.e. @post)
|
|
47
57
|
#
|
|
48
58
|
def load_object
|
|
49
59
|
instance_variable_set "@#{parent_type}", parent_object if parent?
|
|
50
60
|
instance_variable_set "@#{object_name}", object
|
|
51
61
|
end
|
|
52
|
-
|
|
62
|
+
|
|
53
63
|
# Used internally to load the collection in to an instance variable @#{model_name.pluralize} (i.e. @posts)
|
|
54
64
|
#
|
|
55
65
|
def load_collection
|
|
56
66
|
instance_variable_set "@#{parent_type}", parent_object if parent?
|
|
57
67
|
instance_variable_set "@#{object_name.to_s.pluralize}", collection
|
|
58
68
|
end
|
|
59
|
-
|
|
69
|
+
|
|
60
70
|
# Returns the form params. Defaults to params[model_name] (i.e. params["post"])
|
|
61
71
|
#
|
|
62
72
|
def object_params
|
|
63
73
|
params["#{object_name}"]
|
|
64
74
|
end
|
|
65
|
-
|
|
75
|
+
|
|
66
76
|
# Builds the object, but doesn't save it, during the new, and create action.
|
|
67
77
|
#
|
|
68
78
|
def build_object
|
|
@@ -3,60 +3,65 @@
|
|
|
3
3
|
module ResourceController
|
|
4
4
|
module Helpers
|
|
5
5
|
module Nested
|
|
6
|
-
protected
|
|
6
|
+
protected
|
|
7
7
|
# Returns the relevant association proxy of the parent. (i.e. /posts/1/comments # => @post.comments)
|
|
8
8
|
#
|
|
9
9
|
def parent_association
|
|
10
10
|
@parent_association ||= parent_object.send(model_name.to_s.pluralize.to_sym)
|
|
11
11
|
end
|
|
12
|
-
|
|
12
|
+
|
|
13
13
|
# Returns the type of the current parent
|
|
14
14
|
#
|
|
15
15
|
def parent_type
|
|
16
16
|
@parent_type ||= parent_type_from_params || parent_type_from_request
|
|
17
17
|
end
|
|
18
|
-
|
|
18
|
+
|
|
19
19
|
# Returns the type of the current parent extracted from params
|
|
20
|
-
#
|
|
20
|
+
#
|
|
21
21
|
def parent_type_from_params
|
|
22
22
|
[*belongs_to].find { |parent| !params["#{parent}_id".to_sym].nil? }
|
|
23
23
|
end
|
|
24
|
-
|
|
24
|
+
|
|
25
25
|
# Returns the type of the current parent extracted form a request path
|
|
26
|
-
#
|
|
26
|
+
#
|
|
27
27
|
def parent_type_from_request
|
|
28
28
|
[*belongs_to].find { |parent| request.path.split('/').include? parent.to_s }
|
|
29
29
|
end
|
|
30
|
-
|
|
30
|
+
|
|
31
31
|
# Returns true/false based on whether or not a parent is present.
|
|
32
32
|
#
|
|
33
33
|
def parent?
|
|
34
34
|
!parent_type.nil?
|
|
35
35
|
end
|
|
36
|
-
|
|
36
|
+
|
|
37
37
|
# Returns true/false based on whether or not a parent is a singleton.
|
|
38
|
-
#
|
|
38
|
+
#
|
|
39
39
|
def parent_singleton?
|
|
40
40
|
!parent_type_from_request.nil? && parent_type_from_params.nil?
|
|
41
41
|
end
|
|
42
|
-
|
|
42
|
+
|
|
43
43
|
# Returns the current parent param, if there is a parent. (i.e. params[:post_id])
|
|
44
44
|
def parent_param
|
|
45
45
|
params["#{parent_type}_id".to_sym]
|
|
46
46
|
end
|
|
47
|
-
|
|
47
|
+
|
|
48
48
|
# Like the model method, but for a parent relationship.
|
|
49
|
-
#
|
|
49
|
+
#
|
|
50
50
|
def parent_model
|
|
51
51
|
parent_type.to_s.camelize.constantize
|
|
52
52
|
end
|
|
53
|
-
|
|
53
|
+
|
|
54
|
+
# Like the finder method, but for a parent relationship
|
|
55
|
+
def parent_finder
|
|
56
|
+
parent_model.respond_to?(:find_by_permalink) ? :find_by_permalink : :permalink
|
|
57
|
+
end
|
|
58
|
+
|
|
54
59
|
# Returns the current parent object if a parent object is present.
|
|
55
60
|
#
|
|
56
61
|
def parent_object
|
|
57
|
-
parent? && !parent_singleton? ? parent_model.
|
|
62
|
+
parent? && !parent_singleton? ? parent_model.send(parent_finder, parent_param) : nil
|
|
58
63
|
end
|
|
59
|
-
|
|
64
|
+
|
|
60
65
|
# If there is a parent, returns the relevant association proxy. Otherwise returns model.
|
|
61
66
|
#
|
|
62
67
|
def end_of_association_chain
|
metadata
CHANGED
|
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
|
4
4
|
prerelease: false
|
|
5
5
|
segments:
|
|
6
6
|
- 0
|
|
7
|
-
- 6
|
|
8
7
|
- 7
|
|
9
|
-
|
|
8
|
+
- 0
|
|
9
|
+
version: 0.7.0
|
|
10
10
|
platform: ruby
|
|
11
11
|
authors:
|
|
12
12
|
- Mateusz Drozdzynski
|
|
@@ -15,7 +15,7 @@ autorequire:
|
|
|
15
15
|
bindir: bin
|
|
16
16
|
cert_chain: []
|
|
17
17
|
|
|
18
|
-
date: 2010-
|
|
18
|
+
date: 2010-04-19 00:00:00 +02:00
|
|
19
19
|
default_executable:
|
|
20
20
|
dependencies: []
|
|
21
21
|
|