Skip to content

Commit

Permalink
Merge 0c89d37 into a70b2fe
Browse files Browse the repository at this point in the history
  • Loading branch information
ideadapt committed Oct 24, 2013
2 parents a70b2fe + 0c89d37 commit f8d52ba
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 6 deletions.
3 changes: 3 additions & 0 deletions config/locales/rails_admin.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ en:
confirmation: "Yes, I'm sure"
bulk_delete: "The following objects will be deleted, which may delete or orphan some of their related dependencies:"
new_model: "%{name} (new)"
help:
team:
name: "Team Name Help Text."
export:
confirmation: "Export to %{name}"
select: "Select fields to export"
Expand Down
13 changes: 12 additions & 1 deletion lib/rails_admin/config/fields/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def virtual?

# Accessor for field's help text displayed below input field.
register_instance_option :help do
(@help ||= {})[::I18n.locale] ||= (required? ? I18n.translate("admin.form.required") : I18n.translate("admin.form.optional")) + '. '
(@help ||= {})[::I18n.locale] ||= generic_field_help
end

register_instance_option :html_attributes do
Expand Down Expand Up @@ -274,6 +274,17 @@ def value
[method_name]
end

def generic_help
(required? ? I18n.translate("admin.form.required") : I18n.translate("admin.form.optional")) + '. '
end

def generic_field_help
model = abstract_model.model_name.underscore
model_lookup = "admin.help.#{model}.#{name}".to_sym
translated = I18n.translate(model_lookup, help: generic_help, default: [generic_help])
(translated.is_a?(Hash) ? translated.to_a.first[1] : translated).html_safe
end

def parse_input(params)
# overriden
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rails_admin/config/fields/types/boolean.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Boolean < RailsAdmin::Config::Fields::Base
end

# Accessor for field's help text displayed below input field.
register_instance_option :help do
def generic_help
""
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rails_admin/config/fields/types/bson_object_id.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class BsonObjectId < RailsAdmin::Config::Fields::Types::String
label
end

register_instance_option :help do
def generic_help
"BSON::ObjectId"
end

Expand Down
2 changes: 1 addition & 1 deletion lib/rails_admin/config/fields/types/hidden.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Hidden < RailsAdmin::Config::Fields::Base
false
end

register_instance_option :help do
def generic_help
false
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rails_admin/config/fields/types/string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class String < RailsAdmin::Config::Fields::Base
}
end

register_instance_option :help do
def generic_help
text = (required? ? I18n.translate("admin.form.required") : I18n.translate("admin.form.optional")) + '. '
if valid_length.present? && valid_length[:is].present?
text += "#{I18n.translate("admin.form.char_length_of").capitalize} #{valid_length[:is]}."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ class HelpTest < Tableless
visit new_path(:model_name => "team")
expect(find("#team_manager_field .help-block")).to have_content("Optional")
expect(find("#team_division_id_field .help-block")).to have_content("Optional")
expect(find("#team_name_field .help-block")).to have_content("Required")
expect(find("#team_name_field .help-block")).to have_content(I18n.translate("admin.help.team.name"))
end

it "can hide the add button on an associated field" do
Expand Down
15 changes: 15 additions & 0 deletions spec/rails_admin/config/fields/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,21 @@ class CommentReversed < Tableless
end
end

describe "#help" do
it "has a default and be user customizable via i18n" do
RailsAdmin.config Team do
list do
field :division
field :name
end
end
field_specific_i18n = RailsAdmin.config('Team').list.fields.find{|f| f.name == :name}
expect(field_specific_i18n.help).to eq(I18n.translate("admin.help.team.name")) # custom via locales yml
field_no_specific_i18n = RailsAdmin.config('Team').list.fields.find{|f| f.name == :division}
expect(field_no_specific_i18n.help).to eq(field_no_specific_i18n.generic_help) # rails_admin generic fallback
end
end

describe "#css_class" do
it "has a default and be user customizable" do
RailsAdmin.config Team do
Expand Down

0 comments on commit f8d52ba

Please sign in to comment.