Class: ActiveScaffold::DataStructures::ActionColumns

Inherits:
Set
  • Object
show all
Includes:
Configurable
Defined in:
lib/active_scaffold/data_structures/action_columns.rb

Overview

A set of columns. These structures can be nested for organization.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Configurable

#configure, #method_missing, #respond_to_missing?

Methods inherited from Set

#add, #each, #empty?, #exclude, #find_by_name, #find_by_names, #initialize, #initialize_dup, #set_values

Constructor Details

This class inherits a constructor from ActiveScaffold::DataStructures::Set

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class ActiveScaffold::Configurable

Instance Attribute Details

#actionObject

this lets us refer back to the action responsible for this link, if it exists. the immediate need here is to get the crud_type so we can dynamically filter columns from the set.



8
9
10
# File 'lib/active_scaffold/data_structures/action_columns.rb', line 8

def action
  @action
end

#collapsedObject

Whether this column set is collapsed by default in contexts where collapsing is supported



35
36
37
# File 'lib/active_scaffold/data_structures/action_columns.rb', line 35

def collapsed
  @collapsed
end

#labelObject



12
13
14
# File 'lib/active_scaffold/data_structures/action_columns.rb', line 12

def label
  as_(@label) if @label
end

Instance Method Details

#==(other) ⇒ Object

this is so that array.delete and array.include?, etc., will work by column name



25
26
27
28
29
30
31
32
# File 'lib/active_scaffold/data_structures/action_columns.rb', line 25

def ==(other) #:nodoc:
  # another ActionColumns
  if other.class == self.class
    label == other.label
  else
    @label.to_s == other.to_s
  end
end

#action_nameObject



101
102
103
# File 'lib/active_scaffold/data_structures/action_columns.rb', line 101

def action_name
  @action.
end

#add_subgroup(label, &proc) ⇒ Object

nests a subgroup in the column set



38
39
40
41
42
43
44
45
# File 'lib/active_scaffold/data_structures/action_columns.rb', line 38

def add_subgroup(label, &proc)
  columns = ActiveScaffold::DataStructures::ActionColumns.new
  columns.label = label
  columns.action = action
  columns.configure(&proc)
  exclude columns.collect_columns
  add columns
end

#columns_keyObject



105
106
107
# File 'lib/active_scaffold/data_structures/action_columns.rb', line 105

def columns_key
  "#{@action.core.model_id.to_s.underscore}-#{action_name}"
end

#constraint_columnsObject



113
114
115
# File 'lib/active_scaffold/data_structures/action_columns.rb', line 113

def constraint_columns
  ActiveScaffold::Registry.constraint_columns[columns_key]
end

#constraint_columns=(columns) ⇒ Object



109
110
111
# File 'lib/active_scaffold/data_structures/action_columns.rb', line 109

def constraint_columns=(columns)
  ActiveScaffold::Registry.constraint_columns[columns_key] = columns
end

#css_classObject



20
21
22
# File 'lib/active_scaffold/data_structures/action_columns.rb', line 20

def css_class
  @label.to_s.underscore.gsub(/[^-_0-9a-zA-Z]/, '-')
end

#each_column(options = {}, &proc) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/active_scaffold/data_structures/action_columns.rb', line 70

def each_column(options = {}, &proc)
  columns = options[:core_columns] || action.core.columns
  self.unauthorized_columns = []
  options[:for] ||= columns.active_record_class

  each do |item|
    if item.is_a? ActiveScaffold::DataStructures::ActionColumns
      if options[:flatten]
        item.each_column(options, &proc)
      elsif !options[:skip_groups]
        yield item
      end
    else
      next if skip_column?(item, options)
      yield columns[item] || ActiveScaffold::DataStructures::Column.new(item.to_sym, columns.active_record_class)
    end
  end
end

#include?(item) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
50
51
52
53
# File 'lib/active_scaffold/data_structures/action_columns.rb', line 47

def include?(item)
  @set.each do |c|
    return true if !c.is_a?(Symbol) && c.include?(item)
    return true if c == item.to_sym
  end
  false
end

#lengthObject



125
126
127
# File 'lib/active_scaffold/data_structures/action_columns.rb', line 125

def length
  ((@set - constraint_columns) - unauthorized_columns).length
end

#nameObject



16
17
18
# File 'lib/active_scaffold/data_structures/action_columns.rb', line 16

def name
  @label.to_s.underscore
end

#skip_column?(column_name, options) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
62
63
64
65
66
67
68
# File 'lib/active_scaffold/data_structures/action_columns.rb', line 59

def skip_column?(column_name, options)
  # skip if this matches a constrained column
  return true if constraint_columns.include?(column_name.to_sym)
  # skip this field if it's not authorized
  unless options[:for].authorized_for?(action: options[:action], crud_type: options[:crud_type] || action&.crud_type || :read, column: column_name)
    unauthorized_columns << column_name.to_sym
    return true
  end
  false
end

#to_aObject



55
56
57
# File 'lib/active_scaffold/data_structures/action_columns.rb', line 55

def to_a
  Array(@set)
end

#unauthorized_columnsObject



121
122
123
# File 'lib/active_scaffold/data_structures/action_columns.rb', line 121

def unauthorized_columns
  ActiveScaffold::Registry.unauthorized_columns[columns_key]
end

#unauthorized_columns=(columns) ⇒ Object



117
118
119
# File 'lib/active_scaffold/data_structures/action_columns.rb', line 117

def unauthorized_columns=(columns)
  ActiveScaffold::Registry.unauthorized_columns[columns_key] = columns
end

#visible_columns(options = {}) ⇒ Object



89
90
91
92
93
94
95
# File 'lib/active_scaffold/data_structures/action_columns.rb', line 89

def visible_columns(options = {})
  columns = []
  each_column(options) do |column|
    columns << column
  end
  columns
end

#visible_columns_names(options = {}) ⇒ Object



97
98
99
# File 'lib/active_scaffold/data_structures/action_columns.rb', line 97

def visible_columns_names(options = {})
  visible_columns(options.reverse_merge(flatten: true)).map(&:name)
end