Oct92009

Working with namepaced model

Comments Off

Sometime we have to work with namespaced model in Ruby on Rails to keep our code clean.
For example:
I have model named as “Attandance.rb”,
Sometime to keep our code clean we have to put them in custom dir.
This scenario is normal while working with two or three modules.
for ex. I have three module in a project for school.
1. Admin module, Teachers module and a Student module.

So it is better to put all stuff that belongs to teacher in a custom dir named “Teachers”

Be sure that folder name does not match any model that have been put under this folder. I prefer to use plural names for these directories.

But now begins the real headache.
to access model under the “Teachers” directory I have to write this

Teachers::Attendance.find and all that.
And for sure it will be a headache while translating with I18n.

But this violates Ruby’s very own DRY approach.

So to keep being DRY do the below written things.

1
2
3
4
#Open config/environment.rb file.
#Write
config.load_paths += %W(#{RAILS_ROOT}/app/models/teachers,
                        #{RAILS_ROOT}/app/models/ur_custom_dir)

And you are done.
now you can have access to these model like others
Attendance.find()
and while translating you can write normally as you do whith other models.

1
2
3
4
5
6
hi:
  activerecord:
    attributes:
      attendance:
        teacher_id: “xE0\xA4\x97?
        name: ” \xE0\xA4\x94\xE0\xA4\xB0 “

Don’t go on Unicode they are not real.

This is a little thing but it solves many problem that can come in your way.

Happy boarding.

Oct62009

Translating Date and DateTime in Rails

Comments Off

I was using newdesk’s translate plugin for generating unicode. But there was no way by i can translate date and date_time.

So I had to find another way.

Here I am telling you guys what i did

1
2
3
4
5
6
7
#Just go to locale file (must be in config/locales)
#go to the ‘date’ key
#place the appropriate hex Unicode under
abbr_day_names
month_names
day_names
abbr_month_names

And here you go you will be wondering to see date_select and date_time_select in your local language.

And don’t worry about ActiveRecord. It will not bother you.

Happy Boarding.

Get Adobe Flash playerPlugin by wpburn.com wordpress themes