Class: StimulusRailsDatatables::FilterHelper::FilterBuilder

Inherits:
Object
  • Object
show all
Defined in:
app/helpers/stimulus_rails_datatables/filter_helper.rb

Instance Method Summary collapse

Constructor Details

#initialize(view, root_key) ⇒ FilterBuilder

Returns a new instance of FilterBuilder.



19
20
21
22
# File 'app/helpers/stimulus_rails_datatables/filter_helper.rb', line 19

def initialize(view, root_key)
  @view = view
  @root_key = root_key
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *_args, **options, &block) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/helpers/stimulus_rails_datatables/filter_helper.rb', line 26

def method_missing(name, *_args, **options, &block)
  field_name = "#{@root_key}[#{name}]"
  html_class = options[:class] || 'form-control'

  if block
    @view.(
      :select,
      name: field_name,
      class: html_class,
      data: { filter_field_name: name }
    ) do
      yield(OptionBuilder.new(@view))
    end
  else
    remote = options[:remote] || {}
    depends_on = options[:depends_on]

    attrs = {
      filter_field_name: name,
      filter_remote_url_value: remote[:url],
      filter_label_key: remote[:label],
      filter_value_key: remote[:value],
      filter_depends_on: depends_on&.to_s,
      filter_placeholder: remote[:placeholder] || 'Select an option'
    }

    select_options = {
      data: attrs,
      class: html_class
    }

    # Only disable if it depends on another field
    select_options[:disabled] = true if depends_on.present?

    @view.select_tag(field_name, @view.options_for_select([]), **select_options)
  end
end

Instance Method Details

#duration(field_name = :duration, **options, &block) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'app/helpers/stimulus_rails_datatables/filter_helper.rb', line 103

def duration(field_name = :duration, **options, &block)
  full_name = "#{@root_key}[#{field_name}]"
  html_class = options[:class] || 'form-control'

  attrs = {
    action: 'change->filter#durationChanged',
    filter_duration_target: 'select',
    filter_field_name: field_name
  }

  @view.(
    :select,
    name: full_name,
    id: "#{@root_key}-duration",
    class: html_class,
    style: 'width: 100px;',
    data: attrs
  ) do
    yield(OptionBuilder.new(@view)) if block
  end
end

#location(province_url:, city_url:, barangay_url:, **options) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'app/helpers/stimulus_rails_datatables/filter_helper.rb', line 64

def location(province_url:, city_url:, barangay_url:, **options)
  html_class = options[:class] || 'form-select'

  province = province_id(
    remote: {
      url: province_url,
      label: 'name',
      value: 'location_id',
      placeholder: 'All Provinces'
    },
    class: html_class
  )

  city = city_id(
    remote: {
      url: city_url,
      label: 'name',
      value: 'location_id',
      placeholder: 'All Cities'
    },
    depends_on: :province_id,
    class: html_class
  )

  barangay = barangay_id(
    remote: {
      url: barangay_url,
      label: 'name',
      value: 'location_id',
      placeholder: 'All Barangays'
    },
    depends_on: :city_id,
    class: html_class
  )

  # Concatenate them so Rails captures the HTML
  province + city + barangay
end

#respond_to_missing?Boolean

Returns:

  • (Boolean)


24
# File 'app/helpers/stimulus_rails_datatables/filter_helper.rb', line 24

def respond_to_missing?; end