Posts Tagged ‘class’

Ruby usando DRb (Distributed Ruby)

Postado em 18 jun 2009
Categoria(s) Ruby

DRb permite programas em Ruby se comunicar uns com os outros na mesma máquina ou através de uma rede, aplicação cliente/servidor. DRb utiliza o método de invocação remota (RMI) para transmitir comandos e dados entre os processos.

Nesse post vamos criar dois programas, o servidor e o cliente usando DRb.

Construíndo o servidor (server.rb):

1
2
3
4
5
6
7
8
9
10
11
require 'drb'
 
class Server
  def hello
    "Hello, This is a Distributed Ruby (DRb) Example"
  end
end
 
server_object = Server.new
DRb.start_service('druby://localhost:9000', server_object)
DRb.thread.join

Entendendo o código do servidor:

require 'drb'

A linha acima apenas requere a biblioteca DRb, essa biblioteca que a responsável por fazer a comunicação remota. Você pode encontrar mais informações a seu respeito em: http://www.ruby-doc.org/stdlib/libdoc/drb/rdoc/index.html.

class Server
  def hello
    "Hello, This is a Distributed Ruby (DRb) Example"
  end
end

O código acima apena cria uma classe e o método hello, que imprime uma mensagem.

server_object = Server.new
DRb.start_service('druby://localhost:9000', server_object)
DRb.thread.join

Essas três linhas acima que fazem o servidor funcionar, a primeira linha apenas faz a instância da classe Server, a segunda linha inicia o serviço remoto no endereço druby://localhost:9000 disponibilizando o objeto server_object, a terceira linha inicia uma thread no servidor para aceitar requisições remotas.

Construíndo o cliente (client.rb):

1
2
3
4
5
6
require 'drb'
 
DRb.start_service
obj = DRbObject.new(nil, 'druby://localhost:9000')
 
puts obj.hello

Entendendo o código do cliente:

require 'drb'

A linha acima apenas requere a biblioteca DRb, essa biblioteca que a responsável por fazer a comunicação remota. Você pode encontrar mais informações a seu respeito em: http://www.ruby-doc.org/stdlib/libdoc/drb/rdoc/index.html.

DRb.start_service
obj = DRbObject.new(nil, 'druby://localhost:9000')

As suas linhas acima inicia o serviço remoto com o servidor, a primeira inicializa o serviço e a segunda estabelece conexão com o servidor no endereço druby://localhost:9000 obtendo o objeto Server.

puts obj.hello

Essa linha acima invoca o método hello do objeto remoto Server.

Rodando o servidor:

Para inicializar o servidor digite na linha de comando:

ruby server.rb

Não será exibida nenhuma mensagem, o servidor apenas ficará esperando novas requisições remotas.

Rodando o cliente:

ruby client.rb

Quando você rodar o cliente terá como retorno a mensagem:

Hello, This is a Distributed Ruby (DRb) Example

Pronto! Esse recurso é muito interessante. ;-)

Se você gostou desse texto e acha que ajudou você, me recomende: Recommend Me.

  • Share/Bookmark

Ruby on Rails usando strip_tags nos controllers, models e libs

Postado em 15 jun 2009
Categoria(s) Ruby on Rails

O Ruby on Rails possui o métogo strip_tags para remover tags html. Esse método está apenas disponível na camada de view, uma vez que faz parte ActionView::Helpers::SanitizeHelper.

Eu não concordo com essa implementação do Rails, acho que deveria ser disponível também na camada de controller e model, onde são os lugares que esse método é mais útil.

Para implementar o strip_tags nos controllers, models e libs, nós podemos adicionar esse método na classe String, desta forma estará disponível em qualquer lugar.

Abra o arquivo config/initializers/new_rails_defaults.rb:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# Be sure to restart your server when you modify this file.
 
# These settings change the behavior of Rails 2 apps and will be defaults
# for Rails 3. You can remove this initializer when Rails 3 is released.
 
if defined?(ActiveRecord)
  # Include Active Record class name as root for JSON serialized output.
  ActiveRecord::Base.include_root_in_json = true
 
  # Store the full class name (including module namespace) in STI type column.
  ActiveRecord::Base.store_full_sti_class = true
end
 
