Beautify Fixtures (Ruby on Rails)

May 29th, 2006

After generating fixtures from my development database (you can read about it here) I decided to make them nice.

Up to now, for example, Permissions were labelled permission_001 and so on. And that is quite counter-intuitive. A good convention to label permissions would be controller_action. For example:

user_new:
action: new
id: 19
controller: user

The same applies to roles (you can label them with their name) and permissions_roles (you can build the name from their associated role and permission names). This is what my small script does. It is a rake task.

desc "Labels Roles, Permissions and PermissionsRoles after their value so that
the fixture becomes more readable. You can specify the DIR where fixtures we want
to modify are."

require 'yaml'

class Hash
def find_item(key, value, default=nil)
each do |k, v|
return k if (v[key] == value) || (v[key] == value.to_i)
end
default
end
end

task :beautify_fixtures => :environment do
dir = ENV['DIR'] || "#{RAILS_ROOT}/test/fixtures"
db   = ENV['DB']   || 'development'

File.cp "#{dir}/roles.yml",             "#{dir}/roles.bkp.yml"
File.cp "#{dir}/permissions.yml",       "#{dir}/permissions.bkp.yml"
File.cp "#{dir}/permissions_roles.yml", "#{dir}/permissions_roles.bkp.yml"

permissions = YAML.load_file("#{dir}/permissions.bkp.yml")
roles = YAML.load_file("#{dir}/roles.bkp.yml")
File.open("#{dir}/permissions.yml", 'w') do |file|
permissions.each do |k, v|
controller = v['controller']
action =v['action']
file.write "#{controller}_#{action}:n"
v.each {|key, value| file.write "  #{key}: #{value}n"}
end
end

File.open("#{dir}/roles.yml", 'w') do |file|
roles.each do |k, record|
file.write "#{record['name'].downcase}:n"
record.each {|key, value| file.write "  #{key}: #{value}n"}
end
end

permissions = YAML.load_file("#{dir}/permissions.yml")
roles = YAML.load_file("#{dir}/roles.yml")
prs = YAML.load_file("#{dir}/permissions_roles.bkp.yml")

File.open("#{dir}/permissions_roles.yml", 'w') do |file|
prs.each do |k, record|
permission = permissions.find_item('id', record['permission_id'])
role = roles.find_item('id', record['role_id'])
file.write "#{role}_#{permission}n"
record.each {|key, value| file.write "  #{key}: #{value}n"}
end
end

end

You can find a downloadable version here

Programming, Ruby | Comments | Trackback

3 Responses to “Beautify Fixtures (Ruby on Rails)”

  1. 1MikeNo Gravatar
    June 18th, 2006 @ 23:02

    Good job. Although there was a few bugs in the script.

    Mainly for the newlines you just put “n” and not “\n”.

    Below is my updated script.

    desc “Labels Roles, Permissions and PermissionsRoles after their value so that
    the fixture becomes more readable. You can specify the DIR where fixtures we want
    to modify are.”

    require ‘yaml’

    class Hash
    def find_item(key, value, default=nil)
    each do |k, v|
    return k if (v[key] == value) || (v[key] == value.to_i)
    end
    default
    end
    end

    task :beautify_fixtures => :environment do
    dir = ENV['DIR'] || “#{RAILS_ROOT}/test/fixtures”
    db = ENV['DB'] || ‘development’

    File.cp “#{dir}/roles.yml”, “#{dir}/roles.bkp.yml”
    File.cp “#{dir}/permissions.yml”, “#{dir}/permissions.bkp.yml”
    File.cp “#{dir}/permissions_roles.yml”, “#{dir}/permissions_roles.bkp.yml”

    permissions = YAML.load_file(”#{dir}/permissions.bkp.yml”)
    roles = YAML.load_file(”#{dir}/roles.bkp.yml”)
    File.open(”#{dir}/permissions.yml”, ‘w’) do |file|
    permissions.each do |k, v|
    controller = v['controller']
    action =v['action']
    file.write “#{controller}_#{action}:\n”
    v.each {|key, value| file.write ” #{key}: #{value}\n”}
    end
    end

    File.open(”#{dir}/roles.yml”, ‘w’) do |file|
    roles.each do |k, record|
    file.write “#{record['name'].downcase.gsub(/\s/, ‘_’)}:\n”
    record.each {|key, value| file.write ” #{key}: #{value}\n”}
    end
    end

    permissions = YAML.load_file(”#{dir}/permissions.yml”)
    roles = YAML.load_file(”#{dir}/roles.yml”)
    prs = YAML.load_file(”#{dir}/permissions_roles.bkp.yml”)

    File.open(”#{dir}/permissions_roles.yml”, ‘w’) do |file|
    prs.each do |k, record|
    permission = permissions.find_item(’id’, record['permission_id'])
    role = roles.find_item(’id’, record['role_id'])
    role = role.gsub(/\s/, ‘_’)
    file.write “#{role}_#{permission}:\n”
    record.each {|key, value| file.write ” #{key}: #{value}\n”}
    end
    end

    end

  2. 2Enrico FranchiNo Gravatar
    June 20th, 2006 @ 11:21

    Thank you for the fixes. I updated my own script too. Unfortunately Wordpress seems not to be able to manage correctly code… I’ll upload the script and provide a link.

    Hope it was useful!

    (and may I ask you how you got here? )

  3. 3DanNo Gravatar
    August 18th, 2008 @ 17:17

    For all your store fixtures both new and used retail fixtures and shelving come see us at our site: http://www.coasteradvertising.com

Leave a Reply

  1.  
  2.  
  3.  
  4. XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
You can keep track of new comments to this post with the comments feed.

Recent Posts

Blogroll

Siti amici

Misc

Recent Comments

Categories

Enrico Franchi graduated in Maths and Computer Science and is now studying for a Computet Science MSc (though because of italian bureaucracy that very course is to be cancelled).

RiK0's Tech Temple is using WP-Gravatar