📜 ⬆️ ⬇️

Visual sugar for ActiveRecord

Everyone who developed an application on RoR knows that in the console (./script/console) it’s not very convenient to view ActiveRecord objects, they have a mildly unreadable view.

For example, in my last project there is a Schema model.

# == 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>


Accordingly, when I search the objects in the console, I get a “mess” in response:

>> 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">]


')
You can of course do
>> 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">]



or convert to 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>
.....

admit - it doesn’t excite me much ...
But the other day I looked at Railscasts from Ryan , the awesome display of tablets:

image

After spending a couple of hours searching, I found a gem that displays arrays of objects as tables.
It turned out to be Hirb .
it is installed as standard:
sudo gem install hirb

Activated through:
$ ./script/console
Loading development environment (Rails 2.3.5)
irb>> require 'hirb'
=> true
irb>> Hirb.enable
=> nil


Agree, it looks much nicer:

image

but in order to make this mapping by default, add to ~ / .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>


we can disconnect at any time by typing
>> Hirb.disable



for large amounts of data, the output is transmitted to $ PAGER, which you agree is also convenient

Source: https://habr.com/ru/post/78732/


All Articles