# Use ISO 8601 format for JSON serialized times and dates.
ActiveSupport.use_standard_json_time_format = true
 
# Don't escape HTML entities in JSON, leave that for the #json_escape helper.
# if you're including raw json in an HTML page.
ActiveSupport.escape_html_entities_in_json = false

Adicione no final do arquivo as linhas:

1
2
3
4
5
class String
  def strip_tags
    ActionController::Base.helpers.strip_tags(self)
  end
end

As linhas acima criam o método strip_tags na classe String.

Ficando o arquivo completo assim:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# Be sure to restart your server when you modify this file.
 
# These settings change the behavior of Rails 2 apps and will be defaults
# for Rails 3. You can remove this initializer when Rails 3 is released.
 
if defined?(ActiveRecord)
  # Include Active Record class name as root for JSON serialized output.
  ActiveRecord::Base.include_root_in_json = true
 
  # Store the full class name (including module namespace) in STI type column.
  ActiveRecord::Base.store_full_sti_class = true
end
 
# Use ISO 8601 format for JSON serialized times and dates.
ActiveSupport.use_standard_json_time_format = true
 
# Don't escape HTML entities in JSON, leave that for the #json_escape helper.
# if you're including raw json in an HTML page.
ActiveSupport.escape_html_entities_in_json = false
 
class String
  def strip_tags
    ActionController::Base.helpers.strip_tags(self)
  end
end

Agora reinicie o seu servidor web para pegar essas novas configurações na inicialização da aplicação.

Agora quando você precisar do strip_tags pode usar assim, exemplos:

params['title'] = params['title'].strip_tags
 
ou
 
>> '<b>meu texto</b>'.strip_tags
=> "meu texto"

Se você gostou desse texto e acha que ajudou você, me recomende: Recommend Me.

  • Share/Bookmark

Ruby on Rails como fazer dois atributos da mesma classe apontarem para a mesma classe pai no ActiveRecord

Postado em 23 mar 2009
Categoria(s) Ruby on Rails

Hoje estava programando um sistema de menus, onde cada item de menu pode ter vários outros itens, não existindo limite de subníveis.

Para conseguir obter essa relação e associação no banco de dados criei dois models BackendMenu e BackendMenuNode, com as seguintes migrações:

BackendMenu:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class CreateBackendMenus < ActiveRecord::Migration
  def self.up
    create_table :backend_menus do |t|
      t.string  :title,     :null => false
      t.string  :path,      :null => false
      t.integer :weight,    :null => false, :default => 0
      t.boolean :is_active, :null => false, :default => false
      t.boolean :is_root,   :null => false, :default => false
      t.timestamps
    end
  end
 
  def self.down
    drop_table :backend_menus
  end
end

BackendMenuNode:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class CreateBackendMenuNodes < ActiveRecord::Migration
  def self.up
    create_table :backend_menu_nodes do |t|
      t.integer :root_id, :null => false
      t.integer :node_id, :null => false
      t.integer :weight,  :null => false, :default => 0
    end
 
    add_index(:backend_menu_nodes, [:root_id, :node_id], :unique => true)
    execute "ALTER TABLE backend_menu_nodes ADD CONSTRAINT fk_backend_menu_nodes_backend_menus_root FOREIGN KEY (root_id) REFERENCES backend_menus (id) ON DELETE CASCADE ON UPDATE CASCADE"
    execute "ALTER TABLE backend_menu_nodes ADD CONSTRAINT fk_backend_menu_nodes_backend_menus_node FOREIGN KEY (node_id) REFERENCES backend_menus (id) ON DELETE CASCADE ON UPDATE CASCADE"
  end
 
  def self.down
    drop_table :backend_menu_nodes
  end
end

Todos os itens de menu vão ficar cadastrados na tabela backend_menus e associação para descobrir os menus filhos será feito pela tabela backend_menu_nodes. A tabela backend_menu_nodes tem as colunas root_id que se referência ao pai e a coluna node_id que se referência aos menus filhos, desta forma um menu pode ter N itens.

Tanto a coluna root_id e node_id fazem referência a tabela backend_menus.

Para conseguir obter essa associação no ActiveRecord fiz o seguinte:

BackendMenu:

