Class: SnFoil::Controller::API

Inherits:
Base
  • Object
show all
Defined in:
lib/sn_foil/controller/api.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Concerns::UpdateControllerConcern

#process_update, #render_update, #update

Methods included from Concerns::ShowControllerConcern

#process_show, #setup_show, #show

Methods included from Concerns::IndexControllerConcern

#index, #meta, #page, #paginate, #per_page, #process_index, #setup_index

Methods included from Concerns::DestroyControllerConcern

#destroy, #process_destroy, #setup_destroy

Methods included from Concerns::CreateControllerConcern

#create, #process_create, #render_create

Class Attribute Details

.i_deserializerObject (readonly)

Returns the value of attribute i_deserializer.



10
11
12
# File 'lib/sn_foil/controller/api.rb', line 10

def i_deserializer
  @i_deserializer
end

.i_serializerObject (readonly)

Returns the value of attribute i_serializer.



10
11
12
# File 'lib/sn_foil/controller/api.rb', line 10

def i_serializer
  @i_serializer
end

Class Method Details

.deserializer(klass = nil) ⇒ Object



16
17
18
# File 'lib/sn_foil/controller/api.rb', line 16

def deserializer(klass = nil)
  @i_deserializer = klass
end

.serializer(klass = nil) ⇒ Object



12
13
14
# File 'lib/sn_foil/controller/api.rb', line 12

def serializer(klass = nil)
  @i_serializer = klass
end

Instance Method Details

#deserializer(**options) ⇒ Object



25
26
27
# File 'lib/sn_foil/controller/api.rb', line 25

def deserializer(**options)
  options[:deserializer] || self.class.i_deserializer
end

#render_change(model, **options) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/sn_foil/controller/api.rb', line 41

def render_change(model, **options)
  if model.errors.empty?
    params
    render json: serializer(**options).new(model,
                                           **options,
                                           params: (options[:controller_params] || options[:params] || {})
                                                     .merge(current_entity: current_entity)).serializable_hash
  else
    render json: model.errors, status: :unprocessable_entity
  end
end

#render_destroy(model, **_options) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/sn_foil/controller/api.rb', line 53

def render_destroy(model, **_options)
  if model.errors.empty?
    render json: {}, status: :no_content
  else
    render json: model.errors, status: :unprocessable_entity
  end
end

#render_index(results, **options) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/sn_foil/controller/api.rb', line 61

def render_index(results, **options)
  render json: serializer(**options).new(paginate(results, **options),
                                         **options,
                                         params: (options[:controller_params] || options[:params] || {})
                                                   .merge(current_entity: current_entity),
                                         meta: meta(results, **options))
                                    .serializable_hash
end

#render_show(model, **options) ⇒ Object



70
71
72
73
74
75
# File 'lib/sn_foil/controller/api.rb', line 70

def render_show(model, **options)
  render json: serializer(**options).new(model,
                                         **options,
                                         params: (options[:controller_params] || options[:params] || {})
                                                   .merge(current_entity: current_entity)).serializable_hash
end

#serializer(**options) ⇒ Object



21
22
23
# File 'lib/sn_foil/controller/api.rb', line 21

def serializer(**options)
  options[:serializer] || self.class.i_serializer
end

#setup_create(**options) ⇒ Object



33
34
35
# File 'lib/sn_foil/controller/api.rb', line 33

def setup_create(**options)
  super(**options.merge(deserialize: true))
end

#setup_options(**options) ⇒ Object



29
30
31
# File 'lib/sn_foil/controller/api.rb', line 29

def setup_options(**options)
  inject_deserialized_params(super)
end

#setup_update(**options) ⇒ Object



37
38
39
# File 'lib/sn_foil/controller/api.rb', line 37

def setup_update(**options)
  super(**options.merge(deserialize: true))
end