Class: FlexiAdmin::Models::ContextParams

Inherits:
Object
  • Object
show all
Defined in:
lib/flexi_admin/models/context_params.rb

Constant Summary collapse

MAP =
{
  scope: 'fa_scope',
  page: 'fa_page',
  per_page: 'fa_per_page',
  parent: 'fa_parent',
  frame: 'fa_frame', # target turbolinks frame
  sort: 'fa_sort',
  order: 'fa_order',
  form_disabled: 'fa_form_disabled',
  view: 'fa_view',
  ac_action: 'ac_action', # autocomplete action
  ac_fields: { 'ac_fields': [] }, # autocomplete result fields
  ac_path: 'ac_path' # autocomplete path for opening resource in new window
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ ContextParams

Returns a new instance of ContextParams.



36
37
38
39
40
# File 'lib/flexi_admin/models/context_params.rb', line 36

def initialize(params)
  @params = params.to_h.with_indifferent_access.each_with_object({}) do |(key, value), acc|
    acc[self.class.params_map.invert[key.to_s]] = value
  end.with_indifferent_access
end

Instance Attribute Details

#paramsObject (readonly)

Returns the value of attribute params.



34
35
36
# File 'lib/flexi_admin/models/context_params.rb', line 34

def params
  @params
end

Class Method Details

.params_mapObject



28
29
30
31
32
# File 'lib/flexi_admin/models/context_params.rb', line 28

def self.params_map
  MAP.each_with_object({}) do |(key, value), acc|
    acc[key] = value.is_a?(Hash) ? value.keys.first.to_s : value
  end
end

.permitted_keysObject



24
25
26
# File 'lib/flexi_admin/models/context_params.rb', line 24

def self.permitted_keys
  MAP.keys.map(&:to_sym)
end

.permitted_params_keysObject



20
21
22
# File 'lib/flexi_admin/models/context_params.rb', line 20

def self.permitted_params_keys
  MAP.values
end

Instance Method Details

#[](key) ⇒ Object



42
43
44
# File 'lib/flexi_admin/models/context_params.rb', line 42

def [](key)
  @params[key]
end

#current_viewObject



111
112
113
# File 'lib/flexi_admin/models/context_params.rb', line 111

def current_view
  params[:view]
end

#except(*keys) ⇒ Object



71
72
73
74
75
# File 'lib/flexi_admin/models/context_params.rb', line 71

def except(*keys)
  new_params = @params.dup.except(*keys)
  new_params = new_params.transform_keys { |key| self.class.params_map.invert[key.to_s] }
  self.class.new(new_params)
end

#form_disabledObject



107
108
109
# File 'lib/flexi_admin/models/context_params.rb', line 107

def form_disabled
  params[:form_disabled] || false
end

#frameObject



98
99
100
# File 'lib/flexi_admin/models/context_params.rb', line 98

def frame
  params[:frame] || '_top'
end

#merge(params) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/flexi_admin/models/context_params.rb', line 46

def merge(params)
  new_params = @params.dup.merge(params.to_h.with_indifferent_access)

  if params.keys.any? { |key| !self.class.permitted_keys.include?(key.to_sym) }
    msg = "ContextParams: invalid params: "
    msg += params.keys.select { |key| !self.class.permitted_keys.include?(key.to_sym) }.join(", ")
    Rails.logger.warn msg
  end

  new_params = new_params.transform_keys { |key| self.class.params_map[key.to_sym] || key.to_sym }

  self.class.new(new_params)
end

#orderObject



123
124
125
# File 'lib/flexi_admin/models/context_params.rb', line 123

def order
  params[:order]
end

#pageObject



94
95
96
# File 'lib/flexi_admin/models/context_params.rb', line 94

def page
  params[:page] || 1
end

#paginationObject



115
116
117
# File 'lib/flexi_admin/models/context_params.rb', line 115

def pagination
  { page:, per_page: }
end

#parentObject



86
87
88
# File 'lib/flexi_admin/models/context_params.rb', line 86

def parent
  params[:parent]
end

#per_pageObject



90
91
92
# File 'lib/flexi_admin/models/context_params.rb', line 90

def per_page
  params[:per_page] || 13
end

#scopeObject

Not sure if this is needed



103
104
105
# File 'lib/flexi_admin/models/context_params.rb', line 103

def scope
  params[:scope]
end

#sortObject



119
120
121
# File 'lib/flexi_admin/models/context_params.rb', line 119

def sort
  params[:sort]
end

#to_paramsObject



77
78
79
80
# File 'lib/flexi_admin/models/context_params.rb', line 77

def to_params
  self.class.params_map.map { |k, v| [v, params[k]] }
      .reject { |_k, v| v.blank? }.to_h
end

#to_path(path) ⇒ Object



82
83
84
# File 'lib/flexi_admin/models/context_params.rb', line 82

def to_path(path)
  path + "?" + to_params.to_query
end

#with_parent(parent_instance) ⇒ Object



60
61
62
63
64
# File 'lib/flexi_admin/models/context_params.rb', line 60

def with_parent(parent_instance)
  return self if parent_instance.blank?

  merge(parent: parent_instance.gid_param)
end

#with_parent!(parent_instance) ⇒ Object



66
67
68
69
# File 'lib/flexi_admin/models/context_params.rb', line 66

def with_parent!(parent_instance)
  params[:parent] = parent_instance.gid_param
  self
end