1
2
3
4
5
class BackendMenu < ActiveRecord::Base
  # associações
  has_many :nodes, :class_name => "BackendMenuNode", :foreign_key => "root_id"
  ...
end

BackendMenuNode:

1
2
3
4
5
6
class BackendMenuNode < ActiveRecord::Base
  # associações
  belongs_to :root, :class_name => "BackendMenu", :foreign_key => "root_id"
  belongs_to :node, :class_name => "BackendMenu", :foreign_key => "node_id"
  ...
end

Fazendo a relação acima eu consigo navegar pelo menu pai e os meus filhos.

Para imprimir o menu eu criei 3 helpers usando a recursividade:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
module BackendHelper
 
  # cria o menu do backend
  def menu(collection, class_name = "menu")
    content = content_tag :ul, :class => class_name do
      menu_items(collection)
    end
    content
  end
 
  # cria os items do menu
  def menu_items(collection)
    content = ""
    for item in collection
      if item.instance_of? BackendMenu
        content << item_nodes(item)
      else
        content << item_nodes(item.node)
      end
    end
    content
  end
 
  # cria os nós do item de menu
  def item_nodes(item)
    content = ""
    class_name = "leaf"
 
    if item.nodes.size > 0
      class_name = "collapsed"
      content << content_tag(:li, :class => class_name) do
        link_to(item.title, item.path) + menu(item.nodes, "menu hide")
      end
    else
      content << content_tag(:li, link_to(item.title, item.path), :class => class_name)
    end
    content
  end
 
end

No controlador eu faço o seguinte:

1
@backend_menus = BackendMenu.find :all, :conditions => { :is_root => true }, :order => 'weight'

Na view:

1
2
3
4
5
<!-- begin sidebar-left -->
<div id="sidebar-left">
  <%= menu(@backend_menus) %>
</div>
<!-- end sidebar-left -->

