Posts Tagged ‘create’

Ruby on Rails plugin tableless_model

Postado em 11 ago 2009
Categoria(s) Plugins, Ruby on Rails

tableless_model

Ruby on Rails model sem banco de dados, com todo o poder do ActiveRecord e validações.

A principal utilidade é usar em formulários que não precisam de banco de dados, mas precisam das validações do ActiveRecord, form_for e rotas RESTful. Por exemplo formulário de contato.

Instalação

./script/plugin install git://github.com/patrickespake/tableless_model.git

Geradores

Gerador tableless_model

Cria o esboço de um novo tableless model. Passe o nome do tableless model, em CamelCased ou under_scored, e uma lista de pares de atributos opcionais como argumentos.

Os atributos pares opcionais são column_name:sql_type como argumentos, especificando os atributos do tableless model.

Você não tem que pensar em cada atributos lá na frente, mas ajuda a esboçar um pouco para que você possa começar a trabalhar com o tableless model imediatamente.

Isto gera uma classe tableless model em app/models, um teste de unidade em test/unit e uma fixture em test/fixtures/singular_name.yml

Exemplos:

./script/generate tableless_model contact

Cria o tableless model Contact, test e fixture:

  • Model: app/models/contact.rb
  • Test: test/unit/contact_test.rb
  • Fixtures: test/fixtures/contacts.yml
./script/generate tableless_model invite name:string body:text email:string

Cria o tableless model Invite com a string name, text body e string email.

Gerador tableless_scaffold

Tableless scaffolds é composto por vários recursos, a partir do model, controller e views, juntamente com um conjunto completo de testes. O recurso está pronto para ser usado como um ponto de partida, sendo RESTful, orientado para o pedido.

Passe o nome do tableless model (na forma singular), em CamelCased ou under_scored, como primeiro argumento, e uma lista de pares de atributos opcionais.

Os pares de atributos opcionais são column_name:sql_type, especificando os atributos do modelo.

Você não tem que pensar em cada atributo lá na frente, mas ajuda esboçar um pouco para que você possa começar a trabalhar com os recursos imediatamente.

Por exemplo ‘tableless_scaffold contact name:string telephone:string firm:string email:string message:text’ dá-lhe um tableless model com cinco atributos, um controlador para lidar com a criação, formulário para criar os contatos e routas declaradas em config/routes.rb.

Se você quiser remover todos os arquivos gerados, execute:

script/destroy scaffold ModelName

Exemplos:

./script/generate tableless_scaffold invite
./script/generate tableless_scaffold contact name:string telephone:string firm:string email:string message:text

Tipos de colunas disponíveis nos tableless_models

1
2
3
4
5
6
7
class ModelName < TablelessModel
  column :column_name1, :column_type
  column :column_name2, :column_type
  column :column_name3, :column_type
  column :column_nameN, :column_type
  ...
end

Tipos de colunas (column_type) disponíveis:

  • :string
  • :text
  • :integer
  • :float
  • :decimal
  • :datetime
  • :timestamp
  • :time
  • :date
  • :binary
  • :boolean

Validações do ActiveRecord funcionam perfeitamente

1
2
3
4
5
6
7
8
9
10
11
class Contact < TablelessModel
  column :name, :string
  column :telephone, :string
  column :firm, :string
  column :email, :string
  column :message, :text
 
  validates_presence_of :name, :email, :message
  validates_format_of :email, :with => /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i
  validates_length_of :message, :minimum => 3
end

Exemplo completo usando tableless_scaffold gerador

Criar o tableless scaffold

./script/generate tableless_scaffold contact name:string telephone:string firm:string email:string message:text

Adicionar as validações no model

Abra o arquivo app/models/contact.rb e adicione as validações:

1
2
3
4
5
6
7
8
9
10
11
12
class Contact < TablelessModel
  column :name, :string
  column :telephone, :string
  column :firm, :string
  column :email, :string
  column :message, :text
 
  # Validations
  validates_presence_of :name, :email, :message
  validates_format_of :email, :with => /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i
  validates_length_of :message, :minimum => 3
end

Execute

./script/server

Abra o seu navegador em: http://localhost:3000/contacts/new

Exemplo completo usando tableless_model gerador

Criar tableless model

./script/generate tableless_model contact name:string telephone:string firm:string email:string message:text

Adicionar as validações no model

Abra o arquivo app/models/contact.rb e adicione as validações:

