-
-
Notifications
You must be signed in to change notification settings - Fork 257
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feature: radio field * feature specs * pluggy radio constant
- Loading branch information
1 parent
adb68ea
commit 9d85d0f
Showing
12 changed files
with
118 additions
and
2 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,21 @@ | ||
module Avo | ||
module Fields | ||
class RadioField < BaseField | ||
def initialize(id, **args, &block) | ||
super(id, **args, &block) | ||
|
||
@options = args[:options] || {} | ||
end | ||
|
||
def options | ||
Avo::ExecutionContext.new( | ||
target: @options, | ||
record: record, | ||
resource: resource, | ||
view: view, | ||
field: self | ||
).handle | ||
end | ||
end | ||
end | ||
end |
10 changes: 10 additions & 0 deletions
10
app/components/avo/fields/radio_field/edit_component.html.erb
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,10 @@ | ||
<%= field_wrapper **field_wrapper_args do %> | ||
<div class="flex flex-col gap-2"> | ||
<% @field.options.each do |key, value| %> | ||
<div> | ||
<%= form.radio_button @field.id, key %> | ||
<%= form.label @field.id, value: value %> | ||
</div> | ||
<% end %> | ||
</div> | ||
<% 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,4 @@ | ||
# frozen_string_literal: true | ||
|
||
class Avo::Fields::RadioField::EditComponent < Avo::Fields::EditComponent | ||
end |
3 changes: 3 additions & 0 deletions
3
app/components/avo/fields/radio_field/index_component.html.erb
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,3 @@ | ||
<%= index_field_wrapper **field_wrapper_args do %> | ||
<%== @field.value %> | ||
<% 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,4 @@ | ||
# frozen_string_literal: true | ||
|
||
class Avo::Fields::RadioField::IndexComponent < Avo::Fields::IndexComponent | ||
end |
3 changes: 3 additions & 0 deletions
3
app/components/avo/fields/radio_field/show_component.html.erb
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,3 @@ | ||
<%= field_wrapper **field_wrapper_args do %> | ||
<%== @field.value %> | ||
<% 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,4 @@ | ||
# frozen_string_literal: true | ||
|
||
class Avo::Fields::RadioField::ShowComponent < Avo::Fields::ShowComponent | ||
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
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
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,5 @@ | ||
class AddSizeToFishes < ActiveRecord::Migration[6.1] | ||
def change | ||
add_column :fish, :size, :string | ||
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
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,60 @@ | ||
require "rails_helper" | ||
|
||
RSpec.describe "RadioField", type: :feature do | ||
describe "when size is present" do | ||
let!(:fish) { create :fish, size: "small" } | ||
|
||
context "index" do | ||
it "displays the fish name" do | ||
visit "/admin/resources/fish" | ||
|
||
expect(page).to have_text "Size" | ||
expect(page).to have_text fish.size | ||
end | ||
end | ||
|
||
context "show" do | ||
it "displays the fish size" do | ||
visit "/admin/resources/fish/#{fish.id}" | ||
|
||
expect(page).to have_text fish.size | ||
end | ||
end | ||
|
||
context "edit" do | ||
it "changes the fish size" do | ||
visit "/admin/resources/fish/#{fish.id}/edit" | ||
|
||
expect(page).to have_checked_field "fish_size_small" | ||
expect(page).to_not have_checked_field "fish_size_medium" | ||
expect(page).to_not have_checked_field "fish_size_large" | ||
|
||
find("#fish_size_large").click | ||
|
||
expect(page).to_not have_checked_field "fish_size_small" | ||
expect(page).to_not have_checked_field "fish_size_medium" | ||
expect(page).to have_checked_field "fish_size_large" | ||
|
||
save | ||
|
||
fish.reload | ||
|
||
expect(fish.size).to eq "large" | ||
end | ||
end | ||
end | ||
|
||
describe "when size is nil" do | ||
let!(:fish) { create :fish, size: nil } | ||
|
||
context "edit" do | ||
it "does not check radio buttons" do | ||
visit "/admin/resources/fish/#{fish.id}/edit" | ||
|
||
expect(page).to_not have_checked_field "fish_size_small" | ||
expect(page).to_not have_checked_field "fish_size_medium" | ||
expect(page).to_not have_checked_field "fish_size_large" | ||
end | ||
end | ||
end | ||
end |