Class: Ecoportal::API::GraphQL::Base::Page::DataField::Checklist

Inherits:
DataField
  • Object
show all
Defined in:
lib/ecoportal/api/graphql/base/page/data_field/checklist.rb

Instance Method Summary collapse

Instance Method Details

#as_inputObject



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ecoportal/api/graphql/base/page/data_field/checklist.rb', line 15

def as_input
  return nil unless dirty?

  {
    checklist: {
      id:    id,
      # rubocop:disable Style/DoubleNegation
      items: (doc['items'] || []).map { |i| { id: i['id'], checked: !!i['checked'] } }
      # rubocop:enable Style/DoubleNegation
    }
  }
end

#check(label_or_id, checked: true) ⇒ Object

Check/uncheck an item by label or id.



8
9
10
11
12
13
# File 'lib/ecoportal/api/graphql/base/page/data_field/checklist.rb', line 8

def check(label_or_id, checked: true)
  item = (doc['items'] || []).find { |i| i['label'] == label_or_id.to_s || i['id'] == label_or_id.to_s }
  return unless item

  item['checked'] = checked
end