Module: Super::Controls::Steps
- Included in:
- Super::Controls
- Defined in:
- lib/super/controls/steps.rb,
lib/super/pagination.rb
Overview
Methods that are called by controller actions. All of these methods have a default implementation, but feel free to override as needed.
Instance Method Summary collapse
- #apply_queries(query_form:, records:) ⇒ Object
-
#build_record(action:) ⇒ ActiveRecord::Base
Builds a record using
#scope
. -
#build_record_with_params(action:, params:) ⇒ ActiveRecord::Base
Builds and populates a record using
#scope
. -
#destroy_record(action:, record:, params:) ⇒ ActiveRecord::Base, false
Destroys a record.
- #initialize_filter_form(query_form:) ⇒ Object
-
#initialize_pagination(action:, records:, query_params:) ⇒ Pagination
Sets up pagination.
- #initialize_query_form(params:, current_path:) ⇒ Object
- #initialize_sort_form(query_form:) ⇒ Object
-
#load_record(action:, params:) ⇒ ActiveRecord::Base
Loads a record using
#scope
. -
#load_records(action:, params:) ⇒ ActiveRecord::Relation
Tells the controller how to load records in the index action using
#scope
. -
#paginate_records(action:, records:, pagination:) ⇒ ActiveRecord::Relation
Paginates.
-
#save_record(action:, record:, params:) ⇒ true, false
Saves a record.
-
#update_record(action:, record:, params:) ⇒ true, false
Saves a record.
Instance Method Details
#apply_queries(query_form:, records:) ⇒ Object
80 81 82 |
# File 'lib/super/controls/steps.rb', line 80 def apply_queries(query_form:, records:) query_form.apply_changes(records) end |
#build_record(action:) ⇒ ActiveRecord::Base
Builds a record using #scope
31 32 33 |
# File 'lib/super/controls/steps.rb', line 31 def build_record(action:) scope(action: action).build end |
#build_record_with_params(action:, params:) ⇒ ActiveRecord::Base
Builds and populates a record using #scope
40 41 42 |
# File 'lib/super/controls/steps.rb', line 40 def build_record_with_params(action:, params:) scope(action: action).build(permitted_params(params, action: action)) end |
#destroy_record(action:, record:, params:) ⇒ ActiveRecord::Base, false
Destroys a record
67 68 69 |
# File 'lib/super/controls/steps.rb', line 67 def destroy_record(action:, record:, params:) record.destroy end |
#initialize_filter_form(query_form:) ⇒ Object
84 85 86 87 88 89 90 91 92 |
# File 'lib/super/controls/steps.rb', line 84 def initialize_filter_form(query_form:) if filters_enabled? query_form.add( Super::Filter::FormObject, namespace: :f, schema: filter_schema ) end end |
#initialize_pagination(action:, records:, query_params:) ⇒ Pagination
Sets up pagination
105 106 107 108 109 110 111 112 |
# File 'lib/super/pagination.rb', line 105 def initialize_pagination(action:, records:, query_params:) Pagination.new( total_count: records.size, limit: records_per_page(action: action, query_params: query_params), query_params: query_params, page_query_param: :page ) end |
#initialize_query_form(params:, current_path:) ⇒ Object
71 72 73 74 75 76 77 78 |
# File 'lib/super/controls/steps.rb', line 71 def initialize_query_form(params:, current_path:) Super::Query::FormObject.new( model: model, params: params, namespace: :q, current_path: current_path, ) end |
#initialize_sort_form(query_form:) ⇒ Object
94 95 96 97 98 99 100 101 102 103 |
# File 'lib/super/controls/steps.rb', line 94 def initialize_sort_form(query_form:) if sort_enabled? query_form.add( Super::Sort::FormObject, namespace: :s, default: default_sort, sortable_columns: sortable_columns ) end end |
#load_record(action:, params:) ⇒ ActiveRecord::Base
Loads a record using #scope
23 24 25 |
# File 'lib/super/controls/steps.rb', line 23 def load_record(action:, params:) scope(action: action).find(params[:id]) end |
#load_records(action:, params:) ⇒ ActiveRecord::Relation
Tells the controller how to load records in the index action using
#scope
14 15 16 |
# File 'lib/super/controls/steps.rb', line 14 def load_records(action:, params:) scope(action: action) end |
#paginate_records(action:, records:, pagination:) ⇒ ActiveRecord::Relation
Paginates
120 121 122 123 124 |
# File 'lib/super/pagination.rb', line 120 def paginate_records(action:, records:, pagination:) records .limit(pagination.limit) .offset(pagination.offset) end |
#save_record(action:, record:, params:) ⇒ true, false
Saves a record
49 50 51 |
# File 'lib/super/controls/steps.rb', line 49 def save_record(action:, record:, params:) record.save end |
#update_record(action:, record:, params:) ⇒ true, false
Saves a record
58 59 60 |
# File 'lib/super/controls/steps.rb', line 58 def update_record(action:, record:, params:) record.update(permitted_params(params, action: action)) end |