Skip to content

Commit

Permalink
Handle form object being false
Browse files Browse the repository at this point in the history
  • Loading branch information
mcasper committed Oct 3, 2024
1 parent 2ccb020 commit 517cfce
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
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

0 comments on commit 517cfce

Please sign in to comment.