How to Create GEMS
Very simple to create a GEM
1. Create the directory structure
.
--- Rakefile
--- bin
--- hello_anderson
--- hello_anderson.gemspec
--- lib
--- hello_anderson.rb
--- test
--- test_hello_anderson.rb
Inside lib it is necessary to have a rb file with the same name of the GEM.
Ex:
class HelloAnderson
def self.say_hello
puts "Hello Anderson!"
end
end
hello_anderson.gemspec is responsible for model the gem. Inside it we have what the gem is and its version.
Ex:
Gem::Specification.new do |s|
s.name = 'hello_anderson'
s.version = '0.0.0'
s.date = '2013-12-12'
s.summary = "Hellow Anderson!"
s.description = "Example of a gem saing hello to Anderson"
s.authors = ["Anderson Araujo Lopes"]
s.email = 'romalopes@yahoo.com.br'
s.files = ["lib/hello_anderson.rb", other files...]
s.homepage =
'http://rubygems.org/gems/hello_anderson'
s.license = 'MIT'
end
To build a GEM
$ gem build hello_anderson.gemspec
To install a GEM
$ gem install ./hello_anderson-0.0.0.gem
To use a GEM
use: require "hello_anderson"
HelloAnderson.say_hello
The published geuemms are in:
http://rubygems.org/
Process:
1. To setup the computer with your RubyGems account:
$ curl -u qrush https://rubygems.org/api/v1/api_key.yaml >
~/.gem/credentials; chmod 0600 ~/.gem/credentials
2 . gem push hello_anderson-0.0.0.gem
To see the GEM in rubygems.org
$ gem list -r hello_anderson
To install the GEM from rubygems.org
$ gem install hola
Adding a executable
place the file bin/hello_anderson
chmod a+x bin/hello_anderson
#!/usr/bin/env ruby
require 'hello_anderson'
puts Hola.say_hello()
To run the GEM
$ ruby -Ilib ./bin/hello_anderson
Test
Fill Rakefile
require 'rake/testtask'
Rake::TestTask.new do |t|
t.libs << 'test'
end
desc "Run tests"
task :default => :test
Fill test/test_hello_anderson.rb
require 'test/unit'
require 'hello_anderson'
class HelloAndersonTest < Test::Unit::TestCase
def test_hello_anderson
assert_equal "Hello Anderson", HelloAnderson.say_hello
end
end
run test
$ rake test
OR
$ rake
Document
Gems use RDoc to generate docs
quarta-feira, 15 de janeiro de 2014
Ruby on Rails-Action Mailer Basics
====
Based on guides.rubyonrails.org
Action Mailer Basics
Send eails using mailer classes and views
Class inherit from ActionMailer::Base and live in app/mailers
Mailers are conceptually similar to controllers
Steps
1. Create the mailer
$ rails generate mailer UserMailer
user_mailer.rb created
class UserMailer < ActionMailer::Base
default from: 'from@example.com' <-- br="" default="" from=""> end
2. It is possible jusst to create the Mailer in app/mailers
class MyMailer < ActionMailer::Base
end
3. Add a method called welcome
def welcome_email(user)
@user = user
@url = 'http://example.com/login'
mail(to: @user.email, subject: 'Welcome to My Awesome Site')
end
4. Create the views/user_mailer/welcome.html.erb
Welcome to example.com, <%= @user.name %>
You have successfully signed up to example.com,
your username is: <%= @user.login %>.
To login to the site, just follow this link: <%= @url %>.
Thanks for joining and have a great day!
4.1 It is possible to create a text(non html) file too. welcome_email.text.erb
With method webcome_email, rails will automatically render both html and text files.
5. In method create of UsersController
if @user.save
# Tell the UserMailer to send a welcome Email after save
UserMailer.welcome_email(@user).deliver
-->
Ruby on Rails-Working with JavaScript in Rails
====
Based on guides.rubyonrails.org
Working with JavaScript in Rails
Unobtrusive JavaScript
Jaav uses a "Unobtrusive javaScript" technique
EX:
< a href="#" data-background-color="#990000">Paint it red< /a>
data-background is called in unobtrusive way because there is no mix between JavaScript and HTML
Call:
paintIt = (element, backgroundColor, textColor) ->
element.style.backgroundColor = backgroundColor
if textColor?
element.style.color = textColor
$ ->
$("a[data-background-color]").click ->
backgroundColor = $(this).data("background-color")
textColor = $(this).data("text-color")
paintIt(this, backgroundColor, textColor)
Built-in Helpers
Many view helper methods to assist generating HTML.
form_for
<%= form_for(@post, remote: true) do |f| %>
...
<% end %>
form_tag
<%= form_tag('/posts', remote: true) %>
link_to
<%= link_to "a post", @post, remote: true %>
<%= link_to "Delete post", @post, remote: true, method: :delete %>
in CoffeScript, uses
$ -> $("a[data-remote]").on "ajax:success", (e, data, status, xhr) -> alert "The post was deleted."
or in directly
<%= link_to "delete", account, method: :delete, data: { confirm: "You sure?" } %>
button_to
<%= button_to "A post", @post, remote: true %>
The server side
Usually, AJAX requests return JSON rather than HTML
Controller
class UsersController < ApplicationController
def index
@users = User.all
@user = User.new
end
def create
@user = User.new(params[:user])
respond_to do |format|
if @user.save
format.html { redirect_to @user, notice: 'User was successfully created.' }
format.js {}
format.json { render json: @user, status: :created, location: @user }
else
format.html { render action: "new" }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
index.html.erb
< b >Users< /b >
< ul id="users"> <----- appendto="" br="" fill="" here="" in="" js="" the="" will=""> <% @users.each do |user| %>
<%= render user %> <----- _user.html.erb="" br="" render="" the=""> <% end %>
< /ul >
< br>
<%= form_for(@user, remote: true) do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.submit %>
<% end %>
_user.html.erb
< li ><%= user.name %>< /li >
create.js.erb
It is called by the method create.
$("<%= escape_javascript(render @user) %>").appendTo("#users");
Turbolinks
Uses AJAX to speed up page rendering in most applications
It attaches a click handler to all < a > tags
What to do
Include turbolinks in Gemfile
in app/assets/javascripts/application.js
//= require turbolinks
To disable turbolink
< a href="..." data-no-turbolink>No turbolinks here< /a>.
----->----->
terça-feira, 14 de janeiro de 2014
Ruby on Rails-Rails Routing from the Outside In
====
Based on guides.rubyonrails.org
Rails Routing from the Outside In
The Purpose of the Rails Router
Simple example:
GET /patients/17
Route
get '/patients/:id', to: 'patients#show'
Or
get '/patients/:id', to: 'patients#show', as: 'patient'
and your application contains this code in the controller:
@patient = Patient.find(17)
and this in the corresponding view:
<%= link_to 'Patient Record', patient_path(@patient) %>
Route will generate
/patients/17
CRUD, Verbs, and Actions
If declare
resources :photos
If creates 7 routes
HTTP Verb Path
GET /photos index
GET /photos/new new
POST /photos create
GET /photos/:id show
GET /photos/:id/edit edit
PATCH/PUT /photos/:id update
DELETE /photos/:id destroy
Singular resources
To call
/profile
route
get 'profile', to: 'users#show'
A singular resourceful route generates these helpers:
new_photo_path returns / photo /new
edit_ photo _path returns / photo /edit
photo _path returns / photo
Controller Namespaces and routing
It is possible to organize groups of controllers under a namespace
EX:
namespace : admin do
resources :posts, :comments
end
It will create routes for post and comments . For Admin::PostController
GET /admin/posts admin_post_path
GET /admin/posts/new new_admin_post_path
POST /admin/posts/:id admin_post_path(:id)
etc
Uses Admin::PostController without the prefix /admin, just declares:
scope module: ‘admin’ do
resources :posts, :comments
end
OR
resources :posts, module:’admin’
To use /admin/post without admin in path.
Scope ‘/admin’ do
Resources :posts, :comments
End
Resources :posts, path: ‘/admin/posts’
GET /admin/posts posts_path
GET /admin/posts/new new_post_path
Nested Resources
when there are resources that are children of others.
Models:
class Magazine < ActiveRecord::Base
has_many
:ads
end
end
class Ad < ActiveRecord::Base
belongs_to :magazine
end
Resources:
resources :magazines do
resources :ads
end
/magazines/:magazine_id/ads
/magazines/:magazine_id/ads/new
/magazines/:magazine_id/ads
The url: edit_magazine_ad_path
It is possible to nest 2 levels
resources :publishers do
resources :magazines do
resources :photos
end
end
Calling:
/publishers/1/magazines/2/photos/3
Paths and URLs From Objects
resources :magazines do
resources :ads
end
You can pass instances of magazines and ads
<%= link_to 'Ad details', magazine_ad_path(@magazine, @ad) %>
Or
<%= link_to 'Ad details', url_for([@magazine, @ad]) %>
Or
<%= link_to 'Ad details', [@magazine, @ad] %>
Rails will se magazines and ads and create the path.
To call Edit
<%= link_to 'Edit Ad', [:edit, @magazine, @ad] %>
Adding More RESTful Actions
Add Member Routes
resources :photos do
member do
get 'preview'
end
end
Provides:
GET /photos/1/preview
and preview_photo_url and preview_photo_path helpers
Assinar:
Postagens (Atom)