Class: ActiveAdmin::Favorites::ViewLens::Layout

Inherits:
Object
  • Object
show all
Defined in:
app/models/active_admin/favorites/view_lens/layout.rb

Constant Summary collapse

VERSION =
1

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_layout) ⇒ Layout

Returns a new instance of Layout.



28
29
30
# File 'app/models/active_admin/favorites/view_lens/layout.rb', line 28

def initialize(raw_layout)
  @layout = self.class.normalize(raw_layout)
end

Instance Attribute Details

#layoutObject (readonly)

Returns the value of attribute layout.



32
33
34
# File 'app/models/active_admin/favorites/view_lens/layout.rb', line 32

def layout
  @layout
end

Class Method Details

.emptyObject



24
25
26
# File 'app/models/active_admin/favorites/view_lens/layout.rb', line 24

def self.empty
  new({})
end

.normalize(raw_layout) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'app/models/active_admin/favorites/view_lens/layout.rb', line 9

def self.normalize(raw_layout)
  layout = raw_layout.is_a?(Hash) ? raw_layout.deep_stringify_keys : {}
  hidden = layout.fetch("hidden", {})
  {
    "version" => layout.fetch("version", VERSION),
    "hidden" => {
      "columns" => Array(hidden["columns"]).map(&:to_s),
      "filters" => Array(hidden["filters"]).map(&:to_s),
      "action_items" => Array(hidden["action_items"]).map(&:to_s),
      "panels" => Array(hidden["panels"]).map(&:to_s),
      "show_rows" => Array(hidden["show_rows"]).map(&:to_s)
    }
  }
end

Instance Method Details

#hidden_action_item?(action_name) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
# File 'app/models/active_admin/favorites/view_lens/layout.rb', line 62

def hidden_action_item?(action_name)
  action_name.present? && hidden_action_items.include?(action_name.to_s)
end

#hidden_action_itemsObject



42
43
44
# File 'app/models/active_admin/favorites/view_lens/layout.rb', line 42

def hidden_action_items
  @layout.dig("hidden", "action_items")
end

#hidden_column?(column_id) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
# File 'app/models/active_admin/favorites/view_lens/layout.rb', line 54

def hidden_column?(column_id)
  column_id.present? && hidden_columns.include?(column_id.to_s)
end

#hidden_columnsObject



34
35
36
# File 'app/models/active_admin/favorites/view_lens/layout.rb', line 34

def hidden_columns
  @layout.dig("hidden", "columns")
end

#hidden_filter?(filter_id) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
# File 'app/models/active_admin/favorites/view_lens/layout.rb', line 58

def hidden_filter?(filter_id)
  filter_id.present? && hidden_filters.include?(filter_id.to_s)
end

#hidden_filtersObject



38
39
40
# File 'app/models/active_admin/favorites/view_lens/layout.rb', line 38

def hidden_filters
  @layout.dig("hidden", "filters")
end

#hidden_panel?(panel_id) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
# File 'app/models/active_admin/favorites/view_lens/layout.rb', line 66

def hidden_panel?(panel_id)
  panel_id.present? && hidden_panels.include?(panel_id.to_s)
end

#hidden_panelsObject



46
47
48
# File 'app/models/active_admin/favorites/view_lens/layout.rb', line 46

def hidden_panels
  @layout.dig("hidden", "panels")
end

#hidden_show_row?(row_id) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
# File 'app/models/active_admin/favorites/view_lens/layout.rb', line 70

def hidden_show_row?(row_id)
  row_id.present? && hidden_show_rows.include?(row_id.to_s)
end

#hidden_show_rowsObject



50
51
52
# File 'app/models/active_admin/favorites/view_lens/layout.rb', line 50

def hidden_show_rows
  @layout.dig("hidden", "show_rows")
end

#merge(other) ⇒ Object



74
75
76
77
78
79
80
81
82
# File 'app/models/active_admin/favorites/view_lens/layout.rb', line 74

def merge(other)
  return self if other.blank?

  other_layout = self.class.normalize(other.layout || other)
  merged_hidden = layout.fetch("hidden", {}).merge(other_layout.fetch("hidden", {})) do |_key, left, right|
    (left + right).uniq
  end
  self.class.new("version" => VERSION, "hidden" => merged_hidden)
end