Class: CrudComponents::Fieldset

Inherits:
Object
  • Object
show all
Defined in:
lib/crud_components/fieldset.rb

Overview

A named selection of fields and actions. ‘filters:` extends the filterable set beyond the visible fields (“filter only what you can see” is the default rule).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, fields = :all, actions: nil, filters: nil) ⇒ Fieldset

Returns a new instance of Fieldset.

Parameters:

  • name (Symbol)

    the fieldset name.

  • fields (Array<Symbol>, :all) (defaults to: :all)

    the fields, in order (‘:all` = every declared/derived field).

  • actions (Array<Symbol>, String, nil) (defaults to: nil)

    a curated list of action names, or a custom partial path; nil keeps the derived actions.

  • filters (Array<Symbol>, nil) (defaults to: nil)

    filterable fields beyond the visible ones.



14
15
16
17
18
19
# File 'lib/crud_components/fieldset.rb', line 14

def initialize(name, fields = :all, actions: nil, filters: nil)
  @name = name.to_sym
  @field_names = fields == :all ? :all : Array(fields).map(&:to_sym)
  @action_spec = actions
  @filter_names = Array(filters || []).map(&:to_sym)
end

Instance Attribute Details

#action_specObject (readonly)

Returns the value of attribute action_spec.



6
7
8
# File 'lib/crud_components/fieldset.rb', line 6

def action_spec
  @action_spec
end

#field_namesObject (readonly)

Returns the value of attribute field_names.



6
7
8
# File 'lib/crud_components/fieldset.rb', line 6

def field_names
  @field_names
end

#filter_namesObject (readonly)

Returns the value of attribute filter_names.



6
7
8
# File 'lib/crud_components/fieldset.rb', line 6

def filter_names
  @filter_names
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/crud_components/fieldset.rb', line 6

def name
  @name
end

Instance Method Details

#action_namesArray<Symbol>?

The curated action names (‘actions: %i[edit destroy]`), or nil when the derived defaults apply.

Returns:

  • (Array<Symbol>, nil)


27
28
29
# File 'lib/crud_components/fieldset.rb', line 27

def action_names
  @action_spec.is_a?(Array) ? @action_spec.map(&:to_sym) : nil
end

#all_fields?Boolean

Returns whether this fieldset shows every field.

Returns:

  • (Boolean)

    whether this fieldset shows every field.



22
# File 'lib/crud_components/fieldset.rb', line 22

def all_fields? = @field_names == :all

#custom_actions_partialString?

A custom actions partial (‘actions: ’books/actions’‘, receiving `record`), or nil.

Returns:

  • (String, nil)


34
35
36
# File 'lib/crud_components/fieldset.rb', line 34

def custom_actions_partial
  @action_spec.is_a?(String) ? @action_spec : nil
end