Module: LinkedRails::Model::Collections::ClassMethods
- Defined in:
- lib/linked_rails/model/collections.rb
Instance Method Summary collapse
- #collection_iri(**opts) ⇒ Object
-
#collection_options(**options) ⇒ Object
Sets the defaults for all collections for this class.
- #collection_root_relative_iri(**opts) ⇒ Object
- #collection_route_key ⇒ Object
- #default_collection_option(key) ⇒ Object
- #default_collection_options ⇒ Object
-
#with_collection(name, **options) ⇒ Collection
Defines a collection to be used in #collection_for.
Instance Method Details
#collection_iri(**opts) ⇒ Object
66 67 68 |
# File 'lib/linked_rails/model/collections.rb', line 66 def collection_iri(**opts) LinkedRails.iri(path: collection_root_relative_iri(**opts)) end |
#collection_options(**options) ⇒ Object
Sets the defaults for all collections for this class. Can be overridden by #with_collection, called from associated models, or by passing parameters in an iri.
74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/linked_rails/model/collections.rb', line 74 def (**) initialize_default_collection_opts .each do |key, value| raise("Invalid key passed to collection_options: #{key}") unless valid_collection_option?(key) _default_collection_opts[key] = value end _default_collection_opts[:iri_template] = LinkedRails.collection_class.generate_iri_template( _default_collection_opts[:iri_template_keys] ) _default_collection_opts end |
#collection_root_relative_iri(**opts) ⇒ Object
88 89 90 91 92 |
# File 'lib/linked_rails/model/collections.rb', line 88 def collection_root_relative_iri(**opts) opts[:filter] = LinkedRails.collection_class.filter_iri_opts(opts[:filter]) if opts.key?(:filter) opts[:route_key] = collection_route_key default_collection_option(:iri_template).(**opts) end |
#collection_route_key ⇒ Object
94 95 96 |
# File 'lib/linked_rails/model/collections.rb', line 94 def collection_route_key default_collection_option(:route_key) || route_key end |
#default_collection_option(key) ⇒ Object
104 105 106 |
# File 'lib/linked_rails/model/collections.rb', line 104 def default_collection_option(key) [key] end |
#default_collection_options ⇒ Object
98 99 100 101 102 |
# File 'lib/linked_rails/model/collections.rb', line 98 def initialize_default_collection_opts _default_collection_opts end |
#with_collection(name, **options) ⇒ Collection
Note:
Adds a instance_method <name>_collection
Defines a collection to be used in LinkedRails::Model::Collections#collection_for
114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/linked_rails/model/collections.rb', line 114 def with_collection(name, **) # rubocop:disable Metrics/AbcSize [:association] ||= name.to_sym [:association_class] ||= name.to_s.classify.constantize = [:association_class]..merge() [:iri_template] = LinkedRails.collection_class.generate_iri_template( [:iri_template_keys] ) collections_add(name: name, options: ) define_method "#{name.to_s.singularize}_collection" do |opts = {}| collection_for(name, **opts) end end |