scaf-generator 0.2.3 → 0.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,18 @@
1
+ Description:
2
+ Generador Scaffold-Like adaptado a las necesidades de Xaver.
3
+
4
+ Example:
5
+ rails generate prueba Thing
6
+
7
+ This will create:
8
+ app/controllers/admin/things_controller.rb
9
+ app/models/thing.rb
10
+ app/helpers/thing_helper.rb
11
+
12
+ app/views/admin/_form.rb
13
+ app/views/admin/_mini_form.rb
14
+ app/views/admin/_thing.rb
15
+ app/views/admin/edit.rb
16
+ app/views/admin/index.rb
17
+ app/views/admin/show.rb
18
+
@@ -0,0 +1,68 @@
1
+ # -*- encoding : utf-8 -*-
2
+ class Atributo
3
+ CLASES = [:string, :text, :integer, :float, :decimal, :datetime, :timestamp, :time, :date, :binary, :boolean, :references]
4
+ VALORES = [:true, :false, :nil]
5
+ OPCIONES = [:unique, :index, :polymorphic]
6
+ ANCHO = 15
7
+ attr_accessor :nombre, :clase, :default, :opciones
8
+
9
+ def initialize(s)
10
+ parametros = s.split(':')
11
+
12
+ @nombre = parametros.shift
13
+ @clase = :string if parametros.empty?
14
+ @opciones = []
15
+
16
+ parametros.each do |parametro|
17
+ incluir parametro.to_sym
18
+ end
19
+
20
+ end
21
+
22
+ def incluir(parametro)
23
+ if CLASES.include? parametro
24
+ @clase = parametro
25
+ end
26
+
27
+ if VALORES.include? parametro
28
+ @clase ||= :boolean
29
+ @default = parametro
30
+ end
31
+
32
+ if OPCIONES.include? parametro
33
+ @opciones << parametro
34
+ end
35
+ end
36
+
37
+ def es?(opcion)
38
+ @opciones.include? opcion
39
+ end
40
+
41
+ def tiene_opciones?
42
+ ! @opciones.empty?
43
+ end
44
+
45
+ def ejemplo
46
+ case @clase
47
+ when :string then "'Lorem Ipsum'"
48
+ when :integer then rand(50)
49
+ else ":ejemplo"
50
+ end
51
+ end
52
+
53
+ # Este metodo es usado solo para testear.
54
+ # TODO: Eliminar metodo.
55
+ def show
56
+
57
+ n1 = ANCHO - @nombre.size
58
+ n2 = ANCHO - @clase.size
59
+
60
+ n3 = @default ? ANCHO - @default.size : ANCHO
61
+
62
+ s1 = ' ' * n1
63
+ s2 = ' ' * n2
64
+ s3 = ' ' * n3
65
+
66
+ "#{@nombre}#{s1}#{@clase}#{s2}#{@default}#{s3}#{@opciones}\n"
67
+ end
68
+ end
@@ -0,0 +1,101 @@
1
+ # -*- encoding : utf-8 -*-
2
+
3
+ require 'generators/prueba/atributo'
4
+
5
+ class PruebaGenerator < Rails::Generators::Base
6
+ source_root File.expand_path('../templates', __FILE__)
7
+
8
+ argument :name, :type => :string, :required => true, :banner => 'Nombre'
9
+ argument :args, :type => :array , :default => [] , :banner => 'Atributos'
10
+
11
+ class_option :orden , :desc => 'Soporte para Ordenable.' , :type => :boolean, :aliases => "-o", :default => false
12
+ class_option :slug , :desc => 'Soporte pare FriendlyId.' , :type => :boolean, :aliases => "-i", :default => false
13
+ class_option :archivo , :desc => 'Soporte para tener un archivo.', :type => :boolean, :aliases => "-a", :default => false
14
+
15
+
16
+ def initialize(*arguments, &block)
17
+ super
18
+
19
+ @con_orden = options.orden?
20
+ @con_slug = options.slug?
21
+ @con_archivo = options.foto?
22
+
23
+ @singular = @name.downcase
24
+ @plural = @singular.pluralize
25
+ @class = @name.camelize
26
+ @classes = @plural.camelize
27
+
28
+ @singular_path = "admin_#{@singular}_path"
29
+ @plural_path = "admin_#{@plural}_path"
30
+ @new_path = "new_admin_#{@singular}_path"
31
+ @edit_path = "edit_admin_#{@singular}_path"
32
+ @reordenar_path = "reordenar_admin_#{@plural}_path"
33
+
34
+ @atributos = []
35
+
36
+ args.each do |arg|
37
+ @atributos << Atributo.new(arg)
38
+ end
39
+
40
+ info #TODO: sacar de aca!.
41
+
42
+ end
43
+
44
+ def body
45
+ template 'model.erb' , "app/models/#{@singular}.rb"
46
+ template 'migration.erb' , "db/migrate/#{fecha}_create_#{@plural}.rb"
47
+ template 'controller.erb', "app/controllers/admin/#{@plural}_controller.rb"
48
+ template 'helper.erb', "app/helpers/#{@plural}_helper.rb"
49
+
50
+ %w[_form _mini_form edit index new].each do |action|
51
+ template "views/#{action}.erb", "app/views/admin/#{@plural}/#{action}.html.erb"
52
+ end
53
+ template "views/nombre.erb", "app/views/admin/#{@plural}/_#{@singular}.html.erb"
54
+
55
+ # add_admin_route "\n resources :#{@plural} do\n post :reordenar, :on => :collection\n end"
56
+
57
+ template 'cargar.erb', "lib/tasks/cargar_#{@plural}.rake"
58
+ end
59
+
60
+ private
61
+
62
+ # Agrega Ruta en namespace :admin
63
+ def add_admin_route(ruta)
64
+ inject_into_file 'config/routes.rb', ruta , :after => /namespace :admin do/
65
+ end
66
+
67
+ def self.next_migration_number(dirname) #:nodoc:
68
+ if ActiveRecord::Base.timestamped_migrations
69
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
70
+ else
71
+ "%.3d" % (current_migration_number(dirname) + 1)
72
+ end
73
+ end
74
+
75
+ def fecha
76
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
77
+ end
78
+
79
+ # Este metodo es usado solo para testear.
80
+ # TODO: Eliminar metodo.
81
+ def info
82
+ say "Singular:#{@singular}. Plural:#{@plural}. Class:#{@class}. Classes:#{@classes}"
83
+
84
+ say '--- Opciones ---'
85
+ say "Con Orden" if @con_orden
86
+ say "Con Slug" if @con_slug
87
+ say "Con Archivo" if @con_archivo
88
+
89
+ say '--- Atributos ---'
90
+ @atributos.each do |a|
91
+ say a.show
92
+ end
93
+
94
+ say '--- Paths ---'
95
+ say "singular_path #{@singular_path}"
96
+ say "plural_path #{@plural_path}"
97
+ say "new_path #{@new_path}"
98
+ say "edit_path #{@edit_path}"
99
+ end
100
+
101
+ end
@@ -0,0 +1,8 @@
1
+ <%- atributos = @atributos.collect {|a| ":#{a.nombre} => #{a.ejemplo}" } -%>
2
+ namespace :cargar do
3
+ task :<%= @plural %> => :environment do
4
+ <%- 10.times do-%>
5
+ <%= @class %>.create! <%= atributos.join(', ') %>
6
+ <%- end -%>
7
+ end
8
+ end
@@ -0,0 +1,49 @@
1
+ class Admin::<%= @classes %>Controller < Admin::AdminController
2
+ <%= if @con_orden? "include Sortable" %>
3
+ load_and_authorize_resource
4
+
5
+ def index
6
+ @<%= @singular %> = <%= @class %>.new
7
+ end
8
+
9
+ def new; end
10
+ def edit; end
11
+
12
+ def create
13
+ @<%= @singular %> = <%= @class %>.new params[:<%= @singular %>]
14
+ @<%= @singular %>.save!
15
+
16
+ respond_to do |format|
17
+ format.html { redirect_to <%= @plural_path %>, notice: mensaje }
18
+ format.js { @elemento = @<%= @singular %> }
19
+ end
20
+ end
21
+
22
+ def update
23
+ @<%= @singular %>.update_attributes! params[:<%= @singular %>]
24
+ redirect_to <%= @edit_path %>(@<%= @singular %>), notice: mensaje
25
+ end
26
+
27
+ def destroy
28
+ @id = @<%= @singular %>.id
29
+ @<%= @singular %>.eliminar!
30
+
31
+ respond_to do |format|
32
+ format.html { redirect_to <%= @plural_path %>, notice: mensaje }
33
+ format.js
34
+ end
35
+ end
36
+
37
+ private
38
+
39
+ def mensaje
40
+ flash.now.notice = t("notice.#{action_name}")
41
+ end
42
+
43
+ <%- if @con_orden? -%>
44
+ def sort_coleccion
45
+ <%= @class %>.all
46
+ end
47
+ <%- end -%>
48
+
49
+ end
@@ -0,0 +1,3 @@
1
+ module <%= @classes %>Helper
2
+ # Your code goes here
3
+ end
@@ -0,0 +1,16 @@
1
+ class Create<%= @classes %> < ActiveRecord::Migration
2
+ def change
3
+ create_table :<%= @plural %> do |t|
4
+ <%- @atributos.each do |a| -%>
5
+ t.<%= a.clase %> :<%= a.nombre %> <%= ", :default => #{a.default}" if a.default %>
6
+ <%- end -%>
7
+
8
+ <%- if @con_slug -%>
9
+ t.string :slug
10
+ <%- end -%>
11
+ t.boolean :es_activo, :default => true
12
+
13
+ t.timestamps
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,14 @@
1
+ class <%= @class %> < ActiveRecord::Base
2
+
3
+ <%- if @con_slug? -%>
4
+ extend FriendlyId
5
+ friendly_id :codigo, use: :slugged
6
+ <%- end -%>
7
+
8
+ default_scope where(:es_activo => true)<%= if @con_orden? ".order('#{@plural}.orden')" %>
9
+
10
+ def eliminar!
11
+ update_attributes :es_activo => false
12
+ end
13
+
14
+ end
@@ -0,0 +1,6 @@
1
+ <%%= simple_form_for <%= @singular %>, :url => url, :html => { :class => 'form-vertical' } do |f| %>
2
+ <%- @atributos.each do |a| -%>
3
+ <%%= f.<%= a.clase == :references ? :association : :input %> :<%= a.nombre %> %>
4
+ <%- end -%>
5
+ <div class="controls"><%%= f.button :submit, :name => nil, :disable_with => t("espere") %></div>
6
+ <%%- end -%>
@@ -0,0 +1,4 @@
1
+ <%%= mini_form_for <%= @singular %>, :url => url, :remote => true do |f| %>
2
+ <%%= f.input :<%= @atributos.first.nombre %> %>
3
+ <%%= f.button :submit, :name => nil, :disable_with => t("espere") %>
4
+ <%%- end -%>
@@ -0,0 +1,5 @@
1
+ <div class="page-header">
2
+ <h1>Editar <%= @singular %></h1>
3
+ </div>
4
+ <%%= link_to "Volver", <%= @plural_path %> %>
5
+ <%%= render "form", :<%= @singular %> => @<%= @singular %>, :url => <%= @singular_path %>(@<%= @singular %>)%>
@@ -0,0 +1,19 @@
1
+ <div class="page-header">
2
+ <h1><%= @classes %></h1>
3
+ </div>
4
+
5
+ <nav class="well">
6
+ <button class="btn" data-toggle="collapse" data-target="#mini"><%%= icono('zoom-in') %> Mostrar formulario</button>
7
+ <%%= link_to icono('plus', :blanco)+" Agregar <%= @singular %> ", <%= @new_path %>, :class => 'btn btn-primary' %>
8
+ </nav>
9
+
10
+ <%%= render "mini_form", :<%= @singular %> => @<%= @singular %>, :url => <%= @plural_path %> %>
11
+
12
+ <table id="ordenable" class="table table-striped ordenable">
13
+ <%%= thead(:<%= @atributos.first.nombre %>) %>
14
+ <tbody>
15
+ <%%= render @<%= @plural %> %>
16
+ </tbody>
17
+ </table>
18
+
19
+ <%%= hidden_field_tag :url, <%= @reordenar_path %>, :class => :url %>
@@ -0,0 +1,5 @@
1
+ <div class="page-header">
2
+ <h1>Crear <%= @singular %></h1>
3
+ </div>
4
+ <%%= link_to "Volver", <%= @plural_path %> %>
5
+ <%%= render "form", :<%= @singular %> => @<%= @singular %>, :url => <%= @plural_path %> %>
@@ -0,0 +1,7 @@
1
+ <%%- clase ||= 'elemento' -%>
2
+ <%%= content_tag :tr, :class => clase, :id => "elementos_#{<%= @singular %>.id}" do %>
3
+ <td><%%= <%= @singular %>.<%= @atributos.first.nombre %> %></td>
4
+ <td><%%= content_tag :i, '',:class => "handle icon-move", :title => "Mantenga click y arrastre para reordenar" %></td>
5
+ <td><%%= link_to 'Editar' , <%= @edit_path %>(<%= @singular %>), :class => 'btn btn-mini' if can? :editar, <%= @singular %> %></td>
6
+ <td><%%= link_to "Eliminar", <%= @singular_path %>(<%= @singular %>), :method => :delete, :confirm => t("confirmacion"), :remote => true, :class => 'btn btn-mini btn-danger' %></td>
7
+ <%% end %>
data/lib/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module Scaf
2
2
  module Generator
