Sha256: 9d6b57be36462e6e695079d4db9602eecefbc5a40be2fab8f1359135291ec21f

Contents?: true

Size: 1.41 KB

Versions: 12

Compression:

Stored size: 1.41 KB

Contents

module ForestLiana
  class AssociationsController < ForestLiana::ApplicationController

    before_filter :find_resource
    before_filter :find_association

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

      render json: serialize_models(getter.records,
                                    include: includes,
                                    count: getter.count,
                                    params: params)
    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 find_association
      @association = @resource.reflect_on_association(
        params[:association_name])

      # Only accept "many" associations
      if @association.nil? ||
        [:belongs_to, :has_one].include?(@association.macro)
        render json: {status: 404}, status: :not_found
      end
    end

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

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

  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

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