Sha256: 4618137f42f3bf144b36a4ac17351c7bdea9d61ca4d3dc7dc23d2644a16dae64

Contents?: true

Size: 1.71 KB

Versions: 3

Compression:

Stored size: 1.71 KB

Contents

module JsonapiSwaggerHelpers
  module ResourceMixin

    def jsonapi_resource(base_path,
                         tags: [],
                         descriptions: {},
                         only: [],
                         except: [],
                         singular: false)
      self.resources << {
        base_path: base_path,
        tags: tags,
        descriptions: descriptions,
        only: only,
        except: except,
        singular: singular
      }
    end

    def load_resource(config)
      base_path = config[:base_path]
      tags = config[:tags]
      descriptions = config[:descriptions]
      only = config[:only]
      except = config[:except]
      singular = config[:singular]

      actions = %i[index show create update destroy]
      actions.select! { |a| only.include?(a) } unless only.empty?
      actions.reject! { |a| except.include?(a) } unless except.empty?

      prefix     = @swagger_root_node.data[:basePath]
      full_path  = [prefix, base_path].join('/').gsub('//', '/')
      controller = JsonapiSwaggerHelpers::Util.controller_for(full_path)

      actions.each do |action|
        next unless controller.action_methods.include?(action.to_s)
        path = if !singular && %i[show update destroy].include?(action)
                 "#{base_path}/{id}"
               else
                 base_path
               end
        swagger_path path do
          action_class_name = "#{action.to_s.camelize}Action"
          action_class = JsonapiSwaggerHelpers.const_get(action_class_name)
          action_object = action_class.new \
            self, controller, tags: tags, description: descriptions[action], singular: singular
          action_object.generate
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
jsonapi_swagger_helpers-0.6.6 lib/jsonapi_swagger_helpers/resource_mixin.rb
jsonapi_swagger_helpers-0.6.5 lib/jsonapi_swagger_helpers/resource_mixin.rb
jsonapi_swagger_helpers-0.6.4 lib/jsonapi_swagger_helpers/resource_mixin.rb