declarative translation in List and Show views

0 views
Skip to first unread message

EBIHARA, Yuichiro

unread,
Oct 1, 2009, 2:23:18 AM10/1/09
to actives...@googlegroups.com
Hi,

My model has a "status" column to store the status of approval.
The value can be one of "A" for approved, "R" for rejected and "P" for pending.
I'd like to let users choose a value from a select list and I added
the following lines in my controller.

config.columns[:status].form_ui = :select
config.columns[:status].options = {:options => [["Approved", "A"],
["Rejected", "R"], ["Pending", "P"]]}

It works in Update view as I expect.
Now I want the translated string ("Approved", "Rejected", "Pending")
to be displayed in List and Show views, too.
Is it possible to implement it "declaratively", instead of using field override?

Thanks in advance,

ebi

Gabriel Sobrinho

unread,
Oct 1, 2009, 12:54:51 AM10/1/09
to actives...@googlegroups.com
Hi,

I usually do that:

class MyModel
  STATUSES = [
    [human_attribute_name('approved'), 'A'],
    [human_attribute_name('rejected'), 'R'],
    [human_attribute_name('pending'), 'P']
  ]
end

class MyController
  active_scaffold do |config|

    config.columns[:status].form_ui = :select
    config.columns[:status].options = MyModel::STATUSES
  end
end

module MyControllerHelper
  def status_column record
    MyModel.human_attribute_name record.status
  end
end

But I has too much code and I don't know another way. This works like a enum field, I never thinked about simplify that before.

I will wait for other's users suggestions.
-- 
Cordialmente,

Gabriel Sobrinho

Diretor de desenvolvimento

Hite Comunicação Digital e Mídia Interativa
http://www.hite.com.br/

+55 31 8775 8378

EBIHARA, Yuichiro

unread,
Oct 1, 2009, 7:27:22 AM10/1/09
to actives...@googlegroups.com
Gabriel,

Thank you for your reply.
Your idea looks better than my current implementation.

By the way, I have validates_inclusion_of command in my model as follows.

class MyModel < ActiveRecord::Base
  validates_inclusion_of :status, :in => ["A", "R", "P"]
end

If I defined STATUSES as Gabriel proposes, it would be no longer DRY.
Does anyone have a good idea to keep things DRY?

Thanks,

ebi

2009/10/1 Gabriel Sobrinho <gabriel....@gmail.com>

Sergio Cambra .:: entreCables S.L. ::.

unread,
Oct 1, 2009, 9:20:11 AM10/1/09
to actives...@googlegroups.com
On Jueves, 1 de Octubre de 2009 13:27:22 EBIHARA, Yuichiro escribió:
> Gabriel,
>
> Thank you for your reply.
> Your idea looks better than my current implementation.
>
> By the way, I have validates_inclusion_of command in my model as follows.
>
> class MyModel < ActiveRecord::Base
> validates_inclusion_of :status, :in => ["A", "R", "P"]
> end

Change :in => ["A", "R", "P"] with STATUSES.collect{|string, key| key}
> > Hite Comunicação Digital e Mídia Interativahttp://www.hite.com.br/
> >
> > +55 31 8775 8378
>
>
--
Sergio Cambra .:: entreCables S.L. ::.
Mariana Pineda 23, 50.018 Zaragoza
T) 902 021 404 F) 976 52 98 07 E) ser...@entrecables.com

EBIHARA, Yuichiro

unread,
Oct 1, 2009, 10:28:41 PM10/1/09
to actives...@googlegroups.com
Wow, great! Thanks!

ebi

2009/10/1 Sergio Cambra .:: entreCables S.L. ::. <ser...@entrecables.com>:

Gabriel Sobrinho

unread,
Oct 2, 2009, 11:47:03 AM10/2/09
to actives...@googlegroups.com
@Yuichiro

I made a small helper for AS to solve this. I will convert this into a plugin soon, but for now this could be useful.

You need to create a initializer file with this content: http://pastie.org/private/7bwdwqwc7jemz1aharpa

Then you use like I said:

class Model
  VALUES = [
    [human_attribute_name('one'), 'one'],
    [human_attribute_name('two'), 'two'],
  ].freeze
end

class Controller
  active_scaffold do |config|
    config.columns[:name].form_ui = :enum
    config.columns[:name].options = Model::VALUES
  end
end


And you dont need to create a helper to parse the column. The helper was I made will do it using the options value.


Regards

Gabriel Sobrinho

unread,
Oct 2, 2009, 2:50:37 PM10/2/09
to actives...@googlegroups.com
I made the plugin, look here: http://github.com/sobrinho/as_enum_column

EBIHARA, Yuichiro

unread,
Oct 5, 2009, 12:06:38 AM10/5/09
to actives...@googlegroups.com
Gabriel,

It works!
Thank you very much!

ebi


2009/10/3 Gabriel Sobrinho <gabriel....@gmail.com>
Reply all
Reply to author
Forward
0 new messages