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

Custom labels for fields #451

Closed
Xosmond opened this issue Feb 3, 2016 · 16 comments
Closed

Custom labels for fields #451

Xosmond opened this issue Feb 3, 2016 · 16 comments

Comments

@Xosmond
Copy link

Xosmond commented Feb 3, 2016

Hi, i have some models with short name fields and i want to change the label of some with other more verbose, how can i change the labels of some string fields? Thanks all.

@eimhin
Copy link

eimhin commented Feb 24, 2016

+1

@atwoodjw
Copy link

You can do this via localization.

config/locales/en.yml

en:
  helpers:
    label:
      admin_user:
        password_confirmation: "Confirmation"

@Xosmond
Copy link
Author

Xosmond commented Feb 28, 2016

I have tried but does not work 😕. Do you have any project where you have did it to share?

@c-lliope c-lliope reopened this Feb 29, 2016
c-lliope added a commit that referenced this issue Feb 29, 2016
Problem:

There was uncertainty about how to create custom labels for fields.
In #451,
it was suggested that we could use I18n translations
to solve the problem.

If we accept that as the standard way to set custom labels,
we should have a test for it.

Solution:

- Add a test to verify that labels can be customized through I18n.
- Add documentation about how to customize labels

To Do:

- Update view code so the same translation definition
  applies on the show and collection pages,
  in addition to the form page.
@c-lliope
Copy link
Contributor

@Xosmond - I just added a test to verify that @atwoodjw's approach works: #492

Can you post the code you're using that isn't working?

c-lliope added a commit that referenced this issue Mar 8, 2016
Problem:

There was uncertainty about how to create custom labels for fields.
In #451,
it was suggested that we could use I18n translations
to solve the problem.

If we accept that as the standard way to set custom labels,
we should have a test for it.

Solution:

- Add a test to verify that labels can be customized through I18n.
- Add documentation about how to customize labels

To Do:

- Update view code so the same translation definition
  applies on the show and collection pages,
  in addition to the form page.
@Xosmond
Copy link
Author

Xosmond commented Mar 12, 2016

Sorry for the late. This solution works only on normal attributes but on relations doesn't work:
en.yml

  helpers:
    label:
      control1:
        registro1s: "Registros"
        nombre_documento: "titulo"

Here, the "nombre_documento" is a string attribute of control1 and It works. But "registro1s" is a relation, because control1 has many registro1s and it doesn't work.

Any solution for this?

@KatherineMuedas
Copy link

I am trying this but it does not work. Do I need to create an administrate.en.yml file inside locales or can I just add it to my en.yml file ?

@Xosmond
Copy link
Author

Xosmond commented Apr 8, 2016

Just in the en.yml , make sure you are not using i18n on your config/application.rb

@KatherineMuedas
Copy link

Ok, In my en.yml file I added this. (school is my model and tuition fee is the attribute )

en:
  helpers:
    label:
      school:
        tuition_fee: "Tuition fee average"

And my dashboard is like this

class SchoolDashboard < Administrate::BaseDashboard
  ATTRIBUTE_TYPES = {
    tuition_fee: Field::Number.with_options(prefix: "AED ", decimals: 2),
  }

Is that all I need to make it work ? I am trying to customize the label of the tuition fee attribute.

@Xosmond
Copy link
Author

Xosmond commented Apr 9, 2016

Have you tried with only "Field::Number" ? maybe the kind of attribute is the key.

@KatherineMuedas
Copy link

It is only working on the form page but it is not working for the show page or index page.

@Xosmond
Copy link
Author

Xosmond commented Apr 12, 2016

it could be, because the way this is working. I hope they can make a better option to customize labels.

@c-lliope
Copy link
Contributor

@Xosmond, @KatherineMuedas - ah, I didn't realize that you were talking about the index and show pages.

I've updated #492 to work on all three types of pages. I'll try to get that merged in as soon as possible.

@KatherineMuedas
Copy link

@Graysonwright that would be great !! Thanks :)

c-lliope added a commit that referenced this issue Apr 12, 2016
Problem:

There was uncertainty about how to create custom labels for fields.
In #451,
it was suggested that we could use I18n translations
to solve the problem.

If we accept that as the standard way to set custom labels,
we should have a test for it.

Solution:

- Add a test to verify that labels can be customized through I18n.
- Add documentation about how to customize labels

To Do:

- Update view code so the same translation definition
  applies on the show and collection pages,
  in addition to the form page.
@c-lliope
Copy link
Contributor

c-lliope commented May 3, 2016

Closed by #492

@c-lliope c-lliope closed this as completed May 3, 2016
@jondkinney
Copy link

The custom labels don't work in the forms as far as I can tell. They really can't either, because the forms render the attribute's key directly <%= f.label field.attribute_key, field.attribute %> like so:

<%#
# HasMany Form Partial

This partial renders an input element for belongs_to relationships.
By default, the input is a collection select box
that displays all possible records to associate with.
The collection select box supports multiple inputs,
and is augmented with [Selectize].

## Local variables:

- `f`:
  A Rails form generator, used to help create the appropriate input fields.
- `field`:
  An instance of [Administrate::Field::HasMany][1].
  Contains helper methods for displaying a collection select box.

[1]: http://www.rubydoc.info/gems/administrate/Administrate/Field/HasMany
[Selectize]: http://brianreavis.github.io/selectize.js
%>

<div class="field-unit__label">
  <%= f.label field.attribute_key, field.attribute %>
</div>
<div class="field-unit__field">
  <%= f.select(field.attribute_key, nil, {}, multiple: true) do %>
    <%= options_for_select(field.associated_resource_options, field.selected_options) %>
  <% end %>
</div>

However, if we add the same translate call that the collection partial does we can get what we need...

<%#
# HasMany Form Partial

This partial renders an input element for belongs_to relationships.
By default, the input is a collection select box
that displays all possible records to associate with.
The collection select box supports multiple inputs,
and is augmented with [Selectize].

## Local variables:

- `f`:
  A Rails form generator, used to help create the appropriate input fields.
- `field`:
  An instance of [Administrate::Field::HasMany][1].
  Contains helper methods for displaying a collection select box.

[1]: http://www.rubydoc.info/gems/administrate/Administrate/Field/HasMany
[Selectize]: http://brianreavis.github.io/selectize.js
%>

<div class="field-unit__label">
  <% label_text = t(
    "helpers.label.#{f.object.class.name.underscore}.#{field.attribute_key}",
    default: field.attribute.to_s,
  ).titleize %>

  <%= f.label field.attribute_key, label_text %>
</div>
<div class="field-unit__field">
  <%= f.select(field.attribute_key, nil, {}, multiple: true) do %>
    <%= options_for_select(field.associated_resource_options, field.selected_options) %>
  <% end %>
</div>

I'll make a PR with this in the next week or two when I get time. Deploying with my custom fork for now.

@jondkinney
Copy link

Here's an example for number (it's the same for string)

<%#
# Number Form Partial

This partial renders an input element for number attributes.
By default, the input is a text field.

## Local variables:

- `f`:
  A Rails form generator, used to help create the appropriate input fields.
- `field`:
  An instance of [Administrate::Field::Number][1].
  A wrapper around the number pulled from the database.

[1]: http://www.rubydoc.info/gems/administrate/Administrate/Field/Number
%>

<div class="field-unit__label">
  <% label_text = t(
    "helpers.label.#{f.object.class.name.underscore}.#{field.attribute}",
    default: field.attribute.to_s,
  ).titleize %>

  <%= f.label label_text %>
</div>
<div class="field-unit__field">
  <%= f.text_field field.attribute %>
</div>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants