Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle form object being false #173

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
Nothing yet
### Fixed
- Fix `object_errors` when `object` is `false` (#173)

## [0.2.8] - 2024-08-20
### Added
Expand Down
7 changes: 6 additions & 1 deletion app/components/view_component/form/base_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ class << self
attr_reader :form, :object_name, :options

delegate :object, to: :form, allow_nil: true
delegate :errors, to: :object, prefix: true, allow_nil: true

def initialize(form, object_name, options = {})
@form = form
Expand All @@ -30,6 +29,12 @@ def object_errors?
object.errors.any?
end

def object_errors
return nil unless object

object.errors
end

def html_class
nil
end
Expand Down
6 changes: 6 additions & 0 deletions spec/view_component/form/field_component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ def name

it { expect(component.method_errors?).to be(false) }
end

context "with false" do
let(:object) { false }

it { expect(component.method_errors?).to be(false) }
end
end

describe "#value" do
Expand Down
Loading