Class: Wallaby::ActiveRecord::ModelServiceProvider::Permitter

Inherits:
Object
  • Object
show all
Defined in:
lib/adapters/wallaby/active_record/model_service_provider/permitter.rb

Overview

Allowlist the params for mass-assignment

Instance Method Summary collapse

Constructor Details

#initialize(model_decorator) ⇒ Permitter

Returns a new instance of Permitter.

Parameters:

  • model_decorator (Wallaby::ModelDecorator)


9
10
11
# File 'lib/adapters/wallaby/active_record/model_service_provider/permitter.rb', line 9

def initialize(model_decorator)
  @model_decorator = model_decorator
end

Instance Method Details

#compound_hashed_fieldsArray<String>

Returns a list of field names of range and association.

Returns:

  • (Array<String>)

    a list of field names of range and association



25
26
27
28
29
30
# File 'lib/adapters/wallaby/active_record/model_service_provider/permitter.rb', line 25

def compound_hashed_fields
  field_names =
    range_fields.keys +
    many_association_fields.map { |_, | [:foreign_key] }
  field_names.each_with_object({}) { |name, hash| hash[name] = [] }
end

#simple_field_namesArray<String>

Returns a list of field names of general types.

Returns:

  • (Array<String>)

    a list of field names of general types



14
15
16
17
18
19
20
21
22
# File 'lib/adapters/wallaby/active_record/model_service_provider/permitter.rb', line 14

def simple_field_names
  field_names =
    non_range_fields.keys +
    belongs_to_fields.flat_map do |_, |
      [[:foreign_key], [:polymorphic_type]]
    end.compact
  fields = [@model_decorator.primary_key, 'created_at', 'updated_at']
  field_names.reject { |field_name| fields.include?(field_name) }.uniq
end