Class: LinkedRails::Collection
- Inherits:
-
Object
- Object
- LinkedRails::Collection
- Includes:
- ActiveModel::Model, Filterable, Iri, IriMapping, Sortable, Model::Enhancements, Model::Iri
- Defined in:
- app/models/linked_rails/collection.rb,
app/models/linked_rails/collection/iri.rb,
app/models/linked_rails/collection/view.rb,
app/models/linked_rails/collection/filter.rb,
app/models/linked_rails/collection/sorting.rb,
app/models/linked_rails/collection/sortable.rb,
app/models/linked_rails/collection/filterable.rb,
app/models/linked_rails/collection/iri_mapping.rb,
app/models/linked_rails/collection/filter_field.rb,
app/models/linked_rails/collection/filter_option.rb,
app/models/linked_rails/collection/infinite_view.rb,
app/policies/linked_rails/collection/view_policy.rb,
app/models/linked_rails/collection/paginated_view.rb,
app/serializers/linked_rails/collection/view_serializer.rb,
app/serializers/linked_rails/collection/filter_serializer.rb,
app/serializers/linked_rails/collection/sorting_serializer.rb,
app/serializers/linked_rails/collection/filter_field_serializer.rb,
app/serializers/linked_rails/collection/filter_option_serializer.rb
Overview
rubocop:disable Metrics/ClassLength
Defined Under Namespace
Modules: Filterable, Iri, IriMapping, Sortable Classes: Filter, FilterField, FilterFieldSerializer, FilterOption, FilterOptionSerializer, FilterSerializer, InfiniteView, PaginatedView, Sorting, SortingSerializer, View, ViewPolicy, ViewSerializer
Instance Attribute Summary collapse
-
#association ⇒ Object
Returns the value of attribute association.
- #association_base ⇒ Object
-
#association_class ⇒ Object
Returns the value of attribute association_class.
-
#association_scope ⇒ Object
Returns the value of attribute association_scope.
-
#default_display ⇒ Object
writeonly
Sets the attribute default_display.
-
#default_title ⇒ Object
writeonly
Sets the attribute default_title.
-
#default_type ⇒ Object
writeonly
Sets the attribute default_type.
- #display ⇒ Object
-
#grid_max_columns ⇒ Object
Returns the value of attribute grid_max_columns.
-
#joins ⇒ Object
Returns the value of attribute joins.
-
#name ⇒ Object
Returns the value of attribute name.
-
#page_size ⇒ Object
Returns the value of attribute page_size.
-
#parent ⇒ Object
Returns the value of attribute parent.
-
#part_of ⇒ Object
Returns the value of attribute part_of.
-
#policy ⇒ Object
Returns the value of attribute policy.
-
#policy_scope ⇒ Object
writeonly
Sets the attribute policy_scope.
-
#table_type ⇒ Object
writeonly
Sets the attribute table_type.
- #title ⇒ Object
- #type ⇒ Object
-
#user_context ⇒ Object
Returns the value of attribute user_context.
-
#view ⇒ Object
Returns the value of attribute view.
- #views ⇒ Object
Attributes included from Sortable
#default_sortings, #sort, #sort_options
Attributes included from Filterable
#default_filters, #filter, #unfiltered_collection
Instance Method Summary collapse
- #action_list(user_context) ⇒ Object
- #actions(user_context = nil) ⇒ Object
- #apply_scope(association) ⇒ Object
-
#as_json(options = {}) ⇒ Object
prevents a ‘stack level too deep`.
-
#build_child ⇒ Object
rubocop:disable Metrics/AbcSize.
- #columns ⇒ Object
- #default_page_size ⇒ Object
- #default_view ⇒ Object
- #first ⇒ Object
- #inspect ⇒ Object
- #last ⇒ Object
- #new_child(options) ⇒ Object
- #preview_includes ⇒ Object
- #title_from_translation ⇒ Object
- #total_count ⇒ Object
- #total_page_count ⇒ Object
- #unscoped_association ⇒ Object
- #view_with_opts(opts) ⇒ Object
Methods included from Sortable
#default_before_value, #parsed_sort_values, #primary_key_sorting, #sorted?, #sorted_association, #sortings
Methods included from Iri
#filter_iri_opts, #iri_opts, #iri_opts_add, #iri_template, #iri_template_opts, #sort_iri_opts
Methods included from Filterable
#filter_fields, #filtered?, #filters, #unfiltered
Methods included from Model::Iri
#anonymous_iri, #anonymous_iri?, #iri, #iri_opts, #rdf_type, #reload, #root_relative_iri, #route_fragment
Methods included from Model::Enhancements
Instance Attribute Details
#association ⇒ Object
Returns the value of attribute association.
27 28 29 |
# File 'app/models/linked_rails/collection.rb', line 27 def association @association end |
#association_base ⇒ Object
59 60 61 |
# File 'app/models/linked_rails/collection.rb', line 59 def association_base @association_base ||= apply_scope(sorted_association(filtered_association)) end |
#association_class ⇒ Object
Returns the value of attribute association_class.
27 28 29 |
# File 'app/models/linked_rails/collection.rb', line 27 def association_class @association_class end |
#association_scope ⇒ Object
Returns the value of attribute association_scope.
27 28 29 |
# File 'app/models/linked_rails/collection.rb', line 27 def association_scope @association_scope end |
#default_display=(value) ⇒ Object
Sets the attribute default_display
29 30 31 |
# File 'app/models/linked_rails/collection.rb', line 29 def default_display=(value) @default_display = value end |
#default_title=(value) ⇒ Object (writeonly)
Sets the attribute default_title
29 30 31 |
# File 'app/models/linked_rails/collection.rb', line 29 def default_title=(value) @default_title = value end |
#default_type=(value) ⇒ Object
Sets the attribute default_type
29 30 31 |
# File 'app/models/linked_rails/collection.rb', line 29 def default_type=(value) @default_type = value end |
#display ⇒ Object
90 91 92 |
# File 'app/models/linked_rails/collection.rb', line 90 def display @display&.to_sym || default_display end |
#grid_max_columns ⇒ Object
Returns the value of attribute grid_max_columns.
27 28 29 |
# File 'app/models/linked_rails/collection.rb', line 27 def grid_max_columns @grid_max_columns end |
#joins ⇒ Object
Returns the value of attribute joins.
27 28 29 |
# File 'app/models/linked_rails/collection.rb', line 27 def joins @joins end |
#name ⇒ Object
Returns the value of attribute name.
27 28 29 |
# File 'app/models/linked_rails/collection.rb', line 27 def name @name end |
#page_size ⇒ Object
Returns the value of attribute page_size.
27 28 29 |
# File 'app/models/linked_rails/collection.rb', line 27 def page_size @page_size end |
#parent ⇒ Object
Returns the value of attribute parent.
27 28 29 |
# File 'app/models/linked_rails/collection.rb', line 27 def parent @parent end |
#part_of ⇒ Object
Returns the value of attribute part_of.
27 28 29 |
# File 'app/models/linked_rails/collection.rb', line 27 def part_of @part_of end |
#policy ⇒ Object
Returns the value of attribute policy.
27 28 29 |
# File 'app/models/linked_rails/collection.rb', line 27 def policy @policy end |
#policy_scope=(value) ⇒ Object
Sets the attribute policy_scope
29 30 31 |
# File 'app/models/linked_rails/collection.rb', line 29 def policy_scope=(value) @policy_scope = value end |
#table_type=(value) ⇒ Object
Sets the attribute table_type
29 30 31 |
# File 'app/models/linked_rails/collection.rb', line 29 def table_type=(value) @table_type = value end |
#title ⇒ Object
125 126 127 128 129 130 |
# File 'app/models/linked_rails/collection.rb', line 125 def title var = @title || @default_title return var.call(parent) if var.respond_to?(:call) var || title_from_translation end |
#type ⇒ Object
149 150 151 |
# File 'app/models/linked_rails/collection.rb', line 149 def type @type&.to_sym || default_type end |
#user_context ⇒ Object
Returns the value of attribute user_context.
27 28 29 |
# File 'app/models/linked_rails/collection.rb', line 27 def user_context @user_context end |
#view ⇒ Object
Returns the value of attribute view.
27 28 29 |
# File 'app/models/linked_rails/collection.rb', line 27 def view @view end |
#views ⇒ Object
157 158 159 |
# File 'app/models/linked_rails/collection.rb', line 157 def views @views || [default_view] end |
Instance Method Details
#action_list(user_context) ⇒ Object
34 35 36 37 38 39 40 |
# File 'app/models/linked_rails/collection.rb', line 34 def action_list(user_context) @action_list ||= {} @action_list[user_context] ||= association_class.try(:action_list)&.new( resource: self, user_context: user_context ) end |
#actions(user_context = nil) ⇒ Object
42 43 44 45 46 |
# File 'app/models/linked_rails/collection.rb', line 42 def actions(user_context = nil) return [] if action_list(user_context).nil? super end |
#apply_scope(association) ⇒ Object
48 49 50 51 52 |
# File 'app/models/linked_rails/collection.rb', line 48 def apply_scope(association) return association if policy_scope == false policy_scope.new(user_context, association).resolve end |
#as_json(options = {}) ⇒ Object
prevents a ‘stack level too deep`
55 56 57 |
# File 'app/models/linked_rails/collection.rb', line 55 def as_json( = {}) super(.merge(except: %w[association_class unfiltered_collection collection])) end |
#build_child ⇒ Object
rubocop:disable Metrics/AbcSize
63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'app/models/linked_rails/collection.rb', line 63 def build_child # rubocop:disable Metrics/AbcSize child = parent&.build_child(association_class, user_context: user_context) || association_class.build_new(parent: parent, user_context: user_context) attributes_from_filters = ActionController::Parameters.new(association_class.attributes_from_filters(@filter)) permitted_child_keys = Pundit.policy(user_context, child)&.permitted_attributes || [] permitted_attributes_from_filters = attributes_from_filters.permit(permitted_child_keys) child.assign_attributes(permitted_attributes_from_filters) child end |
#columns ⇒ Object
76 77 78 79 80 |
# File 'app/models/linked_rails/collection.rb', line 76 def columns columns_list = association_class.try(:defined_columns).try(:[], table_type) RDF::List[*columns_list] if columns_list.present? end |
#default_page_size ⇒ Object
82 83 84 |
# File 'app/models/linked_rails/collection.rb', line 82 def default_page_size association_class.try(:default_per_page) || 20 end |
#default_view ⇒ Object
86 87 88 |
# File 'app/models/linked_rails/collection.rb', line 86 def default_view @default_view ||= view_with_opts(default_view_opts) end |
#first ⇒ Object
94 95 96 97 98 99 100 101 |
# File 'app/models/linked_rails/collection.rb', line 94 def first case type when :paginated iri_with_root(root_relative_iri(page: 1)) when :infinite iri_with_root(root_relative_iri(before: default_before_value)) end end |
#inspect ⇒ Object
103 104 105 |
# File 'app/models/linked_rails/collection.rb', line 103 def inspect "#<#{association_class}Collection iri:#{iri}>" end |
#last ⇒ Object
107 108 109 |
# File 'app/models/linked_rails/collection.rb', line 107 def last iri_with_root(root_relative_iri(page: [total_page_count, 1].max)) if type == :paginated && total_page_count end |
#new_child(options) ⇒ Object
111 112 113 114 |
# File 'app/models/linked_rails/collection.rb', line 111 def new_child() attrs = .merge(new_child_values) self.class.new(attrs) end |
#preview_includes ⇒ Object
116 117 118 119 120 121 122 123 |
# File 'app/models/linked_rails/collection.rb', line 116 def preview_includes { default_view: default_view.preview_includes, filter_fields: :options, filters: [], sortings: [] } end |
#title_from_translation ⇒ Object
132 133 134 135 136 137 138 139 |
# File 'app/models/linked_rails/collection.rb', line 132 def title_from_translation plural = association_class.name.tableize I18n.t( "#{plural}.collection.#{@filter&.values&.join('.').presence || name || :default}", count: ->(_opts) { total_count }, default: association_class.plural_label ) end |
#total_count ⇒ Object
141 142 143 |
# File 'app/models/linked_rails/collection.rb', line 141 def total_count @total_count ||= association_base.try(:total_count) || unscoped_association.count end |
#total_page_count ⇒ Object
145 146 147 |
# File 'app/models/linked_rails/collection.rb', line 145 def total_page_count (total_count / (page_size || default_page_size).to_f).ceil if total_count end |
#unscoped_association ⇒ Object
153 154 155 |
# File 'app/models/linked_rails/collection.rb', line 153 def unscoped_association association_base.respond_to?(:unscope) ? association_base.unscope(:select) : association_base end |
#view_with_opts(opts) ⇒ Object
161 162 163 164 165 166 |
# File 'app/models/linked_rails/collection.rb', line 161 def view_with_opts(opts) @views ||= [] view = LinkedRails.collection_view_class.new({collection: self, type: type}.merge(opts)) @views << view view end |