foldscaf 0.0.1

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.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in foldscaf.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,15 @@
1
+ # foldscaf
2
+
3
+ Incluye varios generadores adaptados a las necesidades de Xaver.
4
+ Debe usarse en conjunto con el template de Xaver.
5
+
6
+
7
+ ## Instalación
8
+
9
+ Agrega al Gemfile y ejecuta el comando `bundle` para instalarlo.
10
+
11
+ ```ruby
12
+ gem "foldscaf"
13
+ ```
14
+
15
+ **Requiere Ruby 1.9.2 o superior.**
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/foldscaf.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "foldscaf"
7
+ s.version = Foldscaf::VERSION
8
+ s.authors = ["Nicanor Perera"]
9
+ s.email = ["nicanorperera@gmail.com"]
10
+ s.homepage = "http://github.com/nicanorperera/foldscaf"
11
+ s.summary = %q{Generadores adaptados a las necesidades de Xaver}
12
+ s.description = %q{Generadores adaptados a las necesidades de Xaver}
13
+
14
+ s.rubyforge_project = "foldscaf"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ # specify any dependencies here; for example:
22
+ # s.add_development_dependency "rspec"
23
+ # s.add_runtime_dependency "rest-client"
24
+ end
data/lib/foldscaf.rb ADDED
@@ -0,0 +1,5 @@
1
+ require "version"
2
+
3
+ module Foldscaf
4
+ # Your code goes here...
5
+ end
@@ -0,0 +1,18 @@
1
+ Description:
2
+ Generador Scaffold-Like adaptado a las necesidades de Xaver.
3
+
4
+ Example:
5
+ rails generate fold 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
+ when :boolean then 'true'
50
+ else false
51
+ end
52
+ end
53
+
54
+ # Este metodo es usado solo para testear.
55
+ # TODO: Eliminar metodo.
56
+ def show
57
+
58
+ n1 = ANCHO - @nombre.size
59
+ n2 = ANCHO - @clase.size
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,80 @@
1
+ # -*- encoding : utf-8 -*-
2
+
3
+ require 'generators/fold/atributo'
4
+
5
+ class FoldGenerator < 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
+
14
+ def initialize(*arguments, &block)
15
+ super
16
+
17
+ @con_orden = options.orden?
18
+ @con_slug = options.slug?
19
+
20
+ @singular = @name.downcase
21
+ @plural = @singular.pluralize
22
+ @class = @name.camelize
23
+ @classes = @plural.camelize
24
+
25
+ @singular_path = "admin_#{@singular}_path"
26
+ @plural_path = "admin_#{@plural}_path"
27
+ @new_path = "new_admin_#{@singular}_path"
28
+ @edit_path = "edit_admin_#{@singular}_path"
29
+ @reordenar_path = "reordenar_admin_#{@plural}_path"
30
+
31
+ @atributos = []
32
+
33
+ args.each do |arg|
34
+ @atributos << Atributo.new(arg)
35
+ end
36
+ @identificador = @atributos.first.nombre
37
+
38
+ end
39
+
40
+ def body
41
+ template 'model.erb' , "app/models/#{@singular}.rb"
42
+ template 'migration.erb' , "db/migrate/#{fecha}_create_#{@plural}.rb"
43
+ template 'controller.erb', "app/controllers/admin/#{@plural}_controller.rb"
44
+ template 'helper.erb', "app/helpers/#{@plural}_helper.rb"
45
+
46
+ %w[_form _mini_form edit index new].each do |action|
47
+ template "views/#{action}.erb", "app/views/admin/#{@plural}/#{action}.html.erb"
48
+ end
49
+ template "views/nombre.erb", "app/views/admin/#{@plural}/_#{@singular}.html.erb"
50
+
51
+ resources_route = @con_orden ? "\n resources :#{@plural} do\n post :reordenar, :on => :collection\n end" : "\n resources :#{@plural}"
52
+ add_admin_route resources_route
53
+
54
+ template 'cargar.erb', "lib/tasks/cargar_#{@plural}.rake"
55
+ end
56
+
57
+ private
58
+
59
+ # Agrega Ruta en namespace :admin
60
+ def add_admin_route(ruta)
61
+ inject_into_file 'config/routes.rb', ruta , :after => /namespace :admin do/
62
+ end
63
+
64
+ def self.next_migration_number(dirname) #:nodoc:
65
+ if ActiveRecord::Base.timestamped_migrations
66
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
67
+ else
68
+ "%.3d" % (current_migration_number(dirname) + 1)
69
+ end
70
+ end
71
+
72
+ def fecha
73
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
74
+ end
75
+
76
+ def atributos_con_referencia
77
+ @atributos.select {|a| a.clase == :references}
78
+ end
79
+
80
+ end
@@ -0,0 +1,8 @@
1
+ <%- atributos = @atributos.select {|x| x.ejemplo }.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,52 @@
1
+ class Admin::<%= @classes %>Controller < Admin::AdminController
2
+ <%= "include Sortable" if @con_orden %>
3
+ load_and_authorize_resource
4
+
5
+ def index
6
+ @<%= @singular %> = <%= @class %>.new
7
+ end
8
+
9
+ def new
10
+ end
11
+
12
+ def create
13
+ @<%= @singular %> = <%= @class %>.new params[:<%= @singular %>]
14
+ @<%= @singular %>.save!
15
+ notice = mensaje
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 edit
23
+ end
24
+
25
+ def update
26
+ @<%= @singular %>.update_attributes! params[:<%= @singular %>]
27
+ redirect_to <%= @edit_path %>(@<%= @singular %>), notice: mensaje
28
+ end
29
+
30
+ def destroy
31
+ @id = @<%= @singular %>.id
32
+ @<%= @singular %>.eliminar!
33
+ notice = mensaje
34
+ respond_to do |format|
35
+ format.html { redirect_to <%= @plural_path %>, notice: mensaje }
36
+ format.js
37
+ end
38
+ end
39
+
40
+ private
41
+
42
+ def mensaje
43
+ flash.now.notice = t("notice.#{action_name}")
44
+ end
45
+
46
+ <%- if @con_orden -%>
47
+ def sort_coleccion
48
+ <%= @class %>.all
49
+ end
50
+ <%- end -%>
51
+
52
+ end
@@ -0,0 +1,3 @@
1
+ module <%= @classes %>Helper
2
+ # Your code goes here
3
+ end
@@ -0,0 +1,19 @@
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 && a.ejemplo) %>
6
+ <%- end -%>
7
+
8
+ <%- if @con_slug -%>
9
+ t.string :slug
10
+ <%- end -%>
11
+ <%- if @con_orden -%>
12
+ t.integer :orden
13
+ <%- end -%>
14
+ t.boolean :es_activo, :default => true
15
+
16
+ t.timestamps
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,26 @@
1
+ class <%= @class %> < ActiveRecord::Base
2
+ <%= 'before_create :set_orden' if @con_orden %>
3
+ <%- if @con_slug -%>
4
+ extend FriendlyId
5
+ friendly_id :<%= @identificador %>, use: :slugged
6
+ <%- end -%>
7
+
8
+ <%- atributos_con_referencia.each do |a| -%>
9
+ belongs_to :<%= a.nombre %>
10
+ <%- end -%>
11
+
12
+ default_scope where(:es_activo => true)<%= ".order('#{@plural}.orden')" if @con_orden %>
13
+
14
+ def eliminar!
15
+ update_attributes :es_activo => false
16
+ end
17
+
18
+ alias_attribute :to_s, :<%= @identificador %>
19
+
20
+ private
21
+ <%- if @con_orden -%>
22
+ def set_orden
23
+ self.orden = self.id
24
+ end
25
+ <%- end -%>
26
+ 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 :<%= @identificador %>, :placeholder => '<%= @identificador %>' %>
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(:<%= @identificador %>) %>
14
+ <tbody>
15
+ <%%= render @<%= @plural %> %>
16
+ </tbody>
17
+ </table>
18
+
19
+ <%= "<%= hidden_field_tag :url, #{@reordenar_path} , :class => :url %\>" if @con_orden %>
@@ -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 %>.<%= @identificador %> %></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 %>
@@ -0,0 +1,8 @@
1
+ Description:
2
+ Instala los componentes necesarios para que funcione mini_form.
3
+
4
+ Example:
5
+ rails generate mini_form
6
+
7
+ This will create:
8
+ lib/mini_form.rb
@@ -0,0 +1,11 @@
1
+ # -*- encoding : utf-8 -*-
2
+ class MiniFormGenerator < Rails::Generators::Base
3
+ source_root File.expand_path('../templates', __FILE__)
4
+
5
+ def initialize(*arguments, &block)
6
+ super
7
+ end
8
+ def body
9
+ copy_file 'mini_form.rb', 'lib/mini_form.rb'
10
+ end
11
+ end
@@ -0,0 +1,25 @@
1
+ module MiniForm
2
+ def mini_form_for(object, *args, &block)
3
+ args ||= []
4
+ options = args.extract_options!.merge!({:wrapper => :mini, :html => {:id => :mini, :class => 'collapse form-inline'}})
5
+ simple_form_for(object, *(args << options.merge(:builder => MiniFormBuilder)), &block)
6
+ end
7
+
8
+ class MiniFormBuilder < SimpleForm::FormBuilder
9
+ def input(attribute_name, options = {}, &block)
10
+ options[:input_html] = { :class => 'input-medium'}
11
+ super
12
+ end
13
+ end
14
+
15
+ # Wrapper para Mini Formularios
16
+ # TODO: encontrar la manera de cambiar la clase de todos los input por 'input-medium'
17
+ SimpleForm.setup do |config|
18
+ config.wrappers :mini, :tag => 'span' , :class => 'controls', :error_class => 'error' do |b|
19
+ b.use :html5
20
+ b.use :placeholder
21
+ b.use :input
22
+ b.use :error, :wrap_with => { :tag => 'span', :class => 'help-inline' }
23
+ end
24
+ end
25
+ end
data/lib/version.rb ADDED
@@ -0,0 +1,3 @@
1
+ module Foldscaf
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: foldscaf
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Nicanor Perera
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-03-09 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Generadores adaptados a las necesidades de Xaver
15
+ email:
16
+ - nicanorperera@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - README.md
24
+ - Rakefile
25
+ - foldscaf.gemspec
26
+ - lib/foldscaf.rb
27
+ - lib/generators/fold/USAGE
28
+ - lib/generators/fold/atributo.rb
29
+ - lib/generators/fold/fold_generator.rb
30
+ - lib/generators/fold/templates/cargar.erb
31
+ - lib/generators/fold/templates/controller.erb
32
+ - lib/generators/fold/templates/helper.erb
33
+ - lib/generators/fold/templates/migration.erb
34
+ - lib/generators/fold/templates/model.erb
35
+ - lib/generators/fold/templates/views/_form.erb
36
+ - lib/generators/fold/templates/views/_mini_form.erb
37
+ - lib/generators/fold/templates/views/edit.erb
38
+ - lib/generators/fold/templates/views/index.erb
39
+ - lib/generators/fold/templates/views/new.erb
40
+ - lib/generators/fold/templates/views/nombre.erb
41
+ - lib/generators/mini_form/USAGE
42
+ - lib/generators/mini_form/mini_form_generator.rb
43
+ - lib/generators/mini_form/templates/mini_form.rb
44
+ - lib/version.rb
45
+ homepage: http://github.com/nicanorperera/foldscaf
46
+ licenses: []
47
+ post_install_message:
48
+ rdoc_options: []
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ! '>='
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ requirements: []
64
+ rubyforge_project: foldscaf
65
+ rubygems_version: 1.8.15
66
+ signing_key:
67
+ specification_version: 3
68
+ summary: Generadores adaptados a las necesidades de Xaver
69
+ test_files: []