templus_models 1.0.3 → 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (56) hide show
  1. checksums.yaml +4 -4
  2. data/app/controllers/crud_controller.rb +13 -3
  3. data/app/raro_crud/raro_crud.rb +27 -0
  4. data/app/views/crud/_show.html.erb +1 -1
  5. data/lib/templus_models/version.rb +1 -1
  6. data/test/dummy/app/assets/javascripts/crud/cidade_estado.js +17 -0
  7. data/test/dummy/app/controllers/api/cidades_controller.rb +12 -0
  8. data/test/dummy/app/models/cidade.rb +7 -0
  9. data/test/dummy/app/models/endereco.rb +10 -0
  10. data/test/dummy/app/models/estado.rb +6 -0
  11. data/test/dummy/app/models/usuario.rb +22 -0
  12. data/test/dummy/app/raro_crud/usuario_crud.rb +21 -0
  13. data/test/dummy/config/routes.rb +8 -0
  14. data/test/dummy/db/development.sqlite3 +0 -0
  15. data/test/dummy/db/migrate/20150413190054_devise_create_usuarios.rb +48 -0
  16. data/test/dummy/db/migrate/20150422182314_create_estados.rb +10 -0
  17. data/test/dummy/db/migrate/20150422182356_create_cidades.rb +10 -0
  18. data/test/dummy/db/migrate/20150422191007_create_enderecos.rb +15 -0
  19. data/test/dummy/db/schema.rb +56 -1
  20. data/test/dummy/db/seeds.rb +5624 -0
  21. data/test/dummy/log/development.log +26541 -0
  22. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/2n4o2Z5O1JcHUAc3RFWjzIypI20q80wzxzVM4r_ERyw.cache +3 -0
  23. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/3Z3oGZ7T5IvqZcC67gpK4AsOeigoZO6gkC_I5i1YQ5A.cache +2 -0
  24. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/48AnIgWH6arKl7IWqrQgSblyh-a2hg4DufQUe-XkSmM.cache +0 -0
  25. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/5Ndg_OdhlKlE9zJO3mlVyrVPI0_bue64GCfgJ-kUmVo.cache +2 -0
  26. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/6m2nT2sJ8UDyzZkXUAAW6Pbiz2pvQgZ5jY29JIgUAOA.cache +2 -0
  27. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/7I9bH3rdmxwNbCt77t731g2Mx16eBn8oHbuEXDtkzAY.cache +1 -0
  28. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/Bf_cXm5yRDkEVRWJfdkA3tistagWcoXDUCUrfuVzXis.cache +0 -0
  29. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/Cr_0E0-U36_UBFEcSxvfBo2xGo04ASyPbLyUR5wcoFA.cache +2 -0
  30. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/F1xQnpAbpWPtijPIumF7Mt7BZPqIfkGef_w0UnG7OcQ.cache +1 -0
  31. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/HROWT9uEGxn_97GEavSx4eg5DflXU7xHiBrizLNAZUs.cache +0 -0
  32. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/I_Wv7_flyMhSy-BlIV3JA0-WelDRC2mn2RmmhYfKy_4.cache +3 -0
  33. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/Nc4tSZSTnSftkugBRsLqrJt6PWjfoXUP1JxnUS7Lmoo.cache +1 -0
  34. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/NqIIG9vEMA2rJeI9_RCC4PL57N-nwwvpc0HheYeTQKc.cache +1 -0
  35. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/OZmh6w5cQWDI7X8zl3l3m6g_VCjzQEPP-HleReRacm8.cache +1 -0
  36. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/Pf39W9UNHDCDw0mdshzvS-ApNuBzTlHc4sw-9Iz7A0M.cache +0 -0
  37. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/SI8pUq0PxuwXS9qNvSUcybYeEvoJar3xNgpUsg-AVNc.cache +1 -0
  38. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/U7yDOKX_mqpe1QCteIpyU6m1KDbERJnrVcJtS95fl1s.cache +1 -0
  39. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/Zzp263L3L2mOQdG5WdddN5Tr4DoC0FU8U0Svz8xwSlg.cache +1 -0
  40. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/_ND0K6iu-DFf8Q9RDqC5TyS_C7WQ4mCMK75_d1THVWM.cache +0 -0
  41. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/bQqFgBTqmKVxAiTlaFQLPPe7RdqsJwQpFLLWBWM776g.cache +0 -0
  42. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/fBWxV0VDeTvyrcjQoTFNVTsDxU04jcT8hkdAYEDksmY.cache +0 -0
  43. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/gnpi3Bx1XX3VKtj8NZW7rpLToQiWKUuSe_xCA20-Jeg.cache +2 -0
  44. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/hHWSuLTSFbDONR0bPE9-dfqlZVUZj1etJcIv6CLvkis.cache +3 -0
  45. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/jKZwGxu-MspFTQpV8Bo159tfIzPlzHZY9e63kge28kg.cache +1 -0
  46. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/mIqMWu7NYKfgu01OyoiiQ6Z0BVPsAnI-O4-8JtB99k0.cache +1 -0
  47. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/msvpEL1bFwTEGFJ1AU8KRblobsK8eyes0H9hwZwknl4.cache +1 -0
  48. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/nEvoDJz-5P_PlvfurtbPzs1iodmH5zGUdJnq29Z1R3c.cache +0 -0
  49. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/r--UPoL2lUOYmmTmoIXvy39WQ5ggoZKwcX5m9iFR91M.cache +2 -0
  50. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/rXwGOWeBWEiikeu2lkRWFkggCVqTbDO6PBAE8svHo0s.cache +0 -0
  51. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/t7C9Ylg3mowOkxk8QdDBdWidoTLVkf6-P3lNALl1T4E.cache +0 -0
  52. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/tInRy6UTWOkZalX6bISdnoOmfBRBtCjN0rPjRkMlhyw.cache +2 -0
  53. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/uZBa6WaCpX47IM0j8YYF2y-eAX83Xib_mFlOPH9duPQ.cache +1 -0
  54. metadata +90 -6
  55. data/test/dummy/app/raro_crud/#{file_name}_crud.rb +0 -0
  56. data/test/dummy/app/raro_crud/pessoa_crud.rb +0 -50
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5416eadc5dfb02b33423613e21ecd0d4112b64d1
4
- data.tar.gz: 4ed46660ad993f61e05a08d8690bcb3e413db680
3
+ metadata.gz: 6ae4ac517bb260f07241bc4dcda339dee552a95d
4
+ data.tar.gz: efac55165abfa827d5579bf7cd2e84375127ff1f
5
5
  SHA512:
6
- metadata.gz: 07e8a4ca8cbd5561af6a8892ecd279a54ebacdfaa6c887f578846bc7a00a8be32e38e5a26bccfb565dbf259fa67bf263c6d8e1733a1ea7cfaab5d05f155b8066
7
- data.tar.gz: f57354012a1de695d90378856005f381345fc671a5a12200ea17bcc4db40f03e39a3cb2c8985a86cb81437173cd7a1f4b1a2c0b73c8f5e32f922119383721b31
6
+ metadata.gz: 5f0b6acb81a5f1361d9ce228fabf6f3708cebdfb24ae8e042da7e67418a534c074f23d89de40d6d165ef04fb4c6c9a41af5b06d70485c927254189aa0d6f3dd3
7
+ data.tar.gz: 71a4c2b6cad80f5ca692a46f392816d1409263ca858775a758469cd6ed1d34a91fe93bc0fdcee722946afa55d0e8db704935b44c29dd63b68e2d085833e91076
@@ -144,9 +144,19 @@ class CrudController < ApplicationController
144
144
  end
145
145
  end
146
146
  @crud_helper.form_groups.each do |key, groups|
147
- group = {"#{key}_attributes" => [:id, :_destroy]}
148
- groups.each do |field|
149
- group["#{key}_attributes"] << field[:attribute]
147
+ chave = "#{key}_attributes"
148
+ group = {chave => [:id, :_destroy]}
149
+ groups.each do |field|
150
+ modelo = key.to_s.camelcase.constantize
151
+ if modelo.reflect_on_association(field[:attribute])
152
+ if modelo.reflect_on_association(field[:attribute]).macro == :belongs_to
153
+ group[chave] << "#{field[:attribute]}_id".to_sym
154
+ else
155
+ group[chave] << {"#{field[:attribute].to_s.singularize}_ids".to_sym => []}
156
+ end
157
+ elsif (modelo.columns_hash[field[:attribute].to_s] || (modelo.respond_to?(:params_permitt) && modelo.params_permitt.include?(field[:attribute].to_sym)))
158
+ group[chave] << field[:attribute]
159
+ end
150
160
  end