Se você quiser alguns menus de teste, usei a migração abaixo:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
class AddBackendMenus < ActiveRecord::Migration
  def self.up
    BackendMenu.delete_all
 
    # criação dos menus pais
    BackendMenu.create(:title => 'Principal',
      :path => 'principal',
      :weight => 1,
      :is_active => true,
      :is_root => true)
 
    BackendMenu.create(:title => 'Criar Conteúdo',
      :path => 'criar-conteudo',
      :weight => 2,
      :is_active => true,
      :is_root => true)
 
    BackendMenu.create(:title => 'Gerenciamento de Conteúdo',
      :path => 'gerenciamento-de-conteudo',
      :weight => 3,
      :is_active => true,
      :is_root => true)
 
    BackendMenu.create(:title => 'Site Construtor',
      :path => 'site-construtor',
      :weight => 4,
      :is_active => true,
      :is_root => true)
 
    BackendMenu.create(:title => 'Site Configuração',
      :path => 'site-configuracao',
      :weight => 5,
      :is_active => true,
      :is_root => true)
 
    BackendMenu.create(:title => 'Gerenciamento de Usuário',
      :path => 'gerenciamento-de-usuario',
      :weight => 6,
      :is_active => true,
      :is_root => true)
 
    BackendMenu.create(:title => 'Relatórios',
      :path => 'relatorios',
      :weight => 7,
      :is_active => true,
      :is_root => true)
 
    BackendMenu.create(:title => 'Ajuda',
      :path => 'ajuda',
      :weight => 8,
      :is_active => true,
      :is_root => true)
 
    # criação dos menus filhos
    BackendMenu.create(:title => 'Página',
      :path => 'criar-conteudo/pagina',
      :is_active => true,
      :is_root => false)
    BackendMenuNode.create(:root_id => BackendMenu.find_by_path('criar-conteudo').id,
      :node_id => BackendMenu.find_by_path('criar-conteudo/pagina').id,
      :weight => 1)
 
    BackendMenu.create(:title => 'História',
      :path => 'criar-conteudo/historia',
      :is_active => true,
      :is_root => false)
    BackendMenuNode.create(:root_id => BackendMenu.find_by_path('criar-conteudo').id,
      :node_id => BackendMenu.find_by_path('criar-conteudo/historia').id,
      :weight => 2)
 
    BackendMenu.create(:title => 'Comentários',
      :path => 'gerenciamento-de-conteudo/comentarios',
      :is_active => true,
      :is_root => false)
    BackendMenuNode.create(:root_id => BackendMenu.find_by_path('gerenciamento-de-conteudo').id,
      :node_id => BackendMenu.find_by_path('gerenciamento-de-conteudo/comentarios').id,
      :weight => 1)
 
    BackendMenu.create(:title => 'Ativos',
      :path => 'gerenciamento-de-conteudo/comentarios/ativos',
      :is_active => true,
      :is_root => false)
    BackendMenuNode.create(:root_id => BackendMenu.find_by_path('gerenciamento-de-conteudo/comentarios').id,
      :node_id => BackendMenu.find_by_path('gerenciamento-de-conteudo/comentarios/ativos').id,
      :weight => 1)
 
    BackendMenu.create(:title => 'Teste',
      :path => 'gerenciamento-de-conteudo/comentarios/ativos/teste',
      :is_active => true,
      :is_root => false)
    BackendMenuNode.create(:root_id => BackendMenu.find_by_path('gerenciamento-de-conteudo/comentarios/ativos').id,
      :node_id => BackendMenu.find_by_path('gerenciamento-de-conteudo/comentarios/ativos/teste').id,
      :weight => 1)
 
    BackendMenu.create(:title => 'Inativos',
      :path => 'gerenciamento-de-conteudo/comentarios/inativos',
      :is_active => true,
      :is_root => false)
    BackendMenuNode.create(:root_id => BackendMenu.find_by_path('gerenciamento-de-conteudo/comentarios').id,
      :node_id => BackendMenu.find_by_path('gerenciamento-de-conteudo/comentarios/inativos').id,
      :weight => 2)
 
    BackendMenu.create(:title => 'Conteúdo',
      :path => 'gerenciamento-de-conteudo/conteudo',
      :is_active => true,
      :is_root => false)
    BackendMenuNode.create(:root_id => BackendMenu.find_by_path('gerenciamento-de-conteudo').id,
      :node_id => BackendMenu.find_by_path('gerenciamento-de-conteudo/conteudo').id,
      :weight => 2)
 
    BackendMenu.create(:title => 'Tipos de Conteúdo',
      :path => 'gerenciamento-de-conteudo/tipos-de-conteudo',
      :is_active => true,
      :is_root => false)
    BackendMenuNode.create(:root_id => BackendMenu.find_by_path('gerenciamento-de-conteudo').id,
      :node_id => BackendMenu.find_by_path('gerenciamento-de-conteudo/tipos-de-conteudo').id,
      :weight => 3)
 
    BackendMenu.create(:title => 'Publicação de RSS',
      :path => 'gerenciamento-de-conteudo/publicacao-de-rss',
      :is_active => true,
      :is_root => false)
    BackendMenuNode.create(:root_id => BackendMenu.find_by_path('gerenciamento-de-conteudo').id,
      :node_id => BackendMenu.find_by_path('gerenciamento-de-conteudo/publicacao-de-rss').id,
      :weight => 4)
 
    BackendMenu.create(:title => 'Blocos',
      :path => 'site-construtor/blocos',
      :is_active => true,
      :is_root => false)
    BackendMenuNode.create(:root_id => BackendMenu.find_by_path('site-construtor').id,
      :node_id => BackendMenu.find_by_path('site-construtor/blocos').id,
      :weight => 1)
 
    BackendMenu.create(:title => 'Formulários',
      :path => 'site-construtor/formularios',
      :is_active => true,
      :is_root => false)
    BackendMenuNode.create(:root_id => BackendMenu.find_by_path('site-construtor').id,
      :node_id => BackendMenu.find_by_path('site-construtor/formularios').id,
      :weight => 2)
 
    BackendMenu.create(:title => 'Menus',
      :path => 'site-construtor/menus',
      :is_active => true,
      :is_root => false)
    BackendMenuNode.create(:root_id => BackendMenu.find_by_path('site-construtor').id,
      :node_id => BackendMenu.find_by_path('site-construtor/menus').id,
      :weight => 3)
 
    BackendMenu.create(:title => 'Módulos',
      :path => 'site-construtor/modulos',
      :is_active => true,
      :is_root => false)
    BackendMenuNode.create(:root_id => BackendMenu.find_by_path('site-construtor').id,
      :node_id => BackendMenu.find_by_path('site-construtor/modulos').id,
      :weight => 4)
 
    BackendMenu.create(:title => 'temas',
      :path => 'site-construtor/temas',
      :is_active => true,
      :is_root => false)
    BackendMenuNode.create(:root_id => BackendMenu.find_by_path('site-construtor').id,
      :node_id => BackendMenu.find_by_path('site-construtor/temas').id,
      :weight => 5)
 
    BackendMenu.create(:title => 'Tradução',
      :path => 'site-construtor/traducao',
      :is_active => true,
      :is_root => false)
    BackendMenuNode.create(:root_id => BackendMenu.find_by_path('site-construtor').id,
      :node_id => BackendMenu.find_by_path('site-construtor/traducao').id,
      :weight => 6)
 
    BackendMenu.create(:title => 'Idiomas',
      :path => 'site-configuracao/idiomas',
      :is_active => true,
      :is_root => false)
    BackendMenuNode.create(:root_id => BackendMenu.find_by_path('site-configuracao').id,
      :node_id => BackendMenu.find_by_path('site-configuracao/idiomas').id,
      :weight => 1)
 
    BackendMenu.create(:title => 'Logs e Alertas',
      :path => 'site-configuracao/logs-e-alertas',
      :is_active => true,
      :is_root => false)
    BackendMenuNode.create(:root_id => BackendMenu.find_by_path('site-configuracao').id,
      :node_id => BackendMenu.find_by_path('site-configuracao/logs-e-alertas').id,
      :weight => 2)
 
    BackendMenu.create(:title => 'Manutenção do Site',
      :path => 'site-configuracao/manuencao-do-site',
      :is_active => true,
      :is_root => false)
    BackendMenuNode.create(:root_id => BackendMenu.find_by_path('site-configuracao').id,
      :node_id => BackendMenu.find_by_path('site-configuracao/manuencao-do-site').id,
      :weight => 3)
 
    BackendMenu.create(:title => 'Ações',
      :path => 'site-configuracao/acoes',
      :is_active => true,
      :is_root => false)
    BackendMenuNode.create(:root_id => BackendMenu.find_by_path('site-configuracao').id,
      :node_id => BackendMenu.find_by_path('site-configuracao/acoes').id,
      :weight => 4)
 
    BackendMenu.create(:title => 'Regras de Acesso',
      :path => 'gerenciamento-de-usuario/regras-de-acesso',
      :is_active => true,
      :is_root => false)
    BackendMenuNode.create(:root_id => BackendMenu.find_by_path('gerenciamento-de-usuario').id,
      :node_id => BackendMenu.find_by_path('gerenciamento-de-usuario/regras-de-acesso').id,
      :weight => 1)
 
    BackendMenu.create(:title => 'Permissões',
      :path => 'gerenciamento-de-usuario/permissoes',
      :is_active => true,
      :is_root => false)
    BackendMenuNode.create(:root_id => BackendMenu.find_by_path('gerenciamento-de-usuario').id,
      :node_id => BackendMenu.find_by_path('gerenciamento-de-usuario/permissoes').id,
      :weight => 2)
 
    BackendMenu.create(:title => 'Regras',
      :path => 'gerenciamento-de-usuario/regras',
      :is_active => true,
      :is_root => false)
    BackendMenuNode.create(:root_id => BackendMenu.find_by_path('gerenciamento-de-usuario').id,
      :node_id => BackendMenu.find_by_path('gerenciamento-de-usuario/regras').id,
      :weight => 3)
 
    BackendMenu.create(:title => 'Configurações de Usuários',
      :path => 'gerenciamento-de-usuario/configuracoes-de-usuarios',
      :is_active => true,
      :is_root => false)
    BackendMenuNode.create(:root_id => BackendMenu.find_by_path('gerenciamento-de-usuario').id,
      :node_id => BackendMenu.find_by_path('gerenciamento-de-usuario/configuracoes-de-usuarios').id,
      :weight => 4)
 
    BackendMenu.create(:title => 'Usuários',
      :path => 'gerenciamento-de-usuario/usuarios',
      :is_active => true,
      :is_root => false)
    BackendMenuNode.create(:root_id => BackendMenu.find_by_path('gerenciamento-de-usuario').id,
      :node_id => BackendMenu.find_by_path('gerenciamento-de-usuario/usuarios').id,
      :weight => 5)
 
    BackendMenu.create(:title => 'Entrada de Logs',
      :path => 'relatorios/entrada-de-logs',
      :is_active => true,
      :is_root => false)
    BackendMenuNode.create(:root_id => BackendMenu.find_by_path('relatorios').id,
      :node_id => BackendMenu.find_by_path('relatorios/entrada-de-logs').id,
      :weight => 1)
 
    BackendMenu.create(:title => 'Erros de Acesso Negado',
      :path => 'relatorios/erros-de-acesso-negado',
      :is_active => true,
      :is_root => false)
    BackendMenuNode.create(:root_id => BackendMenu.find_by_path('relatorios').id,
      :node_id => BackendMenu.find_by_path('relatorios/erros-de-acesso-negado').id,
      :weight => 2)
 
    BackendMenu.create(:title => 'Status',
      :path => 'relatorios/status',
      :is_active => true,
      :is_root => false)
    BackendMenuNode.create(:root_id => BackendMenu.find_by_path('relatorios').id,
      :node_id => BackendMenu.find_by_path('relatorios/status').id,
      :weight => 3)
  end
 
  def self.down
    BackendMenu.delete_all
  end
