Module: Infienity::Commander

Defined in:
lib/infienity/commander.rb

Instance Method Summary collapse

Instance Method Details

#commander_assigns(*fields) ⇒ Object



3
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/infienity/commander.rb', line 3

def commander_assigns(*fields)
  fields.each do |field|
    current_class = field.to_s.singularize.classify.constantize

    define_method "paginate_#{field}" do |pagination_params:, is_first_load: false|
      search_opts = { attribute: state.search_attribute, search_string: state.search_string }
      sort_opts = state.sorting_dropdown_options.fetch(state.selected_dropdown_option)

      return if (state.per_page * state.index) > current_class.paginate_count(search: search_opts)

      state.index = state.index + 1
      state.users = state.users + current_class.paginate(per_page: state.per_page, start: state.index, search: search_opts, sort: sort_opts)

      execute_js_function('Infienity.fieResponded', is_first_load)
    end

    define_method "filter_#{field}" do
      search_opts = { attribute: state.search_attribute, search_string: state.search_string }
      sort_opts = state.sorting_dropdown_options.fetch(state.selected_dropdown_option)
      state.index = 0

      state.users = current_class.paginate(per_page: state.per_page, start: 0, search: search_opts, sort: sort_opts)

      execute_js_function('Infienity.fieResponded', true)
    end

    define_method "sort_#{field}" do |sort:|
      unless state.sorting_dropdown_options.key?(state.selected_dropdown_option)
        raise StandardError("dropdown option not provided in the correct format")
      end

      state.selected_dropdown_option = sort.keys.first

      search_opts = { attribute: state.search_attribute, search_string: state.search_string }
      sort_opts = sort.values.first
      state.index = 0

      state.users = current_class.paginate(per_page: state.per_page, start: 0, search: search_opts, sort: sort_opts)

      execute_js_function('Infienity.fieResponded', true)
    end

    define_method "sort_select_#{field}" do
      unless state.sorting_dropdown_options.key?(state.selected_dropdown_option)
        raise StandardError("dropdown option not provided in the correct format")
      end

      sort = JSON.parse(@caller[:value])
      state.selected_dropdown_option = sort.keys.first

      search_opts = { attribute: state.search_attribute, search_string: state.search_string }
      sort_opts = sort.values.first
      state.index = 0

      state.users = current_class.paginate(per_page: state.per_page, start: 0, search: search_opts, sort: sort_opts)

      execute_js_function('Infienity.fieResponded', true)
    end
  end
end