Class: Wallaby::ActiveRecord::ModelOperator

Inherits:
ModelOperator show all
Defined in:
lib/adaptors/wallaby/active_record/model_operator.rb

Defined Under Namespace

Classes: Normalizer, Permitter, Querier, Validator

Instance Method Summary collapse

Methods inherited from ModelOperator

#initialize

Constructor Details

This class inherits a constructor from Wallaby::ModelOperator

Instance Method Details

#collection(params, ability) ⇒ Object



2
3
4
5
6
# File 'lib/adaptors/wallaby/active_record/model_operator.rb', line 2

def collection(params, ability)
  query = querier.search params
  query = query.order params[:sort] if params[:sort].present?
  query.accessible_by ability
end

#create(params, ability) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/adaptors/wallaby/active_record/model_operator.rb', line 19

def create(params, ability)
  resource = @model_class.new
  resource.assign_attributes normalize permit(params)
  ensure_attributes_for ability, :create, resource
  resource.save if valid? resource
  [ resource, resource.errors.blank? ]
rescue ActiveRecord::StatementInvalid => e
  resource.errors.add :base, e.message
  [ resource, false ]
end

#destroy(resource, params) ⇒ Object



40
41
42
# File 'lib/adaptors/wallaby/active_record/model_operator.rb', line 40

def destroy(resource, params)
  resource.destroy
end

#find(id, params) ⇒ Object



13
14
15
16
17
# File 'lib/adaptors/wallaby/active_record/model_operator.rb', line 13

def find(id, params)
  @model_class.find id
rescue ActiveRecord::RecordNotFound
  fail Wallaby::ResourceNotFound
end

#new(params) ⇒ Object



8
9
10
11
# File 'lib/adaptors/wallaby/active_record/model_operator.rb', line 8

def new(params)
  permitted = permit params rescue {}
  @model_class.new permitted
end

#update(resource, params, ability) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/adaptors/wallaby/active_record/model_operator.rb', line 30

def update(resource, params, ability)
  resource.assign_attributes normalize permit(params)
  ensure_attributes_for ability, :update, resource
  resource.save if valid? resource
  [ resource, resource.errors.blank? ]
rescue ActiveRecord::StatementInvalid => e
  resource.errors.add :base, e.message
  [ resource, false ]
end