forked from hitobito/hitobito
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path.pryrc
More file actions
76 lines (67 loc) · 2.8 KB
/
Copy path.pryrc
File metadata and controls
76 lines (67 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# frozen_string_literal: true
# Copyright (c) 2023, Schweizer Alpen-Club. This file is part of
# hitobito_sac_cas and licensed under the Affero General Public License version 3
# or later. See the COPYING file at the top-level directory or at
# https://github.com/hitobito/hitobito_sac_cas.
# Lists callbacks
#
# Example:
# list_callbacks(Person)
# list_callbacks(PeopleController)
#
# rubocop:todo Metrics/CyclomaticComplexity
def list_callbacks(klass, skip_procs: true, skip_validations: true)
klass.__callbacks.each_with_object(Hash.new { [] }) do |(k, callbacks), result|
next if skip_validations && k == :validate # ignore validations
callbacks.each do |c|
next if skip_procs && c.filter.is_a?(Proc)
# remove autosaving callbacks from result
next if c.filter.to_s.include?("autosave")
next if c.filter.to_s.include?("_ensure_no_duplicate_errors")
result["#{c.kind}_#{c.name}"] += [c.filter]
end.compact_blank
end
end
# rubocop:enable Metrics/CyclomaticComplexity
def gr(resource, scope: nil, params: {}, ability: nil, as: Role.first.person)
raise "#{resource} is not a resource class" unless resource <= ApplicationResource
context = OpenStruct.new(current_ability: ability || Ability.new(as))
Graphiti.with_context(context, params: params) do
action_controller_params = ActionController::Parameters.new(params)
resources = resource.all(action_controller_params, scope || resource.new.base_scope)
JSON.parse(resources.to_jsonapi).deep_symbolize_keys
end
end
# Setup PaperTrail metadata for the current pry session. If a block is given, the PaperTrail config
# is reverted to the previous state after the block is executed.
#
# Example:
# with_papertrail_metadata do
# user.update!(name: 'foo')
# end
#
# or with the alias `pt`:
# pt { user.update!(name: 'foo') }
def with_papertrail_metadata(whodunnit: "pry", mutation_id: "pry-#{SecureRandom.uuid}")
controller_info = {mutation_id:}
if whodunnit.is_a?(ActiveRecord::Base)
controller_info[:whodunnit_type] = whodunnit.class.sti_name
whodunnit = whodunnit.id
end
if block_given?
PaperTrail.request(whodunnit:, controller_info:) { yield }
else
PaperTrail.request.whodunnit = whodunnit
PaperTrail.request.controller_info = controller_info
end
end
alias pt with_papertrail_metadata # rubocop:disable Style/Alias (alias_method is not available yet)
# When pry is started from the rails console. We set-up the PaperTrail metadata so that we can
# track all changes made during the console session.
if Rails.const_defined?(:Console)
with_papertrail_metadata
end
def form_for(model, name: model.class.table_name.singularize, options: {})
ActionController::Base.helpers.extend(UtilityHelper)
StandardFormBuilder.new(model.class.name, model, ActionController::Base.helpers, options)
end