Module: JSONAPIonify::Api::Resource::Definitions::Attributes
- Defined in:
- lib/jsonapionify/api/resource/definitions/attributes.rb
Class Method Summary collapse
Instance Method Summary collapse
- #attribute(name, type, description = '', **options, &block) ⇒ Object
- #builder(&block) ⇒ Object
- #field_valid?(name) ⇒ Boolean
- #fields ⇒ Object
- #fields_for_action(action, context) ⇒ Object
- #id(sym) ⇒ Object
- #remove_attribute(name) ⇒ Object
Class Method Details
.extended(klass) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/jsonapionify/api/resource/definitions/attributes.rb', line 4 def self.extended(klass) klass.class_eval do extend JSONAPIonify::InheritedAttributes extend JSONAPIonify::Types inherited_array_attribute :attributes delegate :id_attribute, :attributes, to: :class context(:fields, readonly: true) do |context| should_error = false input_fields = context.request.params['fields'] || {} actionable_fields = self.class.fields_for_action(context.action_name, context) input_fields.each_with_object( actionable_fields ) do |(type, fields), field_map| type_sym = type.to_sym field_symbols = fields.to_s.split(',').map(&:to_sym) field_map[type_sym] = field_symbols.each_with_object([]) do |field, field_list| type_attributes = self.class.api.resource(type_sym).attributes attribute = type_attributes.find do |attribute| attribute.name == field && attribute.supports_read_for_action?(context.action_name, context) end if attribute field_list << attribute.name else error(:field_not_permitted, type, field) should_error = true end end end.tap do halt if should_error end end end end |
Instance Method Details
#attribute(name, type, description = '', **options, &block) ⇒ Object
47 48 49 50 51 52 53 54 |
# File 'lib/jsonapionify/api/resource/definitions/attributes.rb', line 47 def attribute(name, type, description = '', **, &block) Attribute.new( name, type, description, **, &block ).tap do |new_attribute| attributes.delete(new_attribute) attributes << new_attribute end end |
#builder(&block) ⇒ Object
64 65 66 67 68 69 70 |
# File 'lib/jsonapionify/api/resource/definitions/attributes.rb', line 64 def builder(&block) context :builder, readonly: true, persisted: true do |context| proc do |resource, instance| block.call resource, instance, context end end end |
#field_valid?(name) ⇒ Boolean
72 73 74 |
# File 'lib/jsonapionify/api/resource/definitions/attributes.rb', line 72 def field_valid?(name) fields.include? name.to_sym end |
#fields ⇒ Object
60 61 62 |
# File 'lib/jsonapionify/api/resource/definitions/attributes.rb', line 60 def fields attributes.select(&:read?).map(&:name) end |
#fields_for_action(action, context) ⇒ Object
76 77 78 79 80 81 82 83 84 |
# File 'lib/jsonapionify/api/resource/definitions/attributes.rb', line 76 def fields_for_action(action, context) api.fields.each_with_object({}) do |(type, attrs), fields| fields[type] = attrs.select do |attr| api.resource(type).attributes.find do |type_attr| type_attr.name == attr end.supports_read_for_action? action, context end end end |
#id(sym) ⇒ Object
41 42 43 44 45 |
# File 'lib/jsonapionify/api/resource/definitions/attributes.rb', line 41 def id(sym) define_singleton_method :id_attribute do sym end end |
#remove_attribute(name) ⇒ Object
56 57 58 |
# File 'lib/jsonapionify/api/resource/definitions/attributes.rb', line 56 def remove_attribute(name) attributes.delete_if { |attr| attr.name == name.to_sym } end |