Class: Koi::IndexActionsBuilder

Inherits:
Object
  • Object
show all
Defined in:
app/helpers/koi/index_actions_helper.rb

Instance Method Summary collapse

Constructor Details

#initialize(context, search:, create:) ⇒ IndexActionsBuilder

Returns a new instance of IndexActionsBuilder.



13
14
15
16
17
# File 'app/helpers/koi/index_actions_helper.rb', line 13

def initialize(context, search:, create:)
  @context = context
  @search  = search
  @create  = create
end

Instance Method Details

#actionsObject



83
84
85
86
87
88
89
# File 'app/helpers/koi/index_actions_helper.rb', line 83

def actions
  [
    ("shortcut:cancel@document->index-actions#clear" if search?),
    ("shortcut:create@document->index-actions#create" if create?),
    ("shortcut:search@document->index-actions#search" if search?),
  ].compact.join(" ")
end

#create?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'app/helpers/koi/index_actions_helper.rb', line 23

def create?
  !!@create
end

#create_button(_form) ⇒ Object



62
63
64
65
66
67
68
# File 'app/helpers/koi/index_actions_helper.rb', line 62

def create_button(_form)
  tag.button(t("koi.labels.new", default: "New"),
             type:       :submit,
             formaction: @create == true ? url_for(action: :new) : new_polymorphic_path(@create),
             class:      "button--primary",
             data:       { index_actions_target: "create" })
end


77
78
79
80
81
# File 'app/helpers/koi/index_actions_helper.rb', line 77

def links(form, &)
  tag.div(class: "actions-group") do
    yield(form) if block_given?
  end
end

#renderObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/helpers/koi/index_actions_helper.rb', line 27

def render(&)
  tag.nav class: "index-actions", data: { controller: "index-actions", action: actions } do
    form_with(**search_options,
              data: { controller:   "search",
                      turbo_action: "replace",
                      action:       <<~ACTIONS,
                        input->index-actions#update
                        change->index-actions#update
                        submit->index-actions#submit
                      ACTIONS
              }) do |form|
      concat(links(form, &))
      concat(sort_input(form))
      concat(search(form)) if create? || search?
    end
  end
end

#search(form) ⇒ Object



45
46
47
48
49
50
51
# File 'app/helpers/koi/index_actions_helper.rb', line 45

def search(form)
  tag.div class: "actions-group" do
    concat(search_button(form)) if search?
    concat(create_button(form)) if create?
    concat(search_input(form)) if search?
  end
end

#search?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'app/helpers/koi/index_actions_helper.rb', line 19

def search?
  !!@search
end

#search_button(form) ⇒ Object

Hidden button to trigger search, avoids triggering create instead.



58
59
60
# File 'app/helpers/koi/index_actions_helper.rb', line 58

def search_button(form)
  form.submit("Search", hidden: "")
end

#search_input(form) ⇒ Object



70
71
72
73
74
75
# File 'app/helpers/koi/index_actions_helper.rb', line 70

def search_input(form)
  form.search_field(:search,
                    placeholder: t("koi.labels.search", default: "Search"),
                    value:       params[:search],
                    data:        { index_actions_target: "search", turbo_permanent: "" })
end

#search_optionsObject



91
92
93
94
95
# File 'app/helpers/koi/index_actions_helper.rb', line 91

def search_options
  options = { url: request.path, method: :get, scope: "" }
  options.merge!(@search) if @search.is_a?(Hash)
  options
end

#sort_input(form) ⇒ Object



53
54
55
# File 'app/helpers/koi/index_actions_helper.rb', line 53

def sort_input(form)
  form.hidden_field(:sort, value: params[:sort], data: { index_actions_target: "sort" })
end