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

Fix custom fields checkbox list #1318

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
4 changes: 1 addition & 3 deletions app/models/fields/field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Field < ActiveRecord::Base
serialize :collection, Array
serialize :settings, HashWithIndifferentAccess

belongs_to :field_group, optional: true # TODO: Is this really optional?
belongs_to :field_group, optional: true

scope :core_fields, -> { where(type: 'CoreField') }
scope :custom_fields, -> { where("type != 'CoreField'") }
Expand Down Expand Up @@ -92,8 +92,6 @@ def render_value(object)

def render(value)
case as
when 'checkbox'
value.to_s == '0' ? "no" : "yes"
when 'date'
value&.strftime(I18n.t("date.formats.mmddyy"))
when 'datetime'
Expand Down
5 changes: 4 additions & 1 deletion app/views/admin/custom_fields/_check_boxes_field.html.haml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
%div
.label.top.req
= "Select Options (pipe separated):"
= f.text_field :collection_string, class: 'field_collection_string', size: 78
= f.text_field :collection_string, class: 'field_collection_string', size: 78, placeholder: "Option 1|Option 2|Option 3"

= render partial: 'admin/custom_fields/base_field', locals: {f: f}

- if f.object.new_record?
.info2 After saving, you must restart all instances of the Rails server to apply column serialization.
5 changes: 1 addition & 4 deletions app/views/fields/_group_table.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@
%td{class: (f.object.errors[field.name].present? ? 'fieldWithErrors' : nil)}
- if field.hint.present?
= image_tag "info_tiny.png", title: field.hint, class: "tooltip-icon"
- if field.as == 'check_boxes'
- value = f.object.send(field.name)
- checked = YAML.load(value.to_s)
.label.top{class: (field.required? ? 'req': nil)}
= "#{field.label}:"
= f.input_field field.name, field.input_options.merge(checked: checked)
= f.input_field field.name, field.input_options
- if i == 0
%td= spacer
31 changes: 17 additions & 14 deletions spec/models/fields/field_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,24 @@
expect(Field.new.input_options).to be_a(Hash)
end

it "should be able to display a empty multi_select value" do
field = Field.new(
label: "Availability",
name: "availability"
)
object = double('Object')
context "render" do
let(:field) { FactoryBot.create(:field, as: as) }

Check notice

Code scanning / Rubocop

Prefer Ruby 1.9 hash syntax { a: 1, b: 2 } over 1.8 syntax { :a => 1, :b => 2 }. Note test

Style/HashSyntax: Omit the hash value.

context "check_boxes" do
let(:as) { "check_boxes" }
it { expect(field.render([1, 2, 3])).to eql("1, 2<br />3") }
it { expect(field.render([1, 2, 3])).to eql("1, 2<br />3") }
end

# as | value | expected
[["check_boxes", [1, 2, 3], "1, 2<br />3"],
%w[checkbox 0 no],
["checkbox", 1, "yes"],
["date", Time.parse('2011-04-19'), Time.parse('2011-04-19').strftime(I18n.t("date.formats.mmddyy"))]].each do |as, value, expected|
field.as = as
allow(object).to receive(field.name).and_return(value)
expect(field.render_value(object)).to eq(expected)
context "date" do
let(:as) { "date" }
it { expect(field.render(Time.parse('2011-04-19'))).to eql("Apr 19, 2011") }
end

context "datetime" do
let(:as) { "datetime" }
it { expect(field.render(Time.parse('2011-04-19 14:47 +0000'))).to eql("19 Apr 2011 at 2:47PM") }
end
end

Check notice

Code scanning / Rubocop

Keeps track of empty lines around block bodies. Note test

Layout/EmptyLinesAroundBlockBody: Extra empty line detected at block body end.
end
Loading