resourcelogic 0.10.0 → 0.11.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/Manifest.txt +4 -0
- data/lib/resourcelogic/aliases.rb +134 -0
- data/lib/resourcelogic/context_options.rb +5 -0
- data/lib/resourcelogic/scope.rb +36 -0
- data/lib/resourcelogic/sub_views.rb +98 -0
- data/lib/resourcelogic/version.rb +1 -1
- metadata +5 -1
data/Manifest.txt
CHANGED
@@ -8,14 +8,18 @@ lib/resourcelogic.rb
|
|
8
8
|
lib/resourcelogic/accessors.rb
|
9
9
|
lib/resourcelogic/action_options.rb
|
10
10
|
lib/resourcelogic/actions.rb
|
11
|
+
lib/resourcelogic/aliases.rb
|
11
12
|
lib/resourcelogic/base.rb
|
12
13
|
lib/resourcelogic/child.rb
|
13
14
|
lib/resourcelogic/context.rb
|
15
|
+
lib/resourcelogic/context_options.rb
|
14
16
|
lib/resourcelogic/failable_action_options.rb
|
15
17
|
lib/resourcelogic/parent.rb
|
16
18
|
lib/resourcelogic/response_collector.rb
|
19
|
+
lib/resourcelogic/scope.rb
|
17
20
|
lib/resourcelogic/self.rb
|
18
21
|
lib/resourcelogic/sibling.rb
|
19
22
|
lib/resourcelogic/singleton.rb
|
23
|
+
lib/resourcelogic/sub_views.rb
|
20
24
|
lib/resourcelogic/urligence.rb
|
21
25
|
lib/resourcelogic/version.rb
|
@@ -0,0 +1,134 @@
|
|
1
|
+
module Resourcelogic
|
2
|
+
# This module let's you define various aliases for your controller. For example,
|
3
|
+
# lets say you have the following routes:
|
4
|
+
#
|
5
|
+
# /account/addresses => UsersController
|
6
|
+
# /admin/users/5/addresses => UsersController
|
7
|
+
#
|
8
|
+
# Here is how your AddressesController would look:
|
9
|
+
#
|
10
|
+
# class AddressesController < ResourceController
|
11
|
+
# belongs_to :user
|
12
|
+
# end
|
13
|
+
#
|
14
|
+
# The problem is that sometimes the parent object is called user, sometimes its
|
15
|
+
# called account. So the solution is to do:
|
16
|
+
#
|
17
|
+
# class ResourceController < ApplicationController
|
18
|
+
# route_alias :account, :user
|
19
|
+
# end
|
20
|
+
#
|
21
|
+
# Now ResourceLogic knows that when it see account in the URL it will know the grab
|
22
|
+
# the User model.
|
23
|
+
#
|
24
|
+
# Now I know an alternative could be to do somethig like:
|
25
|
+
#
|
26
|
+
# belongs_to :user, :alias => :account
|
27
|
+
#
|
28
|
+
# The above presents a problem. Take the following URL:
|
29
|
+
#
|
30
|
+
# /productos/1/pictures/4/comments
|
31
|
+
#
|
32
|
+
# In order for Resourcelogic to do its magic relative URLs, it needs to know what
|
33
|
+
# model "productos" should be using. Which should be the Product model, yet we
|
34
|
+
# can't define that in the CommentsController because it's 2 levels above, and we
|
35
|
+
# only specify the parent.
|
36
|
+
#
|
37
|
+
# Now a lot of people say you should never nest more than 2 levels deep, and this
|
38
|
+
# is absolutely true 95% of the time. But what if I want to link back to the parent
|
39
|
+
# object from the comments controller and preserve it's context? In order to do
|
40
|
+
# this I have to go 3 levels deep, because maybe context for the PicturesController
|
41
|
+
# is really important / required. The only way to preserve context is with the URL.
|
42
|
+
#
|
43
|
+
# Sorry for rambling, this documentation is really more of an internal note for me
|
44
|
+
# and to hopefully clarify why I took this approach.
|
45
|
+
module Aliases
|
46
|
+
def self.included(klass)
|
47
|
+
klass.class_eval do
|
48
|
+
extend Config
|
49
|
+
include InstanceMethods
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
module Config
|
54
|
+
def path_alias(alias_name, model_name)
|
55
|
+
current_aliases = path_aliases
|
56
|
+
model_name = model_name.to_sym
|
57
|
+
current_aliases[model_name] ||= []
|
58
|
+
current_aliases[model_name] << alias_name.to_sym
|
59
|
+
write_inheritable_attribute(:path_aliases, current_aliases)
|
60
|
+
end
|
61
|
+
|
62
|
+
def path_aliases
|
63
|
+
read_inheritable_attribute(:path_aliases) || {}
|
64
|
+
end
|
65
|
+
|
66
|
+
def route_alias(alias_name, model_name)
|
67
|
+
current_aliases = route_aliases
|
68
|
+
model_name = model_name.to_sym
|
69
|
+
current_aliases[model_name] ||= []
|
70
|
+
current_aliases[model_name] << alias_name.to_sym
|
71
|
+
write_inheritable_attribute(:route_aliases, current_aliases)
|
72
|
+
end
|
73
|
+
|
74
|
+
def route_aliases
|
75
|
+
read_inheritable_attribute(:route_aliases) || {}
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
module InstanceMethods
|
80
|
+
private
|
81
|
+
def model_name_from_route_alias(alias_name)
|
82
|
+
route_aliases.each do |model_name, aliases|
|
83
|
+
return model_name if aliases.include?(alias_name.to_sym)
|
84
|
+
end
|
85
|
+
nil
|
86
|
+
end
|
87
|
+
|
88
|
+
def route_aliases
|
89
|
+
self.class.route_aliases
|
90
|
+
end
|
91
|
+
|
92
|
+
def model_name_from_path_alias(alias_name)
|
93
|
+
path_aliases.each do |model_name, aliases|
|
94
|
+
return model_name if aliases.include?(alias_name.to_sym)
|
95
|
+
end
|
96
|
+
nil
|
97
|
+
end
|
98
|
+
|
99
|
+
def path_aliases
|
100
|
+
self.class.path_aliases
|
101
|
+
end
|
102
|
+
|
103
|
+
def possible_model_names(model_name)
|
104
|
+
[model_name] + (route_aliases[model_name] || []) + (path_aliases[model_name] || [])
|
105
|
+
end
|
106
|
+
|
107
|
+
# The point of this method is to determine what the part of a url is really referring to.
|
108
|
+
# For example, let's say you did this:
|
109
|
+
#
|
110
|
+
# map.resources :users, :as => :accounts
|
111
|
+
#
|
112
|
+
# Resource logic looks at the request.path. It's going to see "accounts" in the urls. How
|
113
|
+
# is it to know that by "accounts" you are referring to the "users" resource. That's the
|
114
|
+
# point of this method, to say "hey, accounts is mapped to users".
|
115
|
+
def model_name_from_path_part(part)
|
116
|
+
part = part.to_s.singularize
|
117
|
+
model_name_from_route_alias(part) || model_name_from_path_alias(part) || part.to_sym
|
118
|
+
end
|
119
|
+
|
120
|
+
# The point of this method is to determine the name used in the route method. For example,
|
121
|
+
# let's say you did this:
|
122
|
+
#
|
123
|
+
# map.resources :accounts, :controller => "users"
|
124
|
+
#
|
125
|
+
# You would want to use "account" in your path and url helper, not "user".
|
126
|
+
def route_name_from_path_part(part)
|
127
|
+
part = part.to_s.singularize
|
128
|
+
model_name = model_name_from_path_alias(part)
|
129
|
+
return model_name if model_name
|
130
|
+
part.to_sym
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module Resourcelogic
|
2
|
+
module Scope
|
3
|
+
def self.included(klass)
|
4
|
+
klass.class_eval do
|
5
|
+
add_acts_as_resource_module(Methods)
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
module Methods
|
10
|
+
def self.included(klass)
|
11
|
+
klass.class_eval do
|
12
|
+
attr_accessor :scoping, :scoping_parent
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
def scope
|
18
|
+
return @scope if defined?(@scope)
|
19
|
+
|
20
|
+
if parent?
|
21
|
+
@scope = parent_object.send(parent_scope_name)
|
22
|
+
else
|
23
|
+
@scope = model
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def parent_scope
|
28
|
+
parent_model
|
29
|
+
end
|
30
|
+
|
31
|
+
def parent_scope_name
|
32
|
+
@parent_scope_name ||= model_name.to_s.pluralize
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,98 @@
|
|
1
|
+
# Nested and Polymorphic Resource Helpers
|
2
|
+
#
|
3
|
+
module Resourcelogic
|
4
|
+
module SubViews
|
5
|
+
def self.included(klass)
|
6
|
+
klass.class_eval do
|
7
|
+
extend Config
|
8
|
+
add_acts_as_resource_module(Methods)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
module Config
|
13
|
+
def namespace_views_by_context(value = nil)
|
14
|
+
rw_config(:namespace_views_by_context, value)
|
15
|
+
end
|
16
|
+
|
17
|
+
def namespace_views_by_context?
|
18
|
+
!namespace_views_by_context.blank?
|
19
|
+
end
|
20
|
+
|
21
|
+
def namespace_views_by_route_alias(value = nil)
|
22
|
+
rw_config(:namespace_views_by_route_alias, value)
|
23
|
+
end
|
24
|
+
|
25
|
+
def namespace_views_by_route_alias?
|
26
|
+
!namespace_views_by_route_alias.blank?
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
module Methods
|
31
|
+
def self.included(klass)
|
32
|
+
klass.helper_method :namespace_views_by_route_alias, :namespace_views_by_route_alias?,
|
33
|
+
:namespace_views_by_context, :namespace_views_by_context?, :sub_template_name
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
def namespace_views_by_context?
|
38
|
+
self.class.namespace_views_by_context?
|
39
|
+
end
|
40
|
+
|
41
|
+
def namespace_views_by_context
|
42
|
+
self.class.namespace_views_by_context
|
43
|
+
end
|
44
|
+
|
45
|
+
def namespace_views_by_route_alias?
|
46
|
+
self.class.namespace_views_by_route_alias? && route_name != model_name
|
47
|
+
end
|
48
|
+
|
49
|
+
def namespace_views_by_route_alias
|
50
|
+
self.class.namespace_views_by_route_alias
|
51
|
+
end
|
52
|
+
|
53
|
+
def sub_template_name(name)
|
54
|
+
path_parts = [controller_name]
|
55
|
+
|
56
|
+
if namespace_views_by_context?
|
57
|
+
context_folder_name = namespace_views_by_context.is_a?(Hash) && namespace_views_by_context.key?(context) ?
|
58
|
+
namespace_views_by_context[context] : context
|
59
|
+
path_parts << (context_folder_name || "root")
|
60
|
+
end
|
61
|
+
|
62
|
+
if namespace_views_by_route_alias?
|
63
|
+
route_alias_folder_name = namespace_views_by_route_alias.is_a?(Hash) && namespace_views_by_route_alias.key?(route_name) ?
|
64
|
+
namespace_views_by_route_alias[route_name] : route_name
|
65
|
+
path_parts << (route_alias_folder_name).to_s.pluralize
|
66
|
+
end
|
67
|
+
|
68
|
+
path_parts << name
|
69
|
+
path_parts.join("/")
|
70
|
+
end
|
71
|
+
|
72
|
+
def default_template_name(action_name = self.action_name)
|
73
|
+
if namespace_views_by_context? || namespace_views_by_route_alias?
|
74
|
+
sub_template_name(action_name)
|
75
|
+
else
|
76
|
+
super
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
module Partials
|
82
|
+
def _pick_partial_template(partial_path)
|
83
|
+
if respond_to?(:namespace_views_by_context?) && respond_to?(:namespace_views_by_route_alias?) &&
|
84
|
+
(namespace_views_by_context? || namespace_views_by_route_alias?) && !partial_path.include?("/")
|
85
|
+
partial_path = sub_template_name(partial_path)
|
86
|
+
end
|
87
|
+
super
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
|
94
|
+
module ActionView
|
95
|
+
class Base
|
96
|
+
include Resourcelogic::SubViews::Partials
|
97
|
+
end
|
98
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: resourcelogic
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.11.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ben Johnson of Binary Logic
|
@@ -53,15 +53,19 @@ files:
|
|
53
53
|
- lib/resourcelogic/accessors.rb
|
54
54
|
- lib/resourcelogic/action_options.rb
|
55
55
|
- lib/resourcelogic/actions.rb
|
56
|
+
- lib/resourcelogic/aliases.rb
|
56
57
|
- lib/resourcelogic/base.rb
|
57
58
|
- lib/resourcelogic/child.rb
|
58
59
|
- lib/resourcelogic/context.rb
|
60
|
+
- lib/resourcelogic/context_options.rb
|
59
61
|
- lib/resourcelogic/failable_action_options.rb
|
60
62
|
- lib/resourcelogic/parent.rb
|
61
63
|
- lib/resourcelogic/response_collector.rb
|
64
|
+
- lib/resourcelogic/scope.rb
|
62
65
|
- lib/resourcelogic/self.rb
|
63
66
|
- lib/resourcelogic/sibling.rb
|
64
67
|
- lib/resourcelogic/singleton.rb
|
68
|
+
- lib/resourcelogic/sub_views.rb
|
65
69
|
- lib/resourcelogic/urligence.rb
|
66
70
|
- lib/resourcelogic/version.rb
|
67
71
|
has_rdoc: true
|