3
- VERSION = "0.2.3"
3
+ VERSION = "0.2.4"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: scaf-generator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.2.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-28 00:00:00.000000000 Z
12
+ date: 2012-03-07 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Scaffold adaptado a las necesidades de Xaver
15
15
  email:
@@ -22,6 +22,20 @@ files:
22
22
  - Gemfile
23
23
  - README.md
24
24
  - Rakefile
25
+ - lib/generators/prueba/USAGE
26
+ - lib/generators/prueba/atributo.rb
27
+ - lib/generators/prueba/prueba_generator.rb
28
+ - lib/generators/prueba/templates/cargar.erb
29
+ - lib/generators/prueba/templates/controller.erb
30
+ - lib/generators/prueba/templates/helper.erb
31
+ - lib/generators/prueba/templates/migration.erb
32
+ - lib/generators/prueba/templates/model.erb
33
+ - lib/generators/prueba/templates/views/_form.erb
34
+ - lib/generators/prueba/templates/views/_mini_form.erb
35
+ - lib/generators/prueba/templates/views/edit.erb
36
+ - lib/generators/prueba/templates/views/index.erb
37
+ - lib/generators/prueba/templates/views/new.erb
38
+ - lib/generators/prueba/templates/views/nombre.erb
25
39
  - lib/generators/scaf/USAGE
26
40
  - lib/generators/scaf/scaf_generator.rb
27
41
  - lib/generators/scaf/templates/admin_controller.rb