Module: Brainstem::Concerns::PresenterDSL::ClassMethods

Defined in:
lib/brainstem/concerns/presenter_dsl.rb

Instance Method Summary collapse

Instance Method Details

#associations(&block) ⇒ Object



38
39
40
# File 'lib/brainstem/concerns/presenter_dsl.rb', line 38

def associations(&block)
  AssociationsBlock.new(configuration, &block)
end

#brainstem_key(key) ⇒ Object



159
160
161
# File 'lib/brainstem/concerns/presenter_dsl.rb', line 159

def brainstem_key(key)
  configuration[:brainstem_key] = key.to_s
end

#conditionals(&block) ⇒ Object



30
31
32
# File 'lib/brainstem/concerns/presenter_dsl.rb', line 30

def conditionals(&block)
  ConditionalsBlock.new(configuration, &block)
end

#default_sort_order(sort_string) ⇒ String #default_sort_orderString

Overloads:

  • #default_sort_order(sort_string) ⇒ String

    Sets a default sort order.

    Parameters:

    • sort_string (String)

      The sort order to apply by default while presenting. The string must contain the name of a sort order that has explicitly been declared using #sort_order. The string may end in :asc or :desc to indicate the default order’s direction.

    Returns:

    • (String)

      The new default sort order.

  • #default_sort_orderString

    Returns The default sort order, or nil if one is not set.

    Returns:

    • (String)

      The default sort order, or nil if one is not set.



82
83
84
85
# File 'lib/brainstem/concerns/presenter_dsl.rb', line 82

def default_sort_order(sort_string = nil)
  configuration[:default_sort_order] = sort_string if sort_string
  configuration[:default_sort_order]
end

#description(str, options = { nodoc: false }) ⇒ Object



46
47
48
# File 'lib/brainstem/concerns/presenter_dsl.rb', line 46

def description(str, options = { nodoc: false })
  configuration[:description] = options.merge(info: str)
end

#documented!Object

Temporary implementation to track controllers that have been documented.



57
58
59
# File 'lib/brainstem/concerns/presenter_dsl.rb', line 57

def documented!
  configuration[:documented] = true
end

#fields(&block) ⇒ Object



34
35
36
# File 'lib/brainstem/concerns/presenter_dsl.rb', line 34

def fields(&block)
  FieldsBlock.new(configuration[:fields], &block)
end

#filter(name, type, options = {}) ⇒ Object #filter(name, type, options = {}) {|scope, arg| ... } ⇒ Object

Overloads:

  • #filter(name, type, options = {}) ⇒ Object

    Parameters:

    • name (Symbol)

      The name of the scope that may be applied as a filter.

    • type (Symbol)

      The type of the value that filter holds.

    Options Hash (options):

    • :default (Object)

      If set, causes this filter to be applied to every request. If the filter accepts parameters, the value given here will be passed to the filter when it is applied.

    • :info (String)

      Docstring for the filter.

    • :items (Array)

      List of assignable values for the filter.

  • #filter(name, type, options = {}) {|scope, arg| ... } ⇒ Object

    Parameters:

    • name (Symbol)

      The filter can be requested using this name.

    Yield Parameters:

    • scope (ActiveRecord::Relation)

      The scope that the filter should use as a base.

    • arg (Object)

      The argument passed when the filter was requested.

    Yield Returns:

    • (ActiveRecord::Relation)

      A new scope that filters the scope that was yielded.



141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/brainstem/concerns/presenter_dsl.rb', line 141

def filter(name, type, options = {}, &block)
  if type.to_s == 'array'
    options[:item_type] = options[:item_type].to_s.presence || DEFAULT_FILTER_DATA_TYPE
  end

  valid_options = %w(default info include_params nodoc items item_type)
  options.select! { |k, v| valid_options.include?(k.to_s) }

  configuration[:filters][name] = options.merge({
    value: (block_given? ? block : nil),
    type: type.to_s,
  })
end

#helper(mod = nil, &block) ⇒ Object

Declare a helper module or block whose methods will be available in dynamic fields and associations.



62
63
64
65
66
67
68
69
70
# File 'lib/brainstem/concerns/presenter_dsl.rb', line 62

def helper(mod = nil, &block)
  if mod
    configuration[:helpers] << mod
  end

  if block
    configuration[:helpers] << Module.new.tap { |mod| mod.module_eval(&block) }
  end
end

#nodoc!Object



50
51
52
# File 'lib/brainstem/concerns/presenter_dsl.rb', line 50

def nodoc!
  configuration[:nodoc] = true
end

#preload(*args) ⇒ Object



26
27
28
# File 'lib/brainstem/concerns/presenter_dsl.rb', line 26

def preload(*args)
  configuration.array!(:preloads).concat args
end

#query_strategy(strategy) ⇒ Object



163
164
165
# File 'lib/brainstem/concerns/presenter_dsl.rb', line 163

def query_strategy(strategy)
  configuration[:query_strategy] = strategy
end

#reset_configuration!Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/brainstem/concerns/presenter_dsl.rb', line 168

def reset_configuration!
  configuration.array!(:preloads)
  configuration.array!(:helpers)
  configuration.nest!(:conditionals)
  configuration.nest!(:fields)
  configuration.nest!(:filters)
  configuration.nest!(:sort_orders)
  configuration.nest!(:associations)
  configuration.nonheritable!(:title)
  configuration.nonheritable!(:description)
  configuration.nonheritable!(:nodoc)
end

#search(&block) ⇒ Object



155
156
157
# File 'lib/brainstem/concerns/presenter_dsl.rb', line 155

def search(&block)
  configuration[:search] = block
end

#sort_order(name, order, options) ⇒ Object #sort_order(name, options) {|scope| ... } ⇒ Object

Overloads:

  • #sort_order(name, order, options) ⇒ Object

    Parameters:

    • name (Symbol)

      The name of the sort order.

    • order (String)

      The SQL string to use to sort the presented data.

    • options (Hash)

    Options Hash (options):

    • :info (String)

      Docstring for the sort order

    • :nodoc (Boolean)

      Whether this sort order be included in the generated documentation

  • #sort_order(name, options) {|scope| ... } ⇒ Object

    Create a named sort order, either containing a string to use as ORDER in a query, or with a block that adds an order Arel predicate to a scope.

    Yield Parameters:

    • scope (ActiveRecord::Relation)

      The scope representing the data being presented.

    Yield Returns:

    • (ActiveRecord::Relation)

      A new scope that adds ordering requirements to the scope that was yielded.

Raises:

  • (ArgumentError)

    if neither an order string or block is given.



109
110
111
112
113
114
115
116
117
118
119
# File 'lib/brainstem/concerns/presenter_dsl.rb', line 109

def sort_order(name, *args, &block)
  valid_options = %w(info nodoc)
  options       = args.extract_options!
                    .select { |k, v| valid_options.include?(k.to_s) }
  order         = args.first

  raise ArgumentError, "A sort order must be given" unless block_given? || order
  configuration[:sort_orders][name] = options.merge({
    value: (block_given? ? block : order)
  })
end

#title(str, options = { nodoc: false }) ⇒ Object



42
43
44
# File 'lib/brainstem/concerns/presenter_dsl.rb', line 42

def title(str, options = { nodoc: false })
  configuration[:title] = options.merge(info: str)
end