Class: WithForm::ModelForm

Inherits:
Object
  • Object
show all
Includes:
ActionView::Helpers::TranslationHelper
Defined in:
lib/with_form/model_form.rb

Instance Method Summary collapse

Constructor Details

#initialize(model:, page:) ⇒ ModelForm

Returns a new instance of ModelForm.



5
6
7
8
# File 'lib/with_form/model_form.rb', line 5

def initialize(model:, page:)
  @page = page
  @model = model
end

Instance Method Details

#attach_file(attribute, path = nil, **options) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/with_form/model_form.rb', line 18

def attach_file(attribute, path = nil, **options)
  scope_form.attach_file(
    attribute,
    path || @model.public_send(attribute),
    **options,
  )
end

#check(attribute, **options) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/with_form/model_form.rb', line 37

def check(attribute, **options)
  case attribute
  when Symbol
    values = Array(@model.public_send(attribute))
  else
    values = Array(attribute)
  end

  values.each { |value| scope_form.check(value, **options) }
end

#choose(attribute, **options) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/with_form/model_form.rb', line 26

def choose(attribute, **options)
  case attribute
  when Symbol
    value = @model.public_send(attribute)
  else
    value = attribute
  end

  scope_form.choose value, **options
end

#click_button(action = nil, **options) ⇒ Object



81
82
83
84
85
86
87
88
89
90
# File 'lib/with_form/model_form.rb', line 81

def click_button(action = nil, **options)
  if action.present?
  elsif @model.persisted?
    action = :update
  else
    action = :create
  end

  scope_form.click_button action, **options
end

#fill_in(attribute, with: nil, **options) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/with_form/model_form.rb', line 10

def fill_in(attribute, with: nil, **options)
  scope_form.fill_in(
    attribute,
    with: with || @model.public_send(attribute),
    **options,
  )
end

#select(attribute, from: nil, **options) ⇒ Object



59
60
61
62
63
64
65
66
67
68
# File 'lib/with_form/model_form.rb', line 59

def select(attribute, from: nil, **options)
  case attribute
  when Symbol
    value = @model.public_send(attribute)
  else
    value = attribute
  end

  scope_form.select value, from: from || attribute, **options
end

#uncheck(attribute, **options) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/with_form/model_form.rb', line 48

def uncheck(attribute, **options)
  case attribute
  when Symbol
    values = Array(@model.public_send(attribute))
  else
    values = Array(attribute)
  end

  values.each { |value| scope_form.uncheck(value, **options) }
end

#unselect(attribute, from: nil, **options) ⇒ Object



70
71
72
73
74
75
76
77
78
79
# File 'lib/with_form/model_form.rb', line 70

def unselect(attribute, from: nil, **options)
  case attribute
  when Symbol
    value = @model.public_send(attribute)
  else
    value = attribute
  end

  scope_form.unselect value, from: from || attribute, **options
end