151
161
  fields << group
152
162
  end
@@ -267,6 +267,33 @@ class RaroCrud
267
267
  end
268
268
  end
269
269
 
270
+ def self.adicionar_endereco
271
+ @@form_group[self.to_s.to_sym] ||= {}
272
+ @@form_group[self.to_s.to_sym][:endereco] ||= []
273
+ [
274
+ {campo: :cep, label: "CEP"},
275
+ {campo: :logradouro, label: "Endereço"},
276
+ {campo: :numero, label: "Número"},
277
+ {campo: :complemento, label: "Complemento"},
278
+ {campo: :bairro, label: "Bairro"},
279
+ {campo: :estado, label: "Estado", collection: Estado.order(:sigla).pluck(:sigla)},
280
+ {campo: :cidade_id, label: "Cidade", collection_if: Proc.new{|f| f.try(:object).try(:new_record?) ? [] : (f.try(:object).try(:estado).try(:cidades) || [])}}
281
+ ].each do |field|
282
+ value = {}
283
+ field.each do |atr|
284
+ if atr[0] == :campo
285
+ value[:attribute] = atr[1]
286
+ else
287
+ value[:sf] ||= {}
288
+ value[:sf][atr[0]] = atr[1]
289
+ end
290
+ end
291
+ @@form_group[self.to_s.to_sym][:endereco].push({attribute: value[:attribute],sf: value[:sf]})
292
+ end
293
+ @@form_scripts[self.to_s.to_sym] ||= []
294
+ @@form_scripts[self.to_s.to_sym] << "cidade_estado"
295
+ end
296
+
270
297
  def self.script_formulario(script)
271
298
  @@form_scripts[self.to_s.to_sym] ||= []
272
299
  @@form_scripts[self.to_s.to_sym] << script.to_s
@@ -16,7 +16,7 @@
16
16
  <% else %>
17
17
  <td><%= l @record.send(field[:attribute]) if @record.send(field[:attribute]).present?%></td>
18
18
  <% end %>
19
- <% elsif @model.reflect_on_association(field[:attribute]) && @model.reflect_on_association(field[:attribute]).macro != :belongs_to%>
19
+ <% elsif @model.reflect_on_association(field[:attribute]) && @model.reflect_on_association(field[:attribute]).macro != :belongs_to && @model.reflect_on_association(field[:attribute]).macro != :has_one%>
20
20
  <td>
21
21
  <% @record.send(field[:attribute]).each do |rec| %>
22
22
  <li>
@@ -1,3 +1,3 @@
1
1
  module TemplusModels
2
- VERSION = "1.0.3"
2
+ VERSION = "1.0.4"
3
3
  end
