CategoryDevelopment

Export database tables, fields comments as markup with Ruby on Rails

When you have to export the comments from the database you can use this short snippet to get the schema as markup.

content = ""
database_name = "DATABASE_NAME"
ActiveRecord::Base.connection.tables.each do |table_name|
  content << "h5. #{table_name}\n"
  rows = ActiveRecord::Base.connection.execute("SELECT table_comment 
    FROM INFORMATION_SCHEMA.TABLES 
    WHERE table_schema='#{database_name}' 
        AND table_name='#{table_name}';");
  puts rows.to_a.inspect
  content << rows.to_a.first.first << "\n"

  rows = ActiveRecord::Base.connection.execute("select table_name, column_name, DATA_TYPE, column_comment from INFORMATION_SCHEMA.COLUMNS where 1 AND TABLE_SCHEMA='#{database_name}' AND TABLE_NAME = '#{table_name}'")
  rows.each(:as => :hash) do |row| 
    puts row.inspect
    j = [ row["column_name"], row["DATA_TYPE"], row["column_comment"] ]

    content << "|#{j.join('|')}|\n"
  end
end; ''
puts content

The output should be something like

Table name

table description

column name, type, description

….. the next table

Tired of the old Rails scaffold?

Why Rails scaffold doesn’t work

The need to write a replacement of the Rails scaffold was because the build in one in Ruby on Rails:

  • Does not support namespaces on the controllers and the models it generates funny paths in the views and/or controllers
  • The native way to extend the Rails scaffold was changed frequently I have to spend hours tracing github issues
  • It uses the ugly scaffold.css and it generates it all the time breaking my styles
  • Does some freaky copy/paste optimizations in the controller to get the model. Scaffold should be a starting point from where with few deletes or add few lines of codes to make a usable screen.

The new approach

With scaffold_pico you can create your own pretty administration in a second.

  • No learning curve – if you know Rails you know it.
  • No 3rd party gems slowing your development/production startup times.
  • Clean code
  • Supports namespaces in the models and the controllers

There is also some sugar. With the pico_scaffold you can generate from the beginning fabricators and take care of N+1 by specifying joins/includes clauses. There is separation of the fields for index and editing. There is a search – almost every screen now days has search.

Supports Zurb Foundation, Materializecss CSS frameworks

Here is the github: https://github.com/gudata/scaffold_pico

Activeadmin

If you plan to use activeadmin you have to be warned that  by using active admin you are supporting two back ends – yours and the one for active admin.
So if you have time to work for someone else go with active admin. else just do it your self.
Don’t use it!
There is one rumor that nothing starting with active* was good product. ActiveDirectory for ActiveX…. I was wondering are there exceptions ActiveSupport, ActiveRecord….

Working with Rails as a designer

Installing Rails

You have to install first ruby, rubygems, git, bundle, rails

Installing ruby

Preferable way is to install it with normal user via rvm. I am not an expert in mac so probably you need to install xcode from your disks/Internet. Then use rvm

Here is a one of the many tutorials http://www.shanison.com/2011/02/20/how-to-install-rvm-on-mac/

here are other ways for installing ruby taken from Ruby on Rails for Designers

  • Windows: One-Click Ruby Installer (I recommend using 1.8.6-26 Final Release)
  • Mac OS X 10.4: Ships with broken Ruby but you can follow the amazing guide by Dan Benjamin
  • Mac OS X 10.5: If you install the Developer Tools from Apple you will be set. Try either your installation discs or Apple’s Developer Site and download Xcode
  • Linux: While this may vary for each distribution, you will need to install ruby, irb, & rdoc

Installing rubygems

Download, extract and run ruby setup.rb

more reading here http://docs.rubygems.org/read/chapter/3

Checkout the project

One SCM is git. install it

http://git-scm.com/book/en/Getting-Started-Installing-Git

Then checkout it

git clone ssh://par@gudasoft.com/uppstart

Starting Rails project

go to the project folder you have checkout already.

cd uppstart

rvm will ask you if you trust the version of ruby specified in the project. Please agree and install it if you don’t have it.

installing is done by

rvm install ruby-what-ever-version

then you have to install bundle. This is done by doing

gem install bundler

After you have install bundler you will have the bundle command available. Bundler will try to install all the project dependencies. do this by typing

bundle

if you don’t have errors you will be almost ready to run the application.

check in config/database.yml and prepare the database specified in the development section

cat config/database.yml

If you see in development sqlite3 as an adapter then just type

rake db:setup

start the application by running

rails s

check the app by navigating to http://localhost:3000

Views

All views are located under app.

Files starting with underscore are partials which are reused in multiple places.

Assets

Assets could be in several places.

check

  • app/assets – for assets which are from the application
  • lib/assets – for assets which are exported
  • vendor/assets – for 3rd party assets

If you want to add your own file include it under the

application.js

or

application_screen.css

Do not add css/javascript directly in those files.

Deploying the application

Put your ssh keys on the server. This should be done once.

Then you have to put your changes on the remote repository. This is done by doing

First we need to get any remote changes by doing.

git pull
git commit -a

Then we can add our new files and commit the changed files/folders.

add the new files with

git add filename_or_folder

then commit all with a message

git commit -a -m "nice message what was changed"
git push

Finally From the application folder run

cap deploy

If you have a lot of readings then Struct is the winner.

If you have equal read/write then Hash

And forget for OpenStruct

Here is why and how I benchmark all those methods



guda: ~/vanilla_ui/vanilla_properties  (v2 *$ u=) ∴ irb
ruby-1.8.7-p330 :001 > require ‘benchmark’
=> true
ruby-1.8.7-p330 :002 > require ‘ostruct’
=> false
ruby-1.8.7-p330 :003 > n = 5000000
=> 5000000
ruby-1.8.7-p330 :004 >
ruby-1.8.7-p330 :005 >   puts “convertions”
convertions
=> nil
ruby-1.8.7-p330 :006 >
ruby-1.8.7-p330 :007 >   Benchmark.bm do |x|
ruby-1.8.7-p330 :008 >      x.report(“Struct init at start”) { CustomerOne = Struct.new(:name, :age);  s = CustomerOne.new(‘a’);   n.times do s.name; end }
ruby-1.8.7-p330 :009?>    x.report(“Struct with write op”) { CustomerTwo = Struct.new(:name, :age);  s = CustomerTwo.new;  s.name = ‘a’; n.times do s.name; end }
ruby-1.8.7-p330 :010?>    x.report(“Hash”) {  s = Hash.new;  s[:name] = ‘a’; n.times do   ; s[:name]; end }
ruby-1.8.7-p330 :011?>    x.report(“OpenStruct”) { s = OpenStruct.new;  s.name = ‘a’; n.times do   ;  s.name; end }
ruby-1.8.7-p330 :012?>   end
user     system      total        real
Struct init at start
1.160000   0.000000   1.160000 (  1.155457)
Struct with write op  1.200000   0.000000   1.200000 (  1.208754)
Hash  1.510000   0.000000   1.510000 (  1.507588)
OpenStruct  5.990000   0.000000   5.990000 (  5.999714)
=> true
Lets do some = operations
ruby-1.8.7-p330 :015 >   Benchmark.bm do |x|
ruby-1.8.7-p330 :016 >      x.report(“Struct init at start”) { CustomerMale = Struct.new(:name, :age);  n.times do s = CustomerMale.new(‘a’);  s.name; end }
ruby-1.8.7-p330 :017?>    x.report(“Hash”) { n.times do   ; s = Hash.new;  s[:name] = ‘a’; s[:name]; end }
ruby-1.8.7-p330 :018?>    x.report(“Struct with write op”) { CustomerFemale = Struct.new(:name, :age);  n.times do s = CustomerFemale.new;  s.name = ‘a’; s.name; end }
ruby-1.8.7-p330 :019?>    x.report(“OpenStruct”) { n.times do   ; s = OpenStruct.new;  s.name = ‘a’; s.name; end }
ruby-1.8.7-p330 :020?>   end
user     system      total        real
Struct init at start  9.930000   0.000000   9.930000 (  9.943670)
Hash 11.090000   0.000000  11.090000 ( 11.127766)
Struct with write op 12.230000   0.000000  12.230000 ( 12.243452)
OpenStruct still working ….

to_i or to_s is faster?

require 'benchmark'
n = 500000

puts "convertions"
Benchmark.bm do |x|
 x.report("to_i") { n.times do   ; "123456789".to_i; end }
 x.report("to_s") { n.times do   ; 1234567890.to_s; end }
 x.report("to_sym") { n.times do   ; 1234567890.to_sym; end }
end

user     system      total        real
to_i  0.310000   0.000000   0.310000 (  0.315844)
to_s  0.490000   0.000000   0.490000 (  0.490607)
to_sym  0.170000   0.000000   0.170000 (  0.171511)



Benchmark.bm(30) do |x|
 x.report("string.to_i == number") { n.times do   ; "123456789".to_i == 1234567890; end }
 x.report("number.to_s == string") { n.times do   ; 1234567890.to_s == '1234567890'; end }
 x.report("string.to_sym == string.to_sym") { n.times do   ; '1234567890'.to_sym == '1234567890'.to_sym; end }
 x.report("num.to_sym == string.to_sym") { n.times do   ; 1234567890.to_sym == '1234567890'.to_sym; end }
 x.report("number.sym == number.sym") { n.times do   ; 1234567890.to_sym == 1234567890.to_sym; end }
end

user     system      total        real
string.to_i == number           0.400000   0.000000   0.400000 (  0.401863)
number.to_s == string           0.740000   0.000000   0.740000 (  0.744603)
string.to_sym == string.to_sym  0.560000   0.000000   0.560000 (  0.574106)
num.to_sym == string.to_sym     0.460000   0.000000   0.460000 (  0.459292)
number.sym == number.sym        0.400000   0.000000   0.400000 (  0.399014)

Rails tip of the day

Generating random string

ActiveSupport::SecureRandom.hex(16)

Wireframing for faster website development

I have tried the approach listed in “Getting Real” by 37signals and I have great results.

The client was very happy because he can imagine his website.

Now I am considering to buy or make such nice tools :)

