Module: Pakyow::Reflection::Builders::Helpers::Controller
Constant Summary collapse
- RESOURCE_ENDPOINTS =
%i(new edit list show).freeze
Instance Method Summary collapse
- #controller_at_path(path, state = @app.state(:controller)) ⇒ Object
- #controller_closest_to_path(path, state = @app.state(:controller)) ⇒ Object
- #controller_path(view_path) ⇒ Object
- #define_controller_at_path(path, within: nil) ⇒ Object
- #define_resource_for_scope_at_path(scope, path) ⇒ Object
- #ensure_controller_has_helpers(controller) ⇒ Object
- #find_or_define_controller_at_path(path) ⇒ Object
- #find_or_define_resource_for_scope_at_path(scope, path, endpoint_type = nil) ⇒ Object
- #find_or_define_resource_for_scope_in_resource(scope, resource) ⇒ Object
- #full_resource_path_for_scope_at_path(scope, path) ⇒ Object
- #resource_for_scope_at_path(scope, path, state = @app.state(:controller)) ⇒ Object
- #resource_name_for_scope(scope) ⇒ Object
- #resource_namespace_path_for_scope_at_path(scope, path) ⇒ Object
- #resource_path_for_scope(scope) ⇒ Object
- #resource_source_at_path(view_path) ⇒ Object
- #route_name(view_path) ⇒ Object
- #route_path(view_path) ⇒ Object
- #view_path_directory?(view_path) ⇒ Boolean
- #within_resource?(view_path) ⇒ Boolean
Instance Method Details
#controller_at_path(path, state = @app.state(:controller)) ⇒ Object
16 17 18 19 20 21 22 23 24 |
# File 'lib/pakyow/reflection/builders/helpers/controller.rb', line 16 def controller_at_path(path, state = @app.state(:controller)) if state.any? state.find { |controller| String.normalize_path(String.collapse_path(controller.path_to_self)) == String.normalize_path(path) } || controller_at_path(path, state.flat_map(&:children)) else nil end end |
#controller_closest_to_path(path, state = @app.state(:controller)) ⇒ Object
26 27 28 29 30 31 32 33 34 |
# File 'lib/pakyow/reflection/builders/helpers/controller.rb', line 26 def controller_closest_to_path(path, state = @app.state(:controller)) if state.any? controller_closest_to_path(path, state.flat_map(&:children)) || state.find { |controller| String.normalize_path(path).start_with?(String.normalize_path(String.collapse_path(controller.path_to_self))) } else nil end end |
#controller_path(view_path) ⇒ Object
71 72 73 74 75 76 77 |
# File 'lib/pakyow/reflection/builders/helpers/controller.rb', line 71 def controller_path(view_path) if view_path_directory?(view_path) view_path else view_path.split("/")[0..-2].to_a.join("/") end end |
#define_controller_at_path(path, within: nil) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/pakyow/reflection/builders/helpers/controller.rb', line 36 def define_controller_at_path(path, within: nil) nested_state = if within within.children else @app.state(:controller) end path = String.normalize_path(path) if controller = controller_closest_to_path(path, nested_state) context = controller path = path.gsub( /^#{String.normalize_path(String.collapse_path(controller.path_to_self))}/, "" ) else context = within || @app end controller_name = if path == "/" :root else String.normalize_path(path)[1..-1].gsub("/", "_").to_sym end method = if context.is_a?(Class) && context.ancestors.include?(Pakyow::Routing::Controller) :namespace else :controller end context.send(method, controller_name, String.normalize_path(path)) do # intentionally empty end end |
#define_resource_for_scope_at_path(scope, path) ⇒ Object
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/pakyow/reflection/builders/helpers/controller.rb', line 163 def define_resource_for_scope_at_path(scope, path) context = if resource_namespace_path = resource_namespace_path_for_scope_at_path(scope, path) if within_resource?(resource_namespace_path) ensure_controller_has_helpers( find_or_define_resource_for_scope_at_path( resource_source_at_path(resource_namespace_path), resource_namespace_path ) ) else ensure_controller_has_helpers( find_or_define_controller_at_path(resource_namespace_path) ) end else @app end context.resource resource_name_for_scope(scope), resource_path_for_scope(scope) do # intentionally empty end end |
#ensure_controller_has_helpers(controller) ⇒ Object
203 204 205 206 207 208 209 |
# File 'lib/pakyow/reflection/builders/helpers/controller.rb', line 203 def ensure_controller_has_helpers(controller) unless controller.ancestors.include?(Extension::Controller) controller.include Extension::Controller end controller end |
#find_or_define_controller_at_path(path) ⇒ Object
12 13 14 |
# File 'lib/pakyow/reflection/builders/helpers/controller.rb', line 12 def find_or_define_controller_at_path(path) controller_at_path(path) || define_controller_at_path(path) end |
#find_or_define_resource_for_scope_at_path(scope, path, endpoint_type = nil) ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/pakyow/reflection/builders/helpers/controller.rb', line 121 def find_or_define_resource_for_scope_at_path(scope, path, endpoint_type = nil) resource = resource_for_scope_at_path(scope, path) || define_resource_for_scope_at_path(scope, path) if path.end_with?(resource_path_for_scope(scope)) || endpoint_type.nil? || RESOURCE_ENDPOINTS.include?(path.split("/").last.to_sym) return resource else controller_for_endpoint_type = resource.send(endpoint_type) nested_path = if view_path_directory?(path) path else path.split("/")[0..-2].join("/") end nested_path = nested_path.gsub( /^#{String.normalize_path(String.collapse_path(controller_for_endpoint_type.path_to_self))}/, "" ) if current_controller = controller_at_path(nested_path, resource.children) return current_controller else if nested_path.empty? controller_for_endpoint_type else define_controller_at_path(nested_path, within: controller_for_endpoint_type) end end end end |
#find_or_define_resource_for_scope_in_resource(scope, resource) ⇒ Object
211 212 213 214 215 216 217 |
# File 'lib/pakyow/reflection/builders/helpers/controller.rb', line 211 def find_or_define_resource_for_scope_in_resource(scope, resource) resource.children.find { |child| child.path == resource_path_for_scope(scope) } || resource.resource(resource_name_for_scope(scope), resource_path_for_scope(scope)) do # intentionally empty end end |
#full_resource_path_for_scope_at_path(scope, path) ⇒ Object
219 220 221 222 223 224 225 226 |
# File 'lib/pakyow/reflection/builders/helpers/controller.rb', line 219 def full_resource_path_for_scope_at_path(scope, path) String.normalize_path( File.join( resource_namespace_path_for_scope_at_path(scope, path).to_s, scope.plural_name.to_s ) ) end |
#resource_for_scope_at_path(scope, path, state = @app.state(:controller)) ⇒ Object
151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/pakyow/reflection/builders/helpers/controller.rb', line 151 def resource_for_scope_at_path(scope, path, state = @app.state(:controller)) if state.any? state.select { |controller| controller.expansions.include?(:resource) }.find { |controller| String.normalize_path(String.collapse_path(controller.path_to_self)) == full_resource_path_for_scope_at_path(scope, path) } || resource_for_scope_at_path(scope, path, state.flat_map(&:children)) else nil end end |
#resource_name_for_scope(scope) ⇒ Object
195 196 197 |
# File 'lib/pakyow/reflection/builders/helpers/controller.rb', line 195 def resource_name_for_scope(scope) scope.plural_name end |
#resource_namespace_path_for_scope_at_path(scope, path) ⇒ Object
185 186 187 188 189 190 191 192 193 |
# File 'lib/pakyow/reflection/builders/helpers/controller.rb', line 185 def resource_namespace_path_for_scope_at_path(scope, path) resource_path = resource_path_for_scope(scope) if path.start_with?(resource_path) nil elsif path.include?(resource_path) path.split(resource_path, 2)[0] end end |
#resource_path_for_scope(scope) ⇒ Object
199 200 201 |
# File 'lib/pakyow/reflection/builders/helpers/controller.rb', line 199 def resource_path_for_scope(scope) String.normalize_path(scope.plural_name) end |
#resource_source_at_path(view_path) ⇒ Object
103 104 105 106 107 108 109 110 111 |
# File 'lib/pakyow/reflection/builders/helpers/controller.rb', line 103 def resource_source_at_path(view_path) view_path.split("/").reverse.each do |view_path_part| @app.state(:source).each do |source| if source.plural_name == view_path_part.to_sym return source end end end end |
#route_name(view_path) ⇒ Object
87 88 89 90 91 92 93 |
# File 'lib/pakyow/reflection/builders/helpers/controller.rb', line 87 def route_name(view_path) if view_path_directory?(view_path) :default else view_path.split("/").last.to_sym end end |
#route_path(view_path) ⇒ Object
95 96 97 98 99 100 101 |
# File 'lib/pakyow/reflection/builders/helpers/controller.rb', line 95 def route_path(view_path) if view_path_directory?(view_path) "/" else "/#{view_path.split("/").last}" end end |
#view_path_directory?(view_path) ⇒ Boolean
113 114 115 116 117 |
# File 'lib/pakyow/reflection/builders/helpers/controller.rb', line 113 def view_path_directory?(view_path) @app.state(:templates).any? { |templates| File.directory?(File.join(templates.path, templates.config[:paths][:pages], view_path)) } end |
#within_resource?(view_path) ⇒ Boolean
79 80 81 82 83 84 85 |
# File 'lib/pakyow/reflection/builders/helpers/controller.rb', line 79 def within_resource?(view_path) view_path.split("/").any? { |view_path_part| @app.state(:source).any? { |source| source.plural_name == view_path_part.to_sym } } end |