Sha256: 8b167b8de2df103b8808e10f927fe8e7b4aa6aa3d017a38aa8766674eb9f31bf

Contents?: true

Size: 922 Bytes

Versions: 6

Compression:

Stored size: 922 Bytes

Contents

module IntrospectiveGrape
  module CreateHelpers
    def add_new_records_to_root_record(dsl, routes, params, model)
      dsl.send(:authorize, model, :create?)
      ActiveRecord::Base.transaction do
        old = find_leaves(routes, model, params)
        model.update_attributes( dsl.send(:safe_params, params).permit(whitelist) )
        new = find_leaves(routes, model, params)
        old.respond_to?(:size) ? new - old : new
      end
    end

    def create_new_record(dsl, routes, params)
      model = routes.first.model.new( dsl.send(:safe_params, params).permit(whitelist) )
      dsl.send(:authorize, model, :create?)
      model.save!

      # reload the model with eager loading
      default_includes = routes.first.klass.default_includes(routes.first.model)
      model = model.class.includes(default_includes).find(model.id) if model.persisted?

      find_leaves(routes, model, params)
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
introspective_grape-0.5.7 lib/introspective_grape/create_helpers.rb
introspective_grape-0.5.6 lib/introspective_grape/create_helpers.rb
introspective_grape-0.5.5 lib/introspective_grape/create_helpers.rb
introspective_grape-0.5.4 lib/introspective_grape/create_helpers.rb
introspective_grape-0.5.2 lib/introspective_grape/create_helpers.rb
introspective_grape-0.5.0 lib/introspective_grape/create_helpers.rb