Module: Super::Controls::Optional

Included in:
Super::Controls
Defined in:
lib/super/controls/optional.rb,
lib/super/pagination.rb

Overview

Methods for Controls that have a sane default implementation

Instance Method Summary collapse

Instance Method Details

#collection_actions(action:) ⇒ Array<Link>

Configures the actions linked to on the index page. This is an optional method

Parameters:

Returns:



61
62
63
# File 'lib/super/controls/optional.rb', line 61

def collection_actions(action:)
  Super::Link.find_all(:new)
end

#default_sortObject



108
109
110
# File 'lib/super/controls/optional.rb', line 108

def default_sort
  { id: :desc }
end

#display_schema(action:) ⇒ Display

Configures the fields that are displayed on the index and show actions. This is a required method

Parameters:

Returns:



28
29
30
31
32
# File 'lib/super/controls/optional.rb', line 28

def display_schema(action:)
  Display.new do |fields, type|
    Display::Guesser.new(model: model, action: action, fields: fields, type: type).call
  end
end

#filter_schemaObject



84
85
86
87
88
# File 'lib/super/controls/optional.rb', line 84

def filter_schema
  Super::Filter.new do |fields, type|
    Super::Filter::Guesser.new(model: model, fields: fields, type: type).call
  end
end

#filters_enabled?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/super/controls/optional.rb', line 80

def filters_enabled?
  true
end

#form_schema(action:) ⇒ Form

Configures the editable fields on the new and edit actions. This is a required method

Parameters:

Returns:



39
40
41
42
43
# File 'lib/super/controls/optional.rb', line 39

def form_schema(action:)
  Form.new do |fields, type|
    Form::Guesser.new(model: model, fields: fields, type: type).call
  end
end

#member_actions(action:) ⇒ Array<Link>

Configures the actions linked to on the show page as well as each row of the table on the index page. This is an optional method

Parameters:

Returns:



70
71
72
73
74
75
76
77
78
# File 'lib/super/controls/optional.rb', line 70

def member_actions(action:)
  if action.show?
    Super::Link.find_all(:edit, :destroy)
  elsif action.edit?
    Super::Link.find_all(:show, :destroy)
  else
    Super::Link.find_all(:show, :edit, :destroy)
  end
end

#permitted_params(params, action:) ⇒ ActionController::Parameters

Configures which parameters could be written to the database. This is a required method

Parameters:

Returns:

  • (ActionController::Parameters)


51
52
53
54
# File 'lib/super/controls/optional.rb', line 51

def permitted_params(params, action:)
  strong_params = Super::Form::StrongParams.new(form_schema(action: action))
  params.require(strong_params.require(model)).permit(strong_params.permit)
end

#records_per_page(action:, query_params:) ⇒ ActiveRecord::Relation

Specifies how many records to show per page

Parameters:

Returns:

  • (ActiveRecord::Relation)


93
94
95
# File 'lib/super/pagination.rb', line 93

def records_per_page(action:, query_params:)
  Super.configuration.index_records_per_page
end

#scope(action:) ⇒ ActiveRecord::Relation

Configures what database records are visible on load. This is an optional method, it defaults to "all" methods

Parameters:

Returns:

  • (ActiveRecord::Relation)


19
20
21
# File 'lib/super/controls/optional.rb', line 19

def scope(action:)
  model.all
end

#sort_enabled?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/super/controls/optional.rb', line 90

def sort_enabled?
  true
end

#sortable_columnsObject



94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/super/controls/optional.rb', line 94

def sortable_columns
  action = ActionInquirer.new(
    ActionInquirer.default_for_resources,
    "index"
  )
  attribute_names =
    display_schema(action: action).each_attribute.map do |key, val|
      val = val.build if val.respond_to?(:build)
      key if val.real?
    end

  attribute_names.compact
end

#titleString

This is an optional method

Returns:

  • (String)


10
11
12
# File 'lib/super/controls/optional.rb', line 10

def title
  model.name.pluralize
end