Sha256: 826d9f23af1f1f1976b18b54838a4befd7d5cc3a4be4f12856e1a2802486b02d

Contents?: true

Size: 1.56 KB

Versions: 31

Compression:

Stored size: 1.56 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 serializer: nil, 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 serializer: nil, json: {status: 404}, status: :not_found
      end
    end

    def find_association
      # Rails 3 wants a :sym argument.
      @association = @resource.reflect_on_association(
        params[:association_name].try(:to_sym))

      # Only accept "many" associations
      if @association.nil? ||
        [:belongs_to, :has_one].include?(@association.macro)
        render serializer: nil, 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

31 entries across 31 versions & 1 rubygems

Version Path
forest_liana-1.3.40 app/controllers/forest_liana/associations_controller.rb
forest_liana-1.3.39 app/controllers/forest_liana/associations_controller.rb
forest_liana-1.3.38 app/controllers/forest_liana/associations_controller.rb
forest_liana-1.3.37 app/controllers/forest_liana/associations_controller.rb
forest_liana-1.3.36 app/controllers/forest_liana/associations_controller.rb
forest_liana-1.3.35 app/controllers/forest_liana/associations_controller.rb
forest_liana-1.3.34 app/controllers/forest_liana/associations_controller.rb
forest_liana-1.3.33 app/controllers/forest_liana/associations_controller.rb
forest_liana-1.3.32 app/controllers/forest_liana/associations_controller.rb
forest_liana-1.3.31 app/controllers/forest_liana/associations_controller.rb
forest_liana-1.3.30 app/controllers/forest_liana/associations_controller.rb
forest_liana-1.3.29 app/controllers/forest_liana/associations_controller.rb
forest_liana-1.3.28 app/controllers/forest_liana/associations_controller.rb
forest_liana-1.3.27 app/controllers/forest_liana/associations_controller.rb
forest_liana-1.3.26 app/controllers/forest_liana/associations_controller.rb
forest_liana-1.3.25 app/controllers/forest_liana/associations_controller.rb
forest_liana-1.3.24 app/controllers/forest_liana/associations_controller.rb
forest_liana-1.3.23 app/controllers/forest_liana/associations_controller.rb
forest_liana-1.3.22 app/controllers/forest_liana/associations_controller.rb
forest_liana-1.3.21 app/controllers/forest_liana/associations_controller.rb