@@ -0,0 +1,17 @@
1
+ $(document).ready(function (){
2
+ $("select[id$='_estado']").on('change', function() {
3
+ $.getJSON('/api/cidades/busca?estado=' + $(this).val(), {
4
+ format: 'json'
5
+ }, function(data) {
6
+ var i, options;
7
+ options = "";
8
+ i = 0;
9
+ while (i < data.length) {
10
+ options += "<option value='" + data[i].id + "'>" + data[i].nome + "</option>";
11
+ i++;
12
+ }
13
+ $("select[id$='cidade_id']").html(options).show();
14
+ $("select[id$='cidade_id']").trigger('select');
15
+ });
16
+ });
17
+ });
@@ -0,0 +1,12 @@
1
+ class Api::CidadesController < ApplicationController
2
+ def busca
3
+ respond_to do |format|
4
+ @estado = Estado.find_by(sigla: params[:estado])
5
+ puts "*******************************************"
6
+ p @estado
7
+ puts "*******************************************"
8
+ @cidades = Cidade.por_estado(@estado.try(:id))
9
+ format.json { render json: @cidades }
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,7 @@
1
+ class Cidade < ActiveRecord::Base
2
+ belongs_to :estado
3
+ scope :por_estado, -> (estado_id) {where(estado_id: estado_id)}
4
+ def to_s
5
+ nome
6
+ end
7
+ end
@@ -0,0 +1,10 @@
1
+ class Endereco < ActiveRecord::Base
2
+ belongs_to :cidade
3
+ delegate :estado, to: :cidade, allow_nil: true
4
+ belongs_to :usuario, :class_name => "Usuario", :foreign_key => "usuario_id"
5
+ def to_s
6
+ if logradouro.present?
7
+ "#{logradouro}, #{numero} #{complemento}, #{bairro}, #{cidade.try(:cidade_estado)}"
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,6 @@
1
+ class Estado < ActiveRecord::Base
2
+ has_many :cidades, :class_name => "Cidade", :foreign_key => "estado_id"
3
+ def to_s
4
+ sigla
5
+ end
6
+ end
@@ -0,0 +1,22 @@
1
+ class Usuario < ActiveRecord::Base
2
+
3
+ validates_presence_of :nome, :email
4
+ has_one :endereco, :class_name => "Endereco", :foreign_key => "usuario_id"
5
+ accepts_nested_attributes_for :endereco, :allow_destroy => true
6
+
7
+ def to_s
8
+ nome
9
+ end
10
+
11
+ def ability
12
+ @ability ||= Ability.new(self)
13
+ end
14
+
15
+ def self.current
16
+ return Thread.current[:current_usuario]
17
+ end
18
+
19
+ def self.current=(usuario)
20
+ Thread.current[:current_usuario] = usuario
21
+ end
22
+ end
@@ -0,0 +1,21 @@
1
+ class UsuarioCrud < RaroCrud
2
+ titulo "Usuários"
3
+
4
+ link_superior "Novo Usuário", id: "novo-button", icon: "plus", link: "new"
5
+
6
+ ordenar_por :nome
7
+
8
+ campo_tabela :nome, label: "Nome"
9
+ campo_tabela :email, label: "email"
10
+
11
+ campo_formulario :nome, label: "Nome"
12
+ campo_formulario :email, label: "Email", default_test_pos: "@rarolabs.com"
13
+ adicionar_endereco
14
+
15
+ campo_visualizacao :nome, label: "Nome"
16
+ campo_visualizacao :email, label: "email"
17
+ campo_visualizacao :endereco, label: "Endereço"
18
+
19
+ campo_busca :nome, label: "Nome"
20
+ campo_busca :email, label: "email"
21
+ end
@@ -53,4 +53,12 @@ Rails.application.routes.draw do
53
53
  # # (app/controllers/admin/products_controller.rb)
54
54
  # resources :products
55
55
  # end
56
+
57
+ namespace :api do
58
+ resources :cidades, only: [] do
59
+ collection do
60
+ get 'busca'
61
+ end
62
+ end
63
+ end
56
64
  end
Binary file
@@ -0,0 +1,48 @@
1
+ class DeviseCreateUsuarios < ActiveRecord::Migration
2
+ def change
3
+ create_table(:usuarios) do |t|
4
+ ## Database authenticatable
5
+ t.string :email, null: false, default: ""
6
+ t.string :encrypted_password, null: false, default: ""
7
+
8
+ ## Recoverable
9
+ t.string :reset_password_token
10
+ t.datetime :reset_password_sent_at
11
+
12
+ ## Rememberable
13
+ t.datetime :remember_created_at
14
+
15
+ ## Trackable
16
+ t.integer :sign_in_count, default: 0, null: false
17
+ t.datetime :current_sign_in_at
18
+ t.datetime :last_sign_in_at
19
+ t.string :current_sign_in_ip
20
+ t.string :last_sign_in_ip
21
+
22
+ ## Confirmable
23
+ # t.string :confirmation_token
24
+ # t.datetime :confirmed_at
25
+ # t.datetime :confirmation_sent_at
26
+ # t.string :unconfirmed_email # Only if using reconfirmable
27
+
28
+ ## Lockable
29
+ t.integer :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts
30
+ # t.string :unlock_token # Only if unlock strategy is :email or :both
31
+ t.datetime :locked_at
32
+
33
+ ## Permissions
34
+ t.references :papel, index: true
35
+ t.boolean :root, default: false
36
+
37
+ ## Informações adicionais
38
+ t.string :nome
39
+
40
+ t.timestamps
41
+ end
42
+
43
+ add_index :usuarios, :email, unique: true
44
+ add_index :usuarios, :reset_password_token, unique: true
45
+ # add_index :usuarios, :confirmation_token, unique: true
46
+ # add_index :usuarios, :unlock_token, unique: true
47
+ end
48
+ end
@@ -0,0 +1,10 @@
1
+ class CreateEstados < ActiveRecord::Migration
2
+ def change
3
+ create_table :estados do |t|
4
+ t.string :nome
5
+ t.string :sigla
6
+
7
+ t.timestamps null: false
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ class CreateCidades < ActiveRecord::Migration
2
+ def change
3
+ create_table :cidades do |t|
4
+ t.string :nome
5
+ t.references :estado, index: true, foreign_key: true
6
+
7
+ t.timestamps null: false
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,15 @@
1
+ class CreateEnderecos < ActiveRecord::Migration
2
+ def change
3
+ create_table :enderecos do |t|
4
+ t.string :cep
5
+ t.string :logradouro
6
+ t.string :complemento
7
+ t.string :numero
8
+ t.string :bairro
9
+ t.references :cidade, index: true, foreign_key: true
10
+ t.references :usuario, index: true, foreign_key: true
11
+
12
+ t.timestamps null: false
13
+ end
14
+ end
15
+ end
@@ -11,7 +11,38 @@
11
11
  #
