Class: WithForm::ModelForm
- Inherits:
-
Object
- Object
- WithForm::ModelForm
- Includes:
- ActionView::Helpers::TranslationHelper
- Defined in:
- lib/with_form/model_form.rb
Instance Method Summary collapse
- #attach_file(attribute, path = nil, **options) ⇒ Object
- #check(attribute, **options) ⇒ Object
- #choose(attribute, **options) ⇒ Object
- #click_button(action = nil, **options) ⇒ Object
- #fill_in(attribute, with: nil, **options) ⇒ Object
-
#initialize(model:, page:) ⇒ ModelForm
constructor
A new instance of ModelForm.
- #select(attribute, from: nil, **options) ⇒ Object
- #uncheck(attribute, **options) ⇒ Object
- #unselect(attribute, from: nil, **options) ⇒ Object
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, **) scope_form.attach_file( attribute, path || @model.public_send(attribute), **, ) 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, **) case attribute when Symbol values = Array(@model.public_send(attribute)) else values = Array(attribute) end values.each { |value| scope_form.check(value, **) } 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, **) case attribute when Symbol value = @model.public_send(attribute) else value = attribute end scope_form.choose value, ** 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 (action = nil, **) if action.present? elsif @model.persisted? action = :update else action = :create end scope_form. action, ** 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, **) scope_form.fill_in( attribute, with: with || @model.public_send(attribute), **, ) 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, **) case attribute when Symbol value = @model.public_send(attribute) else value = attribute end scope_form.select value, from: from || attribute, ** 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, **) case attribute when Symbol values = Array(@model.public_send(attribute)) else values = Array(attribute) end values.each { |value| scope_form.uncheck(value, **) } 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, **) case attribute when Symbol value = @model.public_send(attribute) else value = attribute end scope_form.unselect value, from: from || attribute, ** end |