Pull to refresh

Визуальный сахар для ActiveRecord

Reading time 4 min
Views 1.2K
Каждый, кто разрабатывал приложение на RoR знает, что в консоли (./script/console) не слишком удобно просматривать ActiveRecord объекты, они имеют мягко говоря не читабельный вид

Например в моем последнем проекте есть модель Schema

# == Schema Information<br>
#<br>
# Table name: schemas<br>
#<br>
#  id                  :integer         not null, primary key<br>
#  user_id             :integer         not null<br>
#  name                :string(255)     not null<br>
#  public              :boolean         not null<br>
#  description         :string(255)<br>
#  schema_file_name    :string(255)<br>
#  schema_content_type :string(255)<br>
#  schema_file_size    :integer<br>
#  schema_updated_at   :datetime<br>
#  created_at          :datetime<br>
#  updated_at          :datetime<br>
#<br>
<br>


Соответсвенно, когда я в консоли выполняю поиск по объектам, то в ответ получаю «кашу»:

>> Schema.find(:all, :limit => 3)
=> [#<Schema id: 5, user_id: 14, name: "1", public: true, description: "1", schema_file_name: "1.kml", schema_content_type: "text/xml", schema_file_size: 177411, schema_updated_at: "2009-08-19 23:46:19", created_at: "2009-08-19 23:46:19", updated_at: "2009-08-19 23:46:19">, #<Schema id: 6, user_id: 14, name: "2", public: true, description: "2", schema_file_name: "2.kml", schema_content_type: "text/xml", schema_file_size: 3084, schema_updated_at: "2009-08-19 23:46:56", created_at: "2009-08-19 23:46:56", updated_at: "2009-08-19 23:46:56">, #<Schema id: 8, user_id: 14, name: "а", public: true, description: "", schema_file_name: "_.kml", schema_content_type: "text/xml", schema_file_size: 1461, schema_updated_at: "2009-08-19 23:47:53", created_at: "2009-08-19 23:47:53", updated_at: "2009-08-19 23:47:53">]



Можно конечно сделать
>> pp Schema.find(:all, :limit => 3)
[#<Schema id: 5, user_id: 14, name: "1", public: true, description: "1", schema_file_name: "1.kml", schema_content_type: "text/xml", schema_file_size: 177411, schema_updated_at: "2009-08-19 23:46:19", created_at: "2009-08-19 23:46:19", updated_at: "2009-08-19 23:46:19">,
#<Schema id: 6, user_id: 14, name: "2", public: true, description: "2", schema_file_name: "2.kml", schema_content_type: "text/xml", schema_file_size: 3084, schema_updated_at: "2009-08-19 23:46:56", created_at: "2009-08-19 23:46:56", updated_at: "2009-08-19 23:46:56">,
#<Schema id: 8, user_id: 14, name: "а", public: true, description: "", schema_file_name: "_.kml", schema_content_type: "text/xml", schema_file_size: 1461, schema_updated_at: "2009-08-19 23:47:53", created_at: "2009-08-19 23:47:53", updated_at: "2009-08-19 23:47:53">]



или же преобразовать в YAML:
>> puts Schema.find(:all, :limit => 3).to_yaml<br>
--- <br>
- !ruby/object:Schema <br>
  attributes: <br>
    name: "1"<br>
    updated_at: 2009-08-19 23:46:19.606916<br>
    public: t<br>
    schema_updated_at: 2009-08-19 23:46:19.603984<br>
    id: "5"<br>
    description: "1"<br>
    schema_content_type: text/xml<br>
    user_id: "14"<br>
    schema_file_name: 1.kml<br>
    created_at: 2009-08-19 23:46:19.606916<br>
    schema_file_size: "177411"<br>
  attributes_cache: {}<br>
.....

признаться — меня и это не особо возбуждает…
Но на днях я подсмотрел у Ryan'а на Railscasts, обалденное отображение табличек:

image

потратив пару часов на поиск, я нашел gem который отображает массивы объектов как таблицы.
Им оказался Hirb.
устанавливается он стандартно:
sudo gem install hirb

Активируется через:
$ ./script/console
Loading development environment (Rails 2.3.5)
irb>> require 'hirb'
=> true
irb>> Hirb.enable
=> nil


Согласитесь, выглядит гораздо приятней:

image

а для того, чтобы сделать это отображение по умолчанию, добавляем в ~/.irbrc
if ENV['RAILS_ENV']<br>
  require 'rubygems'<br>
  begin<br>
    require 'hirb'<br>
    Hirb.enable<br>
  rescue LoadError<br>
    puts "Error loading Hirb. Run 'sudo gem install hirb'"<br>
  end<br>
end<br>


отключить можем в любой момент, набрав
>> Hirb.disable



при больших объемах данных вывод передается в $PAGER, что согласитесь тоже удобно
Tags:
Hubs:
+24
Comments 33
Comments Comments 33

Articles