12
12
  # It's strongly recommended that you check this file into your version control system.
13
13
 
14
- ActiveRecord::Schema.define(version: 20150416220930) do
14
+ ActiveRecord::Schema.define(version: 20150422191007) do
15
+
16
+ create_table "cidades", force: :cascade do |t|
17
+ t.string "nome"
18
+ t.integer "estado_id"
19
+ t.datetime "created_at", null: false
20
+ t.datetime "updated_at", null: false
21
+ end
22
+
23
+ add_index "cidades", ["estado_id"], name: "index_cidades_on_estado_id"
24
+
25
+ create_table "enderecos", force: :cascade do |t|
26
+ t.string "cep"
27
+ t.string "logradouro"
28
+ t.string "complemento"
29
+ t.string "numero"
30
+ t.string "bairro"
31
+ t.integer "cidade_id"
32
+ t.integer "usuario_id"
33
+ t.datetime "created_at", null: false
34
+ t.datetime "updated_at", null: false
35
+ end
36
+
37
+ add_index "enderecos", ["cidade_id"], name: "index_enderecos_on_cidade_id"
38
+ add_index "enderecos", ["usuario_id"], name: "index_enderecos_on_usuario_id"
39
+
40
+ create_table "estados", force: :cascade do |t|
41
+ t.string "nome"
42
+ t.string "sigla"
43
+ t.datetime "created_at", null: false
44
+ t.datetime "updated_at", null: false
45
+ end
15
46
 
16
47
  create_table "teste1s", force: :cascade do |t|
17
48
  t.string "descricao"
@@ -28,4 +59,28 @@ ActiveRecord::Schema.define(version: 20150416220930) do
28
59
  t.datetime "updated_at"
29
60
  end
30
61
 
62
+ create_table "usuarios", force: :cascade do |t|
63
+ t.string "email", default: "", null: false
64
+ t.string "encrypted_password", default: "", null: false
65
+ t.string "reset_password_token"
66
+ t.datetime "reset_password_sent_at"
67
+ t.datetime "remember_created_at"
68
+ t.integer "sign_in_count", default: 0, null: false
69
+ t.datetime "current_sign_in_at"
70
+ t.datetime "last_sign_in_at"
71
+ t.string "current_sign_in_ip"
72
+ t.string "last_sign_in_ip"
73
+ t.integer "failed_attempts", default: 0, null: false
74
+ t.datetime "locked_at"
75
+ t.integer "papel_id"
76
+ t.boolean "root", default: false
77
+ t.string "nome"
78
+ t.datetime "created_at"
79
+ t.datetime "updated_at"
80
+ end
81
+
82
+ add_index "usuarios", ["email"], name: "index_usuarios_on_email", unique: true
83
+ add_index "usuarios", ["papel_id"], name: "index_usuarios_on_papel_id"
84
+ add_index "usuarios", ["reset_password_token"], name: "index_usuarios_on_reset_password_token", unique: true
85
+
31
86
  end