Module: Dscf::Core::Common

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

Instance Method Summary collapse

Methods included from Authorizable

#authorize, #authorize_action!, #policy_scope, #pundit_user

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



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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'app/controllers/concerns/dscf/core/common.rb', line 93

def create
  authorize @clazz.new, :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

  # Wrap in transaction for atomic file upload support
  ActiveRecord::Base.transaction do
    if obj.save
      # Store in instance variable for auditing and other concerns
      @obj = obj

      # Hook for optional post-save operations (e.g., file attachments)
      # If file upload fails in strict mode, this raises FileUploadError
      after_save_hook(obj) if respond_to?(:after_save_hook, true)

      obj = @clazz.includes(eager_loaded_associations).find(obj.id) if eager_loaded_associations.present?
      @obj = obj # Update with reloaded version

      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
  end
rescue ::Pundit::NotAuthorizedError
  raise
rescue Dscf::Core::FileUploadError => e
  render_error(errors: e.message, status: :unprocessable_entity)
rescue StandardError => e
  render_error(error: e.message)
end

#indexObject



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
62
63
64
# File 'app/controllers/concerns/dscf/core/common.rb', line 15

def index
  authorize @clazz.new, :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 = policy_scope(@clazz)
  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



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

def show
  authorize @obj

  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



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'app/controllers/concerns/dscf/core/common.rb', line 141

def update
  authorize @obj

  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

  # Wrap in transaction for atomic file upload support
  ActiveRecord::Base.transaction do
    if obj.update(model_params)
      # Hook for optional post-update operations (e.g., file attachments)
      # If file upload fails in strict mode, this raises FileUploadError
      after_save_hook(obj) if respond_to?(:after_save_hook, true)

      obj = @clazz.includes(eager_loaded_associations).find(obj.id) if eager_loaded_associations.present?
      @obj = obj # Update with reloaded version for auditing

      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
  end
rescue ::Pundit::NotAuthorizedError
  raise
rescue Dscf::Core::FileUploadError => e
  render_error(errors: e.message, status: :unprocessable_entity)
rescue StandardError => e
  render_error(error: e.message)
end