end

Se você gostou desse texto e acha que ajudou você, me recomende: Recommend Me.

  • Share/Bookmark

Ruby on Rails Plugin LightBox

Postado em 24 jan 2009
Categoria(s) Ruby on Rails

Conheço um plugin bacana para o Rails para trabalhar com Lightbox, ele usa as seguintes bibliotecas javascripts: http://www.huddletogether.com/projects/lightbox2/.

Para instalar o plugin execute o comando abaixo no diretório da sua aplicação:

1
ruby script/plugin install git://github.com/Lipsiasoft/lightbox.git

No layout da sua aplicação adicione a linha abaixo entre as tags …, por exemplo application.html.erb:

1
<%= yield :stylesheet %>

Agora nos templates onde você deseja usar o lightbox, adicione o css necessário, por exemplo show.html.erb:

1
2
3
<% content_for(:stylesheet) do %>
  <%= stylesheet_link_tag 'lightbox' %>
<% end %>

Esse plugin tem dois helpers lightbox_link_to (para trabalhar com links) e lightbox_image_tag (para trabalhar em cima de um thumbnail):

1
2
3
<%= lightbox_link_to "Link Name", "/path/of/your/image.png" %>
ou
<%= lightbox_image_tag("/path/of/your/image-thumb.png", "/path/of/your/image.png", {:class=>"images"}, :title => "This is a test!") %>

