Sha256: c1ebaae0b63054f2e560fd76407492585445432725b0ee632569725d23692fc8

Contents?: true

Size: 1.92 KB

Versions: 4

Compression:

Stored size: 1.92 KB

Contents

module ForestLiana
  class AssociationsController < ForestLiana::ApplicationController
    if Rails::VERSION::MAJOR < 4
      before_filter :find_resource
      before_filter :find_association
    else
      before_action :find_resource
      before_action :find_association
    end

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

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

    def update
      updater = BelongsToUpdater.new(@resource, @association, params)
      updater.perform

      head :no_content
    end

    def associate
      associator = HasManyAssociator.new(@resource, @association, params)
      associator.perform

      head :no_content
    end

    def dissociate
      dissociator = HasManyDissociator.new(@resource, @association, params)
      dissociator.perform

      head :no_content
    end

    private

    def find_resource
      @resource = SchemaUtils.find_model_from_collection_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) &&
         params[:action] == 'index')
        render serializer: nil, json: {status: 404}, status: :not_found
      end
    end

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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
forest_liana-1.6.11 app/controllers/forest_liana/associations_controller.rb
forest_liana-1.6.10 app/controllers/forest_liana/associations_controller.rb
forest_liana-1.6.7 app/controllers/forest_liana/associations_controller.rb
forest_liana-1.6.6 app/controllers/forest_liana/associations_controller.rb