Class: MetadataPresenter::AddressFieldset

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Translation
Includes:
ActionView::Helpers, ActiveModel::Validations
Defined in:
app/models/metadata_presenter/address_fieldset.rb

Constant Summary collapse

DEFAULT_COUNTRY =
'United Kingdom'.freeze
FIELDS =
%w[
  address_line_one
  address_line_two
  city
  county
  postcode
  country
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(address) ⇒ AddressFieldset

Returns a new instance of AddressFieldset.



19
20
21
22
23
24
25
26
27
# File 'app/models/metadata_presenter/address_fieldset.rb', line 19

def initialize(address)
  FIELDS.each do |field|
    next unless address[field]

    instance_variable_set(:"@#{field}", conform(address[field]))
  end

  @country ||= DEFAULT_COUNTRY
end

Instance Method Details

#as_jsonObject



33
34
35
# File 'app/models/metadata_presenter/address_fieldset.rb', line 33

def as_json
  super(only: FIELDS)
end

#conform(address_field) ⇒ Object



37
38
39
# File 'app/models/metadata_presenter/address_fieldset.rb', line 37

def conform(address_field)
  sanitize(address_field, tags: [], attributes: []).strip
end

#to_aObject



29
30
31
# File 'app/models/metadata_presenter/address_fieldset.rb', line 29

def to_a
  as_json.values.compact_blank
end