Module: ActiveScaffold::DataStructures::ActionColumns::AfterConfiguration

Defined in:
lib/active_scaffold/data_structures/action_columns.rb

Overview

A package of stuff to add after the configuration block. This is an attempt at making a certain level of functionality inaccessible during configuration, to reduce possible breakage from misuse. The bulk of the package is a means of connecting the referential column set (ActionColumns) with the actual column objects (Columns). This lets us iterate over the set and yield real column objects.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#constraint_columnsObject



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

def constraint_columns
  @constraint_columns ||= []
end

#unauthorized_columnsObject



131
132
133
# File 'lib/active_scaffold/data_structures/action_columns.rb', line 131

def unauthorized_columns
  @unauthorized_columns ||= []
end

Instance Method Details

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



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/active_scaffold/data_structures/action_columns.rb', line 84

def collect_visible(options = {}, &proc)
  columns = []
  options[:for] ||= @columns.active_record_class
  self.unauthorized_columns = []
  @set.each do |item|
    unless item.is_a? ActiveScaffold::DataStructures::ActionColumns
      item = (@columns[item] || ActiveScaffold::DataStructures::Column.new(item.to_sym, @columns.active_record_class))
      next if self.skip_column?(item, options)
    end
    if item.is_a? ActiveScaffold::DataStructures::ActionColumns and options.has_key?(:flatten) and options[:flatten]
      columns = columns + item.collect(options, &proc)
    else
      columns << item
    end
  end
  columns
end

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

Redefine the each method to yield actual Column objects. It will skip constrained and unauthorized columns.

Options:

* :flatten - whether to recursively iterate on nested sets. default is false.
* :for - the record (or class) being iterated over. used for column-level security. default is the class.


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/active_scaffold/data_structures/action_columns.rb', line 68

def each(options = {}, &proc)
  options[:for] ||= @columns.active_record_class
  self.unauthorized_columns = []
  @set.each do |item|
    unless item.is_a? ActiveScaffold::DataStructures::ActionColumns
      item = (@columns[item] || ActiveScaffold::DataStructures::Column.new(item.to_sym, @columns.active_record_class))
      next if self.skip_column?(item, options)
    end
    if item.is_a? ActiveScaffold::DataStructures::ActionColumns and options.has_key?(:flatten) and options[:flatten]
      item.each(options, &proc)
    else
      yield item
    end
  end
end

#lengthObject



135
136
137
# File 'lib/active_scaffold/data_structures/action_columns.rb', line 135

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

#set_columns(columns) ⇒ Object

registers a set of column objects (recursively, for all nested ActionColumns)



117
118
119
120
121
122
123
# File 'lib/active_scaffold/data_structures/action_columns.rb', line 117

def set_columns(columns)
  @columns = columns
  # iterate over @set instead of self to avoid dealing with security queries
  @set.each do |item|
    item.set_columns(columns) if item.respond_to? :set_columns
  end
end

#skip_column?(column, options) ⇒ Boolean

Returns:

  • (Boolean)


102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/active_scaffold/data_structures/action_columns.rb', line 102

def skip_column?(column, options)
  result = false
  # skip if this matches a constrained column
  result = true if constraint_columns.include?(column.name)
  # skip if this matches the field_name of a constrained column
  result = true if column.field_name and constraint_columns.include?(column.field_name)
  # skip this field if it's not authorized
  unless options[:for].authorized_for?(:action => options[:action], :crud_type => options[:crud_type] || self.action.crud_type, :column => column.name)
    self.unauthorized_columns << column.name
    result = true
  end
  return result
end