Module: Dscf::Core::Common

Extended by:
ActiveSupport::Concern
Includes:
Filterable, JsonResponse, Pagination
Included in:
AddressesController
Defined in:
app/controllers/concerns/dscf/core/common.rb

Instance Method Summary collapse

Methods included from Filterable

#filter_records

Methods included from JsonResponse

#render_error, #render_success, #serialize

Methods included from Pagination

#default_per_page, #order_by, #order_direction, #page_no, #paginate, #paginate_offset, #pagination_links, #per_page

Instance Method Details

#createObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'app/controllers/concerns/dscf/core/common.rb', line 88

def create
  obj = nil
  options = {}
  if block_given?
    incoming = yield
    if incoming.instance_of?(Array)
      obj, options = incoming
    elsif incoming.instance_of?(Hash)
      obj = @clazz.new(model_params)
      options = incoming
    else
      obj = incoming
    end
  else
    obj = @clazz.new(model_params)
  end

  if obj.save
    obj = @clazz.includes(eager_loaded_associations).find(obj.id) if eager_loaded_associations.present?

    includes = serializer_includes_for_action(:create)
    options[:include] = includes if includes.present?

    render_success(data: obj, serializer_options: options, status: :created)
  else
    render_error(errors: obj.errors.full_messages[0], status: :unprocessable_entity)
  end
rescue StandardError => e
  render_error(error: e.message)
end

#indexObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/controllers/concerns/dscf/core/common.rb', line 14

def index
  data = nil
  options = {}
  if block_given?
    incoming = yield
    if incoming.instance_of?(Array)
      data, options = incoming
    elsif incoming.instance_of?(Hash)
      options = incoming
    else
      data = incoming
    end
  else
    data = @clazz.all
  end

  # Apply eager loading if defined
  data = data.includes(eager_loaded_associations) if eager_loaded_associations.present?

  # Apply Ransack filtering
  data = filter_records(data)

  # Get total count before pagination
  total_count = data.count if params[:page]

  data = data.then(&paginate) if params[:page]

  # Add action specific serializer includes
  includes = serializer_includes_for_action(:index)
  options[:include] = includes if includes.present?

  # Add pagination metadata if paginated
  if params[:page]
    total_pages = (total_count.to_f / per_page).ceil
    count = data.is_a?(Array) ? data.length : data.count

    options[:pagination] = {
      current_page: page_no,
      per_page: per_page,
      count: count,
      total_count: total_count,
      total_pages: total_pages,
      links: pagination_links(total_pages)
    }
  end

  render_success(data: data, serializer_options: options)
end

#showObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'app/controllers/concerns/dscf/core/common.rb', line 63

def show
  data = nil
  options = {}
  if block_given?
    incoming = yield
    if incoming.instance_of?(Array)
      data, options = incoming
    elsif incoming.instance_of?(Hash)
      data = @obj
      options = incoming
    else
      data = incoming
    end
  else
    data = @obj
  end

  data = @clazz.includes(eager_loaded_associations).find(params[:id]) if data.is_a?(@clazz) && eager_loaded_associations.present?

  includes = serializer_includes_for_action(:show)
  options[:include] = includes if includes.present?

  render_success(data: data, serializer_options: options)
end

#updateObject



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'app/controllers/concerns/dscf/core/common.rb', line 119

def update
  obj = nil
  options = {}
  if block_given?
    incoming = yield
    if incoming.instance_of?(Array)
      obj, options = incoming
    elsif incoming.instance_of?(Hash)
      obj = set_object
      options = incoming
    else
      obj = incoming
    end
  else
    obj = set_object
  end

  if obj.update(model_params)
    obj = @clazz.includes(eager_loaded_associations).find(obj.id) if eager_loaded_associations.present?

    includes = serializer_includes_for_action(:update)
    options[:include] = includes if includes.present?

    render_success(data: obj, serializer_options: options)
  else
    render_error(errors: obj.errors.full_messages[0], status: :unprocessable_entity)
  end
rescue StandardError => e
  render_error(error: e.message)
end