Showing posts with label class. Show all posts
Showing posts with label class. Show all posts

Monday, November 22, 2010

Each loop for a class or model instance variables

Sometimes we need to check all of instance variables in a class or model one by one. In this exaple we will check if a varaible is an Array or not:

your_model.instance_variables.each do |i|
 if your_model.instance_variable_get(i).instance_of?(Array) then
  #your code to do anything with your_model.instance_variable_get(i) what is a value
 end
end



If you have an active record model, you can do:

@account = Account.first

Account.column_names.each do |i|
  @account.instance_eval(i)
 # row returns @account.name for example inside the loop, next @account.address and so on
end





That's all!

Tuesday, October 26, 2010

How to load i18N locale files in rails3 with our new gem

That is very simple. We'll create a gem with our own locale files, first, we create the locale files in our gem's lib/locales directory (en.yml and hu.yml for example). There would be our gem's .rb file in lib directory (example.rb). Don't forget to specify these files in gemspec. Now we can create a new method: translate_string in our 'lib/example.rb'.


class Example
require 'rubygems'
require 'active_support'
require 'i18n'


path=File.dirname(__FILE__)
I18n.load_path += Dir[ File.join(path, 'locales', '*.{rb,yml}') ]

def initialize
end

def translate_string(var)
  return I18n.t(var, :scope=>[:example, :vars])
end

end

Then, we can create our locale files 'lib/locales/en.yml' like this:


en:
  example:
    vars:
      title: example

And our lib/locales/hu.yml:


hu:
  example:
    vars:
      title: példa

Take care of spaces instead of tabs. After we build our gem and insert to Gemfile (what is in our Rails3 application) we can call our new class in our controller. Don't forget to restart your application before.



e=Example.new
e.translate_string("title")


This gives "example". That's all!

Monday, October 25, 2010

Create new class in Rails3 with hash parameters

This guide is about how to create new class with hash parameters, without ActiveRecord. If you don't want to use database to define a new object, this guide is for you. First, we create a new contact.rb file in "app/models" directory


class Contact
 include ActiveModel::Serialization
 attr_accessor :name, :company_name, :email, :phone, :comment

 def initialize(attributes = {})
  @name=attributes[:name]
  @company_name=attributes[:company_name]
  @email=attributes[:email]
  @phone=attributes[:phone]
  @comment=attributes[:comment]
 end
 
# persisted is important not to get "undefined method `to_key' for" error
 def persisted?
  false
 end 
 
end

And then you can use this model to create a new form, like ActiveRecord in controllers:

@contact=Contact.new

or

@contact=Contact.new(params[:contact])

Now we can use this object to send email to sales or save it to another object's text column with json. For example if there is a json_contact text column in User active_record object, users.rb:


Class User < ActiveRecord::Base
before_save :before_save_function

def before_save_function
  unless self.json_contact.is_a?(String) then
    # we need this condition because if we updating, must know if it is a String (json modelled class) or a Contact model class
    self.json_contact=self.json_contact.to_json
  end
  true
 end

 def contact
  unless self.json_contact.nil? then
   contact_hash = ActiveSupport::JSON.decode(self.json_contact).symbolize_keys.clone
   return Contact.new(contact_hash)
  else
   return nil
  end
 end

end

Then you can give a Contact object to User.json_contact like:

@user=... # your turn (find, or create) @contact=Contact.new(params[:contact]) @user.json_contact=@contact @user.save

Don't forget to restart your application after modifying anything in your models. Now you can get original User.contact as an object, and you can use it like Contact model:

contact_name=@user.contact.name

That's all