Module: Effective::Resources::Actions
- Included in:
- Effective::Resource
- Defined in:
- app/models/effective/resources/actions.rb
Constant Summary collapse
- EMPTY_HASH =
{}
- POST_VERBS =
['POST', 'PUT', 'PATCH']
- CRUD_ACTIONS =
%i(index new create show edit update destroy)
Instance Method Summary collapse
-
#action_path(action, resource = nil, opts = nil) ⇒ Object
Effective::Resource.new(‘admin/posts’).action_path(:edit, Post.last) => ‘/admin/posts/3/edit’ Will work for any action.
-
#action_path_helper(action) ⇒ Object
Effective::Resource.new(‘admin/posts’).action_path_helper(:edit) => ‘edit_admin_posts_path’ This will return empty for create, update and destroy.
- #actions ⇒ Object
-
#collection_actions ⇒ Object
GET actions.
- #collection_get_actions ⇒ Object
- #collection_post_actions ⇒ Object
-
#controller_path ⇒ Object
Same as controller_path in the view.
- #crud_actions ⇒ Object
-
#member_actions ⇒ Object
All actions.
- #member_delete_actions ⇒ Object
-
#member_get_actions ⇒ Object
GET actions.
-
#member_post_actions ⇒ Object
POST/PUT/PATCH actions.
-
#routes ⇒ Object
This was written for the Edit actions fallback templates and Datatables Effective::Resource.new(‘admin/posts’).routes.
Instance Method Details
#action_path(action, resource = nil, opts = nil) ⇒ Object
Effective::Resource.new(‘admin/posts’).action_path(:edit, Post.last) => ‘/admin/posts/3/edit’ Will work for any action. Returns the real path
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'app/models/effective/resources/actions.rb', line 55 def action_path(action, resource = nil, opts = nil) opts ||= EMPTY_HASH if klass.nil? && resource.present? && initialized_name.kind_of?(ActiveRecord::Reflection::BelongsToReflection) return Effective::Resource.new(resource, namespace: namespace).action_path(action, resource, opts) end return unless routes[action] if resource.kind_of?(Hash) opts = resource; resource = nil end # edge case: Effective::Resource.new('admin/comments').action_path(:new, @post) if resource && klass && !resource.kind_of?(klass) if (bt = belongs_to(resource)).present? && instance.respond_to?("#{bt.name}=") return routes[action].format(klass.new(bt.name => resource)).presence end end target = (resource || instance) formattable = if routes[action].parts.include?(:id) if target.respond_to?(:to_param) && target.respond_to?(:id) && (target.to_param != target.id.to_s) routes[action].parts.each_with_object({}) do |part, h| if part == :id h[part] = target.to_param elsif part == :format # Nothing elsif target.respond_to?(part) h[part] = target.public_send(part) end end elsif target.respond_to?(:to_param) || target.respond_to?(:id) target else {id: target} end end # Generate the path path = (routes[action].format(formattable || EMPTY_HASH) rescue nil) if path.present? && opts.present? uri = URI.parse(path) uri.query = URI.encode_www_form(opts) path = uri.to_s end path end |
#action_path_helper(action) ⇒ Object
Effective::Resource.new(‘admin/posts’).action_path_helper(:edit) => ‘edit_admin_posts_path’ This will return empty for create, update and destroy
48 49 50 51 |
# File 'app/models/effective/resources/actions.rb', line 48 def action_path_helper(action) return unless routes[action] return (routes[action].name + '_path') if routes[action].name.present? end |
#actions ⇒ Object
107 108 109 |
# File 'app/models/effective/resources/actions.rb', line 107 def actions @route_actions ||= routes.keys end |
#collection_actions ⇒ Object
GET actions
116 117 118 119 120 |
# File 'app/models/effective/resources/actions.rb', line 116 def collection_actions @collection_actions ||= ( routes.map { |_, route| route.defaults[:action].to_sym if is_collection_route?(route) }.tap(&:compact!) ) end |
#collection_get_actions ⇒ Object
122 123 124 125 126 |
# File 'app/models/effective/resources/actions.rb', line 122 def collection_get_actions @collection_get_actions ||= ( routes.map { |_, route| route.defaults[:action].to_sym if is_collection_route?(route) && is_get_route?(route) }.tap(&:compact!) ) end |
#collection_post_actions ⇒ Object
128 129 130 131 132 |
# File 'app/models/effective/resources/actions.rb', line 128 def collection_post_actions @collection_post_actions ||= ( routes.map { |_, route| route.defaults[:action].to_sym if is_collection_route?(route) && is_post_route?(route) }.tap(&:compact!) ) end |
#controller_path ⇒ Object
Same as controller_path in the view
162 163 164 |
# File 'app/models/effective/resources/actions.rb', line 162 def controller_path [namespace, plural_name].compact * '/' end |
#crud_actions ⇒ Object
111 112 113 |
# File 'app/models/effective/resources/actions.rb', line 111 def crud_actions @crud_actions ||= (actions & CRUD_ACTIONS) end |
#member_actions ⇒ Object
All actions
135 136 137 138 139 |
# File 'app/models/effective/resources/actions.rb', line 135 def member_actions @member_actions ||= ( routes.map { |_, route| route.defaults[:action].to_sym if is_member_route?(route) }.tap(&:compact!) ) end |
#member_delete_actions ⇒ Object
148 149 150 151 152 |
# File 'app/models/effective/resources/actions.rb', line 148 def member_delete_actions @member_delete_actions ||= ( routes.map { |_, route| route.defaults[:action].to_sym if is_member_route?(route) && is_delete_route?(route) }.tap(&:compact!) ) end |
#member_get_actions ⇒ Object
GET actions
142 143 144 145 146 |
# File 'app/models/effective/resources/actions.rb', line 142 def member_get_actions @member_get_actions ||= ( routes.map { |_, route| route.defaults[:action].to_sym if is_member_route?(route) && is_get_route?(route) }.tap(&:compact!) ) end |
#member_post_actions ⇒ Object
POST/PUT/PATCH actions
155 156 157 158 159 |
# File 'app/models/effective/resources/actions.rb', line 155 def member_post_actions @member_post_actions ||= ( routes.map { |_, route| route.defaults[:action].to_sym if is_member_route?(route) && is_post_route?(route) }.tap(&:compact!) ) end |
#routes ⇒ Object
This was written for the Edit actions fallback templates and Datatables Effective::Resource.new(‘admin/posts’).routes
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'app/models/effective/resources/actions.rb', line 12 def routes @routes ||= ( matches = [ [namespace, plural_name].compact.join('/'), [namespace, name].compact.join('/') ] # Check main Rails app routes = Rails.application.routes.routes.select do |route| (matches & [route.defaults[:controller]]).present? && !route.name.to_s.end_with?('root') end # Check engine routes if routes.blank? matches = [ [namespace, plural_name].compact.join('/'), [namespace, name].compact.join('/'), ['effective', namespace, plural_name].compact.join('/'), ['effective', namespace, name].compact.join('/') ] (Rails::Engine.subclasses.reverse + [Rails.application]).each do |engine| routes = engine.routes.routes.select do |route| (matches & [route.defaults[:controller]]).present? && !route.name.to_s.end_with?('root') end break if routes.present? end end Array(routes).inject({}) { |h, route| h[route.defaults[:action].to_sym] = route; h } ) end |