Class: ActiveAdmin::Favorites::ViewLens::Applicator

Inherits:
Object
  • Object
show all
Defined in:
lib/activeadmin/favorites/view_lens/applicator.rb

Class Method Summary collapse

Class Method Details

.build_catalog(active_admin_config:, action:, render_context: nil) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/activeadmin/favorites/view_lens/applicator.rb', line 71

def build_catalog(active_admin_config:, action:, render_context: nil)
  Catalog.for(
    active_admin_config: active_admin_config,
    action: action,
    render_context: render_context
  )
end

.catalog_payload(active_admin_config:, action:, render_context: nil) ⇒ Object



79
80
81
82
83
84
85
86
87
# File 'lib/activeadmin/favorites/view_lens/applicator.rb', line 79

def catalog_payload(active_admin_config:, action:, render_context: nil)
  build_catalog(
    active_admin_config: active_admin_config,
    action: action,
    render_context: render_context
  ).map do |entry|
    {id: entry.id, label: entry.label, group: entry.group}
  end
end

.column_label_from_args(args, column_id:) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/activeadmin/favorites/view_lens/applicator.rb', line 118

def column_label_from_args(args, column_id:)
  title = args[0]
  options = args.last.is_a?(Hash) ? args.last : {}
  return options[:label].to_s if options[:label].present?
  return title.to_s.humanize if title.is_a?(Symbol)
  return title if title.is_a?(String) && title.present?

  if column_id.to_s.start_with?("block_column_")
    position = column_id.to_s.delete_prefix("block_column_")
    return I18n.t("active_admin.favorites.block_column", position: position, default: "Custom column %{position}")
  end

  column_id.to_s.humanize
end

.current_catalogObject



20
21
22
# File 'lib/activeadmin/favorites/view_lens/applicator.rb', line 20

def current_catalog
  Current.catalog
end

.current_catalog=(value) ⇒ Object



24
25
26
# File 'lib/activeadmin/favorites/view_lens/applicator.rb', line 24

def current_catalog=(value)
  Current.catalog = value
end

.current_layoutObject



8
9
10
11
12
13
14
# File 'lib/activeadmin/favorites/view_lens/applicator.rb', line 8

def current_layout
  value = Current.layout
  return Layout.empty if value.nil?
  return value if value.is_a?(Layout)

  Layout.new(value)
end

.current_layout=(value) ⇒ Object



16
17
18
# File 'lib/activeadmin/favorites/view_lens/applicator.rb', line 16

def current_layout=(value)
  Current.layout = value
end

.extract_column_id(args, resource_key: nil) ⇒ Object



107
108
109
110
111
112
113
114
115
116
# File 'lib/activeadmin/favorites/view_lens/applicator.rb', line 107

def extract_column_id(args, resource_key: nil)
  title = args[0]
  return title.to_s if title.is_a?(Symbol)
  return title.to_s.parameterize(separator: "_") if title.is_a?(String) && title.present?

  options = args.last.is_a?(Hash) ? args.last : {}
  return options[:lens_id].to_s if options[:lens_id].present?

  next_block_column_id(resource_key) if resource_key.present?
end

.extract_panel_id(title, options) ⇒ Object



139
140
141
# File 'lib/activeadmin/favorites/view_lens/applicator.rb', line 139

def extract_panel_id(title, options)
  options[:lens_id]&.to_s.presence || title.to_s.parameterize(separator: "_")
end

.extract_row_id(title, options) ⇒ Object



143
144
145
# File 'lib/activeadmin/favorites/view_lens/applicator.rb', line 143

def extract_row_id(title, options)
  options[:lens_id]&.to_s.presence || title.to_s.parameterize(separator: "_")
end

.next_block_column_id(resource_key) ⇒ Object



133
134
135
136
137
# File 'lib/activeadmin/favorites/view_lens/applicator.rb', line 133

def next_block_column_id(resource_key)
  sequence = Current.block_column_sequence
  sequence[resource_key] += 1
  "block_column_#{sequence[resource_key]}"
end

.register_column(resource_key:, id:, label:) ⇒ Object



89
90
91
92
93
# File 'lib/activeadmin/favorites/view_lens/applicator.rb', line 89

def register_column(resource_key:, id:, label:)
  return if id.blank?

  registered_columns[resource_key] << {id: id.to_s, label: label.to_s}
end

.register_panel(resource_key:, id:, label:) ⇒ Object



95
96
97
98
99
# File 'lib/activeadmin/favorites/view_lens/applicator.rb', line 95

def register_panel(resource_key:, id:, label:)
  return if id.blank?

  registered_panels[resource_key] << {id: id.to_s, label: label.to_s}
end

.register_row(resource_key:, id:, label:) ⇒ Object



101
102
103
104
105
# File 'lib/activeadmin/favorites/view_lens/applicator.rb', line 101

def register_row(resource_key:, id:, label:)
  return if id.blank?

  registered_rows[resource_key] << {id: id.to_s, label: label.to_s}
end

.registered_columnsObject



28
29
30
# File 'lib/activeadmin/favorites/view_lens/applicator.rb', line 28

def registered_columns
  Current.registered_columns
end

.registered_panelsObject



32
33
34
# File 'lib/activeadmin/favorites/view_lens/applicator.rb', line 32

def registered_panels
  Current.registered_panels
end

.registered_rowsObject



36
37
38
# File 'lib/activeadmin/favorites/view_lens/applicator.rb', line 36

def registered_rows
  Current.registered_rows
end

.reset!Object



40
41
42
# File 'lib/activeadmin/favorites/view_lens/applicator.rb', line 40

def reset!
  Current.reset
end

.resolve_layout(controller:) ⇒ Object



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
# File 'lib/activeadmin/favorites/view_lens/applicator.rb', line 44

def resolve_layout(controller:)
  user = controller.send(ActiveAdmin::Favorites.config.current_user_method)
  config = controller.send(:active_admin_config)
  action = controller.action_name
  resource_key = config.resource_name.route_key

  layout = ViewLens::Layout.empty
  default = ViewLensDefault.for_user(user, resource_key: resource_key, action: action)
  layout = layout.merge(default.layout_object) if default

  favorite_id = controller.params[:favorite_id]
  if favorite_id.present?
    bookmark = Favorite.visible_to(user).find_by(id: favorite_id)
    if bookmark&.layout.present? && bookmark.resource_key == resource_key && bookmark.action == action
      layout = layout.merge(bookmark.layout_object)
    end
  end

  Current.layout = layout
  Current.catalog_context = {
    active_admin_config: config,
    action: action,
    render_context: controller.helpers
  }
  layout
end