Generating Entity-Relationship Diagram for Ruby on Rails

Installation
Install graphviz (if not yet)
window http://www.graphviz.org/Download_windows.php
linux sudo apt-get install graphviz
osx brew install graphviz

Add the gem

In the project gemfile add

group :development do
  gem 'rails-erd'
end


Then execute cd PATH_OF_THE_PROJECT && bundle install

Using it
It's as simple as a bundle exec rake erd and it will generate a PDF file in the root of the app.

Ruby Percent Syntax (Percent Functions)

I wanted to post a quick guide to the special ruby syntax for literals that utilize the % (percent) symbol. Most beginners guides to ruby leave out an explanation of these forms of literals, but many ruby coders use them. When someone encounters them for the first time it is almost impossible to figure out what they mean. (Try searching Google for “%w”)

Ruby has special syntax for making strings, arrays and system commands easier to write. They allow you to use different characters as delimiters so you can minimize escaping in your literals.

The syntax

The syntax for the % literals is a percent symbol (%) a letter which defines what kind of literal it is (Q, q, w, x, r) a delimiter,  the content, and the closing delimiter.

The delimiter can be any character, and is defined as whatever is immediately after the letter in the syntax. For example %Q!content! , the delimiter is the ! and it surrounds the content. There are special cases when the delimiter is { or (, the closing delimiter will be } or ) respectively.

%Q and %q (Percent Q): Strings

%Q!Some String of “Characters”! <==> ” Some String of \”Characters\” “

%Q is the equivalent to a double-quoted ruby string. #{expression} evaluation works just like in double-quoted strings, even if you use %Q{} as your delimiter!

You can also leave off the Q and it will have the same functionality. I recommend leaving the Q in to be more clear.

%q!Some String of “Characters”! <==> ‘Some String of Characters’

The %q is just like %Q, but acts the same as a single-quoted string. Whatever is inside the delimiters is returned as a string.

You can remember %Q is for strings because it acts like Quotes.

More info here: http://docs.huihoo.com/ruby/ruby-man-1.4/syntax.html#string

%W (Percent W): Arrays

%W(North South East West) <==> ["North", "South", "East", "West"]

%W (and %w) allow you to create an Array of strings without using quotes and commas.

The delimiter rules are the same as strings, but typically parentheses are used. The content inside the delimiters are split by white-space, and put into an array. This is great if you have a hard coded list of single word strings.

When using %W (capital W), it is evaluated as a double-quoted string. This allows you to use #{} to interpolate values. %w (lower-case w) will evaluate as a single quoted string.

You can remember %W is by thinking of it as a White-space divided Array.

More info here : http://docs.huihoo.com/ruby/ruby-man-1.4/syntax.html#array

%x (Percent x): System Execution

%x{ ls /usr/local } <==> `ls /usr/local`

