-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from pantographe/check-box-component
Add Form::CheckBoxComponent
- Loading branch information
Showing
2 changed files
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# frozen_string_literal: true | ||
|
||
module ViewComponent | ||
module Form | ||
class CheckBoxComponent < FieldComponent | ||
attr_reader :checked_value, :unchecked_value | ||
|
||
def initialize(form, object_name, method_name, checked_value, unchecked_value, options = {}) # rubocop:disable Metrics/ParameterLists | ||
@checked_value = checked_value | ||
@unchecked_value = unchecked_value | ||
|
||
super(form, object_name, method_name, options) | ||
end | ||
|
||
def call | ||
ActionView::Helpers::Tags::CheckBox.new( | ||
object_name, | ||
method_name, | ||
form, | ||
checked_value, | ||
unchecked_value, | ||
options | ||
).render | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe ViewComponent::Form::CheckBoxComponent, type: :component do | ||
let(:object) { OpenStruct.new } | ||
let(:form) { form_with(object) } | ||
let(:options) { {} } | ||
|
||
let(:component) { render_inline(described_class.new(form, object_name, :admin, "1", "0", options)) } | ||
let(:component_html_attributes) { component.css("input").last.attributes } | ||
|
||
context "with simple args" do | ||
it do | ||
expect(component.to_html).to eq( | ||
%(<input name="user[admin]" type="hidden" value="0">) + | ||
%(<input type="checkbox" value="1" name="user[admin]" id="user_admin">) | ||
) | ||
end | ||
end | ||
|
||
include_examples "component with custom html classes" | ||
include_examples "component with custom data attributes" | ||
end |