1
2
3
4
5
6
7
8
9
10
11
12
class Contact < TablelessModel
  column :name, :string
  column :telephone, :string
  column :firm, :string
  column :email, :string
  column :message, :text
 
  # Validations
  validates_presence_of :name, :email, :message
  validates_format_of :email, :with => /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i
  validates_length_of :message, :minimum => 3
end

Criar o controlador

./script/generate controller contacts new create

Adicionar os códigos das actions:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class ContactController < ApplicationController
  def new
    @contact = Contact.new
  end
 
  def create
    @contact = Contact.new(params[:contact])
 
    if @contact.save # or @contact.valid?
      flash[:notice] = "Contact email sent successfully."
 
      # Send contact email
      #MyMailer.deliver_contact(@contact)
 
      redirect_to new_contact_path
    else
      render :action => "new"
    end
  end
end

Criar as routas para os contacts

Abra o arquivo config/routes.rb e adicione a linha:

1
2
3
4
ActionController::Routing::Routes.draw do |map|
  map.resources :contacts, :only => [:new, :create]
  ...
end

Criar o formulário de contato

Abra o arquivo app/views/contacts/new.html.erb e adicione o conteúdo:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<% form_for(@contact) do |f| %>
  <%= f.error_messages %>
 
  <%= f.label :name %>
  <%= f.text_field :name %>
 
  <%= f.label :telephone %>
  <%= f.text_field :telephone %>
 
  <%= f.label :firm %>
  <%= f.text_field :firm %>
 
  <%= f.label :email %>
  <%= f.text_field :email %>
 
  <%= f.label :message %>
  <%= f.text_area :message %>
 
  <%= f.submit "Send" %>
<% end %>

Apague o arquivo app/views/contacts/create.html.erb.

Execute

./script/server

Abra o seu navegador em: http://localhost:3000/contacts/new

Documentação

http://lab.patrickespake.com/tableless_model

Código fonte

http://github.com/patrickespake/tableless_model

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

  • Share/Bookmark

Ruby on Rails acts_as_tree

Postado em 06 abr 2009
Categoria(s) Ruby on Rails

No Ruby on Rails é possível fazer um model trabalhar em árvore, isso é algo muito interessante quando você precisa trabalhar com categorias e subcategorias. Essa árvore não tem limite de crescimento, você pode ter várias subcategorias dentro de subcategorias.

Esse comportamento de árvore do model recebe o nome de acts_as_tree.

No Ruby on Rails 1.2 o acts_as_tree fazia parte do ActiveRecord, no Ruby on Rails 2 ele não faz mais parte, para usar você precisa instalar o plugin acts_as_tree.

Instalação:

1
ruby script/plugin install acts_as_tree

Configuração:

É necessário ter mais uma coluna na tabela chamada parent_id através dessa coluna é possível estabelecer a relação entre pais e filhos, ou seja, categoria e subcategorias.

Por exemplo:

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.integer :parent_id
      t.timestamps
    end
  end
 
  def self.down
    drop_table :backend_menus
  end
end

Agora no model você deve adicionar o comportamento de árvore acts_as_tree e especificar uma coluna para ordenação:

1
2
3
4
class BackendMenu < ActiveRecord::Base
  # comportamento de árvore
  acts_as_tree :order => 'weight'
end

Desta forma serão criados alguns métodos para você conseguir trabalhar com a árvore:

Representação do Model com Acts as Tree

Representação do Model com Acts as Tree

1
2
3
4
5
6
7
8
root      = Category.create("name" => "root")
child1    = root.children.create("name" => "child1")
subchild1 = child1.children.create("name" => "subchild1")
 
root.parent   # => nil
child1.parent # => root
root.children # => [child1]
root.children.first.children.first # => subchild1

Existe mais 4 métodos que podem ser úteis:

  • ancestors: Retorna lista dos antepassados, a partir da mãe até raiz;
  • root: Retorna o nó raiz da árvore;
  • siblings: Retorna todos os irmãos do nó atual;
  • self_and_siblings: Retorna todos os irmãos e uma referência ao nó atual.

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 routes RESTful usando only

Postado em 02 fev 2009
Categoria(s) Ruby on Rails

Quando você usa no arquivo routes.rb:

1
map.resources :photos

O Rails cria 7 actions padrões (index, show, new, create, edit, update, e destroy).

Você pode querer apenas usar as actions index e show, desta forma é possível fazer a seguinte configuração:

1
map.resources :photos, : only => [:index, :show]

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