Até mais! :-)

Se você gostou desse texto e acha que ajudou você, me recomende: Recommend Me.

  • Share/Bookmark

Instalando e usando o plugin paperClip no Ruby on Rails

Postado em 18 dez 2008
Categoria(s) Ruby on Rails

O plugin paperClip do Ruby on Rails é muito bacana, agiliza muito a vida, com ele é possível fazer upload de imagens e já gerar várias dimensões da imagem.

Vou explicar como instalar e como usar.

Instalação:

Faça o download do paperClip: http://github.com/tarballs/thoughtbot-paperclip-18c0246c11c51dafa77b6367ddaf730684d0e752.zip

Descompacte o arquivo e renomei o diretório extraído para paperclip.

Coloque esse diretório dentro de seu_projeto/vendor/plugins/paperclip.

Você vai precisar do imageMagick, através dele o paperClip faz o resize nas imagens. Para instalar no Ubuntu 8.04 faça:

1
sudo apt-get install imagemagick

Configuração:

Para o paperClip funcionar você vai precisar que exista 4 novas colunas na sua tabela são elas: image_file_name, image_content_type, image_file_size, image_updated_at.

Esse nome image_… pode ser qualquer outra coisa por exemplo: avatar_…, foto_…

Seguindo essa ideia você poderia ter um migrate mais ou menos assim:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class CreateTournaments < ActiveRecord::Migration
  def self.up
    create_table :tournaments do |t|
      t.column :name,               :string,    :limit => 100, :null => false
      t.column :description,        :text,      :null => true
      t.column :image_file_name,    :string
      t.column :image_content_type, :string
      t.column :image_file_size,    :integer
      t.column :image_updated_at,   :timestamp
      t.column :is_active,          :boolean,   :null => false, :default => false
      t.column :start_date,         :date,      :null => false
      t.column :end_date,           :date,      :null => false
      t.column :created_at,         :timestamp, :null => false
      t.column :updated_at,         :timestamp, :null => false
    end
  end
 
  def self.down
    drop_table :tournaments
  end
