Sha256: a5e90dcc1c5a1770b52f12d48566c45b12602cdbdc7079775db71a067599635d

Contents?: true

Size: 1.35 KB

Versions: 13

Compression:

Stored size: 1.35 KB

Contents

module Effective
  module Resources
    module Actions

      # This was written for the Edit actions fallback templates

      def controller_routes
        @controller_routes ||= (
          path = controller_path

          Rails.application.routes.routes.select do |route|
            (route.defaults[:controller] == path) && route.defaults[:action].present?
          end
        )
      end

      def controller_actions
        controller_routes.map { |route| route.defaults[:action] }
      end

      # GET actions
      def member_actions
        controller_routes.map { |route| route.defaults[:action] if is_get_member?(route) }.compact - crud_actions
      end

      # POST/PUT/PATCH actions
      def member_post_actions
        controller_routes.map { |route| route.defaults[:action] if is_post_member?(route) }.compact - crud_actions
      end

      # Same as controller_path in the view
      def controller_path
        [namespace, plural_name].compact * '/'
      end

      private

      def crud_actions
        %w(index new create show edit update destroy)
      end

      def is_get_member?(route)
        route.verb.to_s.include?('GET') && route.path.required_names == ['id']
      end

      def is_post_member?(route)
        ['POST', 'PUT', 'PATCH'].any? { |verb| route.verb == verb } && route.path.required_names == ['id']
      end

    end
  end
end



Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
effective_resources-0.6.9 app/models/effective/resources/actions.rb
effective_resources-0.6.8 app/models/effective/resources/actions.rb
effective_resources-0.6.7 app/models/effective/resources/actions.rb
effective_resources-0.6.6 app/models/effective/resources/actions.rb
effective_resources-0.6.5 app/models/effective/resources/actions.rb
effective_resources-0.6.4 app/models/effective/resources/actions.rb
effective_resources-0.6.3 app/models/effective/resources/actions.rb
effective_resources-0.6.2 app/models/effective/resources/actions.rb
effective_resources-0.6.1 app/models/effective/resources/actions.rb
effective_resources-0.6.0 app/models/effective/resources/actions.rb
effective_resources-0.5.1 app/models/effective/resources/actions.rb
effective_resources-0.5.0 app/models/effective/resources/actions.rb
effective_resources-0.4.10 app/models/effective/resources/actions.rb