%x allows you to call system commands, equivilent to wrapping the command in `s (grave accents). The benefit of the $x{} syntax is you don’t have to escape your accents in commands that use them.

You can remember to use X because it eXecutes a command.

More info here: http://docs.huihoo.com/ruby/ruby-man-1.4/syntax.html#command

%r (Percent r): Regular Expressions

%r{/usr/bin/} <==> /\/usr\/bin\//

%r is really handy for regular expressions that contain /s (forward slashes) which are the default delimiter for regular expressions and have to be escaped.

Remember to use %r with regular expressions.

More info here: http://docs.huihoo.com/ruby/ruby-man-1.4/syntax.html#regexp

I hope this information is helpful. Please leave a comment if this helped or if I left something out.

Tags: Percent Functions, Percent Literals, Ruby, Syntax

credit : http://jimhoskins.com/2008/10/07/ruby-percent-syntax-percent-functions/

paperclips fix : has an extension that does not match its contents

ระหว่างทำ gallery เจอ error message ว่า
has an extension that does not match its contents
แต่... บน ubuntu ไม่เป็น งงเบย เป็นเฉพาะ windows
สร้างไฟล์ config/initializers/paperclips.rb
require 'paperclip/media_type_spoof_detector'
module Paperclip
  class MediaTypeSpoofDetector
    def spoofed?
      false
    end
  end
end

ยังไม่จบ แก้ไฟล์ config/enviroments/development.rb (สำหรับ run แบบ development)
...
Paperclip.options[:command_path] = "C:/Program Files/ImageMagick-6.8.9-Q16/"
...

Fix Paperclip: “has an extension that does not match its content” error on windows

เพิ่มบรรทัดด้านล่าง config/environments/development.rb

Paperclip.options[:command_path] = "C:/Program Files/ImageMagick-6.8.9-Q16/"


เพิ่มไฟล์ config/initializers/paperclip.rb
require 'paperclip/media_type_spoof_detector'
module Paperclip
  class MediaTypeSpoofDetector
    def spoofed?
      false
    end
  end
end

prettyprint กับ bootstrap-wysiwyg DIY

ไม่มีอะไรมาก ใช้มุก replace

            <script>
                jQuery(function($) {
                    $('#article_body_editor').on('keyup keypress blur change', function() {
                        article_body_editor = $('#article_body_editor').html()
                            .replace(/\[prettyprint\]/g, '<pre class="prettyprint">')
                            .replace(/\[prettyprint linenums\]/g, '<pre class="prettyprint linenums">')
                            .replace(/\[\/prettyprint\]/g, '</pre>')
                            .replace(/\[code\]/g, '<code>')
                            .replace(/\[\/code\]/g, '</code>');
                        $('#article_body').val(article_body_editor);
                    });
                });
            </script>

Using MySQL with Rails 3 on Windows

gem mysql2 ไม่ support mysql 64 bits ???

ลง mysql connector 32 bits

    http://dev.mysql.com/downloads/connector/c/


แล้วก็ ...


    gem install mysql2 -- '--with-mysql-lib="C:\Program Files (x86)\MySQL\MySQL Connector C 6.1\lib" --with-mysql-include="C:\Program Files (x86)\MySQL\MySQL Connector C 6.1\include"


แล้วก็ ...

copy libmysql.dll จาก C:\Program Files (x86)\MySQL\MySQL Connector C 6.1\lib ไปไว้ที่ C:\Ruby193\bin นะจ๊ะ

Using MySQL with Rails 3 on Windows

    Install railsinstaller -> www.railsinstaller.org (I installed it to c:\Rails)

    Install MySQL (I used MySQL 5.5) -> dev.mysql.com/downloads/installer/

    --- for mySQL installation ---

    If you dont already have these two files installed you might need them to get your MySQL going

    vcredist_x86.exe -> http://www.microsoft.com/download/en/details.aspx?id=5555 dotNetFx40_Full_x86_x64.exe -> http://www.microsoft.com/download/en/details.aspx?id=17718

    Use default install Developer Machine

    -MySQL Server Config-
    port: 3306
    windows service name: MySQL55
    mysql root pass: root (you can change this later)
    (username: root)
    -MySQL Server Config-

    --- for mySQL installation ---

    --- Install the mysql2 Gem ---

    Important: Do this with Git Bash Command Line(this was installed with railsinstaller) -> start/Git Bash

    gem install mysql2 -- '--with-mysql-lib="c:\Program Files\MySQL\MySQL Server 5.5\lib" --with-mysql-include="c:\Program Files\MySQL\MySQL Server 5.5\include"'

    Now the gem should have installed correctly

    Lastly copy the libmysql.dll file from
    C:\Program Files\MySQL\MySQL Server 5.5\lib
    to
    C:\Rails\Ruby1.9.2\bin

    --- Install the mysql2 Gem ---




http://stackoverflow.com/questions/3608287/error-installing-mysql2-failed-to-build-gem-native-extension

RAILS MySQL ssh Tunnel

มีโทรศัพท์แจ้งมาว่า "แบท พี่เปลี่ยนวิธี connect mysql ให้ต้อง ssh tunnel นะ"
ตึ่งโป๊ะ!!! ทำไงๆๆๆ คืออะไร แค่เคยเห็นผ่านๆ แล้ว Ruby on Rails ทำไง

อันดับแรก
ssh -N -L 8888:127.0.0.1:3306 -f remote-user@remote-domain-or-ip

ใน database.yml ก็

 production:
  adapter: mysql2
  encoding: utf8
  host: 127.0.0.1
  port: 8888
  database: databasename
  username: remote-user
  password: password-remote-user


เสร็จ connect สำเร็จ ข้อมูลมา แต่ทำไมออกมาไม่เหมือนกัน
อ้าว!!! เปลี่ยนชื่อ database อีกก็ไม่บอก

Net::SMTPFatalError: 553 5.1.2 The address specified is not a valid RFC-5321 address.

โอ้ววว เมลไม่ออก ตั้งแต่วันอังคาร

Net::SMTPFatalError: 553 5.1.2 The address specified is not a valid RFC-5321 address. - gsmtp

จากเดิมใน xxx_mailer.rb
mail(from: "ibatt.in.th@gmail.com", to: "to@email.com", subject: 'สวัสดีมนุษย์')

แก้เป็น
mail(from: "I'm BATT <ibatt.in.th@gmail.com>", to: "to@email.com", subject: 'สวัสดีมนุษย์')

Prawn Font family

อยากใช้ font ไทย ใน prawn แล้วอยากใช้ inline format ด้วย

prawn คืออะไร plugin ruby เอาไว้เขียน pdf
inline format คืออะไร ก็แบบ ใส่ <b></b> แล้วได้ตัวหน้า ใส่ <i></i> ได้ตัวเอียง

ก็เพิ่ม font family สิครับ


    pdf = Prawn::Document.new :page_size => 'A4', :margin => [margin_top, margin_right, margin_bottom, margin_left]
    
    pdf.font_families.update("THSarabun" => {
      :normal => File.join("public", "fonts", "THSarabun.ttf"),
      :italic => File.join("public", "fonts", "THSarabunItalic.ttf"),
      :bold => File.join("public", "fonts", "THSarabunBold.ttf"),
      :bold_italic => File.join("public", "fonts", "THSarabunBoldItalic.ttf")
    })
   
    pdf.font "THSarabun", size: 14   
   
    pdf.text "แบบฟอร์ม", align: :center
    pdf.text "<b>1. ชื่อโครงการ</b> #{@project.name}", inline_format: true
    pdf.text "#{@project.code}"
 

Multiple node js same machine

Installation of NVM

Windows

Download the latest version of NVM

https://github.com/coreybutler/nvm-windows/releases/latest

Download nvm-setup.zip and install on the windows

Verify Installation

nvm version

MacOs/Linux
Using curl
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash

Using Wget
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash

Verify Installation
command -v nvm

Usage
Get a List of all available NodeJs versions
nvm  ls available //windows

nvm ls-remote //MacOs/Linux

Install latest NodeJs version

nvm install node

Install latest LTS Release

nvm install --lts

Install particular NodeJs version

Multiple NodeJs version can be installed using the below command.

nvm install 8.11.1 // to install the 8.11.1 version

nvm install 12.13.1 //to install the 12.13.1 version

UnInstall the multiple NodeJs version

nvm uninstall 8.11.1

Switching the NodeJs version

Get a List of installed NodeJs version

nvm list  //for windows

nvm ls //for MacOs/linux


nvm list

Switching Between installed NodeJs Versions

nvm use 8.11.1  //To enable 8.11.1

nvm use 12.13.1 //To enable 12.13.1


Use Custom alias for installed NodeJs versions

We can create an alias as per the Project basis or the group of projects using the same NodeJs version.

nvm alias awesome-project 12.13.1

After creating an alias, then switch to the alias

nvm use awesome-project

Remove the Alias

nvm unalias awesome-project

Run to specific NodeJs version, without switching

nvm run 8.11.1 app.js

or can be used below command

nvm exec 8.11.1 node app.js

Other Commands

To use installed nodeJs version in the system

nvm use system

To completely remove nvm from the system.

nvm unload