class WithForm::ModelForm

Public Class Methods

new(model:, page:) click to toggle source
# File lib/with_form/model_form.rb, line 5
def initialize(model:, page:)
  @page = page
  @model = model
end

Public Instance Methods

attach_file(attribute, path = nil, **options) click to toggle source
# File lib/with_form/model_form.rb, line 26
def attach_file(attribute, path = nil, **options)
  scope_form.attach_file(
    attribute,
    path || @model.public_send(attribute),
    **options,
  )
end
check(attribute, **options) click to toggle source
# File lib/with_form/model_form.rb, line 44
def check(attribute, **options)
  if attribute.kind_of? Symbol
    value = read_attribute(attribute)
  else
    value = attribute
  end

  Array(value).each { |value| scope_form.check(value, **options) }
end
choose(attribute, **options) click to toggle source
# File lib/with_form/model_form.rb, line 34
def choose(attribute, **options)
  if attribute.kind_of? Symbol
    value = read_attribute(attribute)
  else
    value = attribute
  end

  scope_form.choose value, **options
end
click_button(action = nil, **options) click to toggle source
# File lib/with_form/model_form.rb, line 84
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) click to toggle source
# 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
fill_in_rich_text_area(attribute, with: nil, **options) click to toggle source
# File lib/with_form/model_form.rb, line 18
def fill_in_rich_text_area(attribute, with: nil, **options)
  scope_form.fill_in_rich_text_area(
    attribute,
    with: with || @model.public_send(attribute),
    **options,
  )
end
select(attribute, from: nil, **options) click to toggle source
# File lib/with_form/model_form.rb, line 64
def select(attribute, from: nil, **options)
  if attribute.kind_of? Symbol
    value = read_attribute(attribute)
  else
    value = attribute
  end

  scope_form.select value, from: from || attribute, **options
end
uncheck(attribute, **options) click to toggle source
# File lib/with_form/model_form.rb, line 54
def uncheck(attribute, **options)
  if attribute.kind_of? Symbol
    value = read_attribute(attribute)
  else
    value = attribute
  end

  Array(value).each { |value| scope_form.uncheck(value, **options) }
end
unselect(attribute, from: nil, **options) click to toggle source
# File lib/with_form/model_form.rb, line 74
def unselect(attribute, from: nil, **options)
  if attribute.kind_of? Symbol
    value = read_attribute(attribute)
  else
    value = attribute
  end

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

Private Instance Methods

read_attribute(attribute) click to toggle source
# File lib/with_form/model_form.rb, line 101
def read_attribute(attribute)
  attribute_value = @model.public_send(attribute)

  if attribute_value.in?([true, false, nil])
    attribute
  else
    attribute_value
  end
end
scope_form() click to toggle source
# File lib/with_form/model_form.rb, line 97
def scope_form
  WithForm::ScopeForm.new(scope: @model.model_name.i18n_key, page: @page)
end