http://www.uistencils.com/website-stencil-kit.html

http://www.mstonerblog.com/index.php/blog/comments/return_of_the_paper_wireframes/
1. Pencil

Pencil (free) is a Firefox plugin that professes to enable you to build wireframes and prototypes. As a prototyping tool it’s quite good, allowing you to quickly put together a reasonably high fidelity mockup. However, be aware you’ll still need to produce the visual design elements for Pencil, as it relies on dragging and dropping pre-made graphical elements.

The output wireframe elements that ship with Pencil do tend to be based on the look and feel of a Windows desktop application. This really is undesirable for a web application, however you could modify this with your own page elements.

Another downside of Pencil is that its export functionality provides only a few image formats. This means that Pencil falls short of being a real interactive prototype development tool.

Balsamiq Mockups

Balsamiq Mockups (price:$US79, demo available) is an interesting product running on Adobe AIR. Its representation of the interface elements has a refreshing hand-drawn, sketch-like quality to them. This does help promote the degree of changeability of the wireframes, as they look very much like a draft. If you lack the skills to create hand-drawn sketches then Balsamiq is a useful tool, as it allows you to produce quality roughs. Balsamiq also offers a standard collection of interactive screen elements, which is helpful to start off with.

http://gomockingbird.com/ is like balsamia

https://www.jumpchart.com/ – for very simple pages- I cant find how to change the layout.

Denim

A java based.

http://dub.washington.edu:2007/denim/

Eclipse based plugin – http://wireframesketcher.com/

And a nice topic in stackoverflow http://stackoverflow.com/questions/55363/best-tools-for-creating-website-wireframes

iphone

http://iphonemockup.lkmc.ch/

Software development

After my blog post about the junior software developers I have very bright idea of how the world could get benefit from them.

When you catch a junior in mistake – let him contribute to the open source community. Not with coding. With translating. Take some open project and translate it to your native language.

Maybe after all the junior will release that he is much happier to translate than coding :)

win-win condition.

N.B. On the picture – Young coder is coding in Word.

Hash to encode iso-8859-1 (western) to html entities

ENTITIES = {
 'À' => '&#192;',
 'Á' => '&#193;',
 'Â' => '&#194;', Continue reading

© 2024 Gudasoft

Theme by Anders NorénUp ↑