Module: IntrospectiveGrape::CreateHelpers

Included in:
API
Defined in:
lib/introspective_grape/create_helpers.rb

Instance Method Summary collapse

Instance Method Details

#add_new_records_to_root_record(dsl, routes, params, model) ⇒ Object



4
5
6
7
8
9
10
11
12
# File 'lib/introspective_grape/create_helpers.rb', line 4

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

#create_new_record(dsl, routes, params) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/introspective_grape/create_helpers.rb', line 14

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