In my Rails app I need to maintain too many default texts. For example, I have a Hotel model. When someone creates a hotel, some default email templates, default sms templates get created automatically for that hotel. Currently I am maintaining them inside a Constants modules and access them like:
Constants::DEFAULT_RESERVATION_EMAIL_TEMPLATE
Constants::DEFAULT_RESERVATION_SMS_TEMPLATE
etc.
I wonder if there any other convenient and efficient ways to maintain those default texts as it seems Constants module is going huge in each days. I am thinking like I can manage them in a yaml file so that it does not affect on memory and I can read from the yaml file when necessary.
I would use the normal i18n yml files for that. You can manage multiple languages through yml files. And you can easily change the content.
But you also can use a database for your backend.
Following links could give you an idea:
http://asciicasts.com/episodes/256-i18n-backends
Rails: store translations in database
http://franck.verrot.fr/blog/2010/02/27/rails-3-let-activerecord-manage-your-translations/
https://github.com/svenfuchs/globalize3 (if you also have default texts for model attributes, you can define them in a yml file and save them in the database after create, or something similar..)
Related
I'm developing a site with Ruby on Rails. I have data on layout that doesn't repeat more then once. And I need to enable content-menegers to change it easily. I could create a model or several models for them and let content-managers edit it in RailsAdmin. But I doubt I should use models that way because entities of such models will be ment to exist in singular number. I mean there aren't multple site names, logos, backgrounds, welcoming texts, etc. But I want to enable them to be edited in a web-interface or equally comfortable not worrying about the cache and not looking at code and serfing through complicated directories structure.
What is the right way to do that?
I'm currently constructing a Rails site to edit a collection of files used for configuration of various services. The files are simple plain text files. The point of the site is to provide an easy interface for editing the files as well as validate changes for the less technically incline individuals who will be editing them.
I've looked around but I can't seem to find anything on using text files instead of a database. What I do find suggests that what I'm trying to do may not be correct for rails at all. The closest thing have is this question, but the answers are less than helpful.
Is there a correct way to create an MVC for text files and not use a database at all?
Yes. Rails provides MVC with activerecord being default for models. you can keep rails views and controllers (VC part) and write your own models (M part). Imagine using mongoid ORM instead of active record. For that reason rails provides activemodel to make it easy for people to write their own models that are backed by different storage mechanism. look at https://github.com/rails/rails/tree/master/activemodel to get started on writing your own ORM that will use text files as backend instead of SQL database. Also include http://api.rubyonrails.org/classes/ActiveModel/Lint/Tests.html in your model test files to verify that your models conform activemodel api. After that, you can use rails form helpers in views and routes with your models without problem.
Currently building a Rails 3 app and I've notice there are a lot of words and phrases that I use in many different views multiple times. Rather than hard-coding them in the views each time, I thought it might be a good idea to have the content stored in a CONSTANTS hash. Would it make sense to make a constants.rb file in config/initializers and then have a hash in it called CONSTANTS, then whenever I need to content for a view, I'd just call the CONSTANTS hash with a respective key? Is this a good way of going about it, or should I do something else?
Sounds like Rails locale files might be what you want. You would define your strings in config/locales/en.yml (for English) file. You define your strings in a yaml file, then use them via t (:my_string) in your views. It makes it easier to maintain, or to extend to another language.
Take a look at Rails i18n Guide, it explains things in detail.
I am using Ruby on Rails 3.1.0 and I would like to know if it is possible to handle text translations for data stored in database tables. Is it possible? If so, how?
For example, if I have a database table column named Title and in that I have a record with the Title value set\stored to "car", I would like to show
the text "automobile" for italian people;
the text "auto" for german people;
and so on...
For user generated content it's best to store the language with the content.
You can then have your users create "copies" of this data with a different language reference.
Depending on how much data you need to change per table a different gems might be suitable for your needs. I18n gem is NOT a good choice here, as it's meant for static content. See: https://www.ruby-toolbox.com/categories/i18n
Ruby can help you translate the words from one language to another, but only by providing hashes and structures or code algorithms that allow you to do lookups. By itself it knows nothing of the meaning of words, and cannot translate words.
For your purpose, you should build a table of translations, with the primary language words in a primary field, and their translations in secondary fields, one field for each language. Then you can quickly look up all the words for a form in German, or Italian, and substitute them into the form.
You can setup your rails application for internationalization using Rails Internationalization (i18n) API. You would need to pass and set the locale. The locale files would reside in config/locales. As mentioned by the tin Man, the locale file is a key:value file. Here you can override custom rails defaults.
Locale can be set from params and also from domain name. The Rails guide is actually the best place to start with in my opinion.
http://guides.rubyonrails.org/i18n.html
As already mentioned in the other answers, you'll want to use the rails i18n functionality to get this to work.
There are however alternative ways of storing your translations. This railscast describes how to set up a redis-based translation backend, as well as links to a whole bunch of different backends.
If you just want to store your translations in a database using ActiveRecord, I'd suggest you have a look at i18n-active_record.
With these plugins you can easily store your translations in the database, and have your application look them up for you using I18n.t('some_translation_key').
So I know STI is the most reviled thing ever but I have an instance where I think it might actually make sense. My app is parsing a bunch of different types of xml files. Every file model stores the exact same information. Just some info about what user it is associated with, when it was uploaded, and where it is stored on S3.
After the xml file gets stored then I parse it for information which I use to create various other models. Each type of file is going to create different things. It is possible there could be 100 or more different types of xml files although I don't think I'm going to write parsers for that many. Does STI make sense in this case?
The downside I guess is models are all in one directory so it is going to flood that directory unless hack Rails and stick it in a subdir in models dir.
The other option is I have a kind field and put something in the lib directory that handles all this. Or I'm using resque, maybe every xml file parser should be it's own job. There are drawbacks to that though like it being kind of awkward to force a job in the rails console.
From your explanation, the 'file' model is only storing the results of the file upload process and associated meta data. Without more information about the other kinds of models being generated from the parsed XML data, I don't see why single table inheritance applies to this use case.