Sha256: c6a94bb437fe508941e8580e6a10705d611fdf00edfa2973ce4f89698c58df33

Contents?: true

Size: 1.83 KB

Versions: 13

Compression:

Stored size: 1.83 KB

Contents

module ForestLiana
  class ResourcesController < ForestLiana::ApplicationController
    begin
      prepend ResourcesExtensions
    rescue NameError
    end

    before_filter :find_resource

    def index
      getter = ResourcesGetter.new(@resource, params)
      getter.perform

      render json: serialize_models(getter.records,
                                    include: includes,
                                    count: getter.count,
                                    params: params)
    end

    def show
      getter = ResourceGetter.new(@resource, params)
      getter.perform

      render json: serialize_model(getter.record, include: includes)
    end

    def create
      if Rails::VERSION::MAJOR == 4
        record = @resource.create!(resource_params.permit!)
      else
        record = @resource.create!(resource_params, without_protection: true)
      end

      render json: serialize_model(record, include: includes)
    end

    def update
      getter = ResourceUpdater.new(@resource, params)
      getter.perform

      render json: serialize_model(getter.record, include: includes)
    end

    def destroy
      @resource.destroy_all(id: params[:id])
      render nothing: true, status: 204
    end

    private

    def find_resource
      @resource = SchemaUtils.find_model_from_table_name(params[:collection])

      if @resource.nil? || !@resource.ancestors.include?(ActiveRecord::Base)
        render json: {status: 404}, status: :not_found
      end
    end

    def resource_params
      ResourceDeserializer.new(@resource, params[:resource]).perform
    end

    def includes
      @resource
        .reflect_on_all_associations
        .select do |a|
          [:belongs_to, :has_and_belongs_to_many, :has_one]
            .include?(a.macro) && !a.options[:polymorphic]
        end
        .map {|a| a.name.to_s }
    end

  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
forest_liana-1.1.28 app/controllers/forest_liana/resources_controller.rb
forest_liana-1.1.27 app/controllers/forest_liana/resources_controller.rb
forest_liana-1.1.26 app/controllers/forest_liana/resources_controller.rb
forest_liana-1.1.25 app/controllers/forest_liana/resources_controller.rb
forest_liana-1.1.23 app/controllers/forest_liana/resources_controller.rb
forest_liana-1.1.22 app/controllers/forest_liana/resources_controller.rb
forest_liana-1.1.20 app/controllers/forest_liana/resources_controller.rb
forest_liana-1.1.19 app/controllers/forest_liana/resources_controller.rb
forest_liana-1.1.18 app/controllers/forest_liana/resources_controller.rb
forest_liana-1.1.17 app/controllers/forest_liana/resources_controller.rb
forest_liana-1.1.16 app/controllers/forest_liana/resources_controller.rb
forest_liana-1.1.15 app/controllers/forest_liana/resources_controller.rb
forest_liana-1.1.14 app/controllers/forest_liana/resources_controller.rb