end

Agora é necessário informar o seu model que existirá anexos e que formatos são aceitos:

1
2
3
4
5
6
7
8
9
10
class Tournament < ActiveRecord::Base
  has_attached_file :image,
                    :styles => {:large => '600x600>', :medium => '300x300>', :small => '150x150>', :thumb => '50x50>'},
                    :path => ":rails_root/public/images/:class/:id/:style_:basename.:extension",
                    :url => "/images/:class/:id/:style_:basename.:extension"
 
  validates_uniqueness_of :name
  validates_presence_of   :name, :start_date, :end_date
  validates_attachment_content_type :image, :content_type => ['image/jpeg', 'image/png', 'image/gif']
end

O interessante da linha abaixo, é que posso definir todos os tamanhos de imagens que desejo gerar em cima da imagem original, nesse trecho de código eu também alterei o path de armazenamento das imagens, fiz as imagens ficarem no diretório public_html/images/tournaments/id_do_banco_de_dados/imagens_em_diversas_dimensões:

1
2
3
4
  has_attached_file :image,
                    :styles => {:large => '600x600>', :medium => '300x300>', :small => '150x150>', :thumb => '50x50>'},
                    :path => ":rails_root/public/images/:class/:id/:style_:basename.:extension",
                    :url => "/images/:class/:id/:style_:basename.:extension"

A linha seguinte informa os tipos de mime types aceitos:

1
  validates_attachment_content_type :image, :content_type => ['image/jpeg', 'image/png', 'image/gif']

Agora você precisa definir que o seu formulário de cadastro trabalha com multipart, para enviar dados binários:

1
2
3
4
  <% form_for :tournament, @tournament, :url => { :action => 'create' }, :html => { :multipart => true } do |form| %>
    <%= render :partial => 'form', :locals => { :form => form } %>
    <div class="submit"><%= submit_tag "Criar" %></div>
  <% end %>

No código acima eu chamo o partial _form.rhtml, segue o seu conteúdo abaixo:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<%= error_messages_for 'tournament' %>
 
<!--[form:tournament]-->
<p><label for="tournament_name">Nome</label>
<%= form.text_field :name  %></p>
 
<p><label for="tournament_description">Descrição</label>
<%= form.text_area :description  %></p>
 
<p><label for="tournament_image">Imagem</label>
<%= form.file_field :image %></p>
 
<p><label for="tournament_is_active">Ativo</label>
<%= form.check_box :is_active  %>
 
<p><label for="tournament_start_date">Data de início</label>
<%= form.date_select(:start_date, :order => [:day, :month, :year], :use_month_numbers => true) %></p>
 
<p><label for="tournament_end_date">Data de término</label>
<%= form.date_select(:end_date, :order => [:day, :month, :year], :use_month_numbers => true)  %></p>
<!--[eoform:tournament]-->

Nesse partial _form.rhtml é declarado o campo para upload da imagem:

1
<%= form.file_field :image %>

Perceba que na tabela do banco de dados não existe a coluna chamada image, mas sim aquelas 4 que criei no migrate. Aqui que entra a mágica do paperClip juntamente com o model, ele consegue fazer o upload da imagem e definir os valores para os 4 campos no model.

Para exibir as imagens e suas diversas proporções use:

1
2
3
4
<%= image_tag @tournament.image.url(:thumb)  %>
<%= image_tag @tournament.image.url(:small)  %>
<%= image_tag @tournament.image.url(:medium)  %>
<%= image_tag @tournament.image.url(:large)  %>

Pronto! Depois de pegar o jeito com o paperClip você vai dar risada a toa. ;-)

Se você gostou desse texto e acha que ajudou você, me recomende: Recommend Me.

  • Share/Bookmark