Skip to content

Commit

Permalink
Expose required fields on form (#1521)
Browse files Browse the repository at this point in the history
Displays an asterisk beside label on form when field is required

Issue: #1489

![Captura de Tela 2020-01-09 às 17 20 38](https://user-images.githubusercontent.com/54637230/72172195-55363580-33b3-11ea-8475-edb4520b276d.png)
  • Loading branch information
davi-tapajos-codeminer42 authored and pablobm committed Jan 25, 2020
1 parent e7a086a commit b9a6d6d
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
Expand Up @@ -34,3 +34,10 @@
width: calc(25% - 1rem);
}
}

.field-unit--required {
label::after {
color: $red;
content: " *";
}
}
19 changes: 19 additions & 0 deletions app/helpers/administrate/application_helper.rb
Expand Up @@ -15,6 +15,25 @@ def render_field(field, locals = {})
render locals: locals, partial: field.to_partial_path
end

def requireness(field)
required_field?(field) ? "required" : "optional"
end

def required_field?(field)
has_presence_validator?(field.resource.class, field.attribute)
end

def has_presence_validator?(resource_class, field_name)
validators_on(resource_class, field_name).
any? { |v| v.class == ActiveRecord::Validations::PresenceValidator }
end

def validators_on(resource_class, field_name)
return [] unless resource_class.respond_to?(:validators_on)

resource_class.validators_on(field_name)
end

def class_from_resource(resource_name)
resource_name.to_s.classify.constantize
end
Expand Down
2 changes: 1 addition & 1 deletion app/views/administrate/application/_form.html.erb
Expand Up @@ -34,7 +34,7 @@ and renders all form fields for a resource's editable attributes.
<% end %>
<% page.attributes.each do |attribute| -%>
<div class="field-unit field-unit--<%= attribute.html_class %>">
<div class="field-unit field-unit--<%= attribute.html_class %> field-unit--<%= requireness(attribute) %>">
<%= render_field attribute, f: f %>
</div>
<% end -%>
Expand Down
28 changes: 28 additions & 0 deletions spec/helpers/administrate/application_helper_spec.rb
Expand Up @@ -54,6 +54,34 @@
end
end

describe "#requireness" do
let(:page) do
Administrate::Page::Form.new(Blog::PostDashboard.new, Blog::Post.new)
end

it "returns 'required' if field is required" do
title = page.attributes.detect { |i| i.attribute == :title }
expect(requireness(title)).to eq("required")
end

it "returns 'optional' if field is not required" do
publish_at = page.attributes.detect { |i| i.attribute == :published_at }
expect(requireness(publish_at)).to eq("optional")
end
end

describe "#has_presence_validator?" do
it "returns true if field is required" do
required = has_presence_validator?(Blog::Post, :title)
expect(required).to eq(true)
end

it "returns false if field is not required" do
required = has_presence_validator?(Blog::Post, :publish_at)
expect(required).to eq(false)
end
end

describe "#sort_order" do
it "sanitizes to ascending/descending/none" do
expect(sort_order("asc")).to eq("ascending")
Expand Down

0 comments on commit b9a6d6d

Please sign in to comment.