initial commit
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
= form_for @<%= resource_name %>, html: {class: 'form-horizontal' } do |f|
|
||||
= render 'error_messages', target: @<%= resource_name %>
|
||||
<%- columns.each do |column| -%>
|
||||
.control-group class=(@<%= resource_name %>.errors[:<%= column.name %>].any? ? 'error' : nil)
|
||||
= f.label :<%= column.name %>, class: 'control-label'
|
||||
.controls
|
||||
= f.<%= column.field_type %> :<%= column.name %>, class: '<%= column.field_type %>'
|
||||
<%- end -%>
|
||||
<%- belongs_to_associations.each do |association| -%>
|
||||
.control-group class=(@<%= resource_name %>.errors[:<%= association.name %>_id].any? ? 'error' : nil)
|
||||
= f.label :<%= association.name %>_id, <%= association.options[:class_name] %>.model_name.human, class: 'control-label'
|
||||
.controls
|
||||
= f.select :<%= association.name %>_id, options_for_select(@<%= association.name.to_s.pluralize %>.map{|a| [a.<%= association.name_property%>, a.id]}), include_blank: <%= model_name.constantize.validators_on(:"#{association.name}_id").any? ? 'nil' : "''" %>
|
||||
<%- end -%>
|
||||
.form-actions
|
||||
= f.submit nil, class: 'btn btn-primary'
|
||||
'
|
||||
= link_to t("helpers.links.cancel"), <%= controller_routing_path %>_path, class: 'btn'
|
||||
@@ -0,0 +1,4 @@
|
||||
- model_class = <%= resource_name.classify %>
|
||||
.page-header
|
||||
= title :edit, model_class
|
||||
= render 'form'
|
||||
@@ -0,0 +1,35 @@
|
||||
- model_class = <%= resource_name.classify %>
|
||||
div.page-header= title :index, model_class
|
||||
- if @<%= plural_resource_name %>.any?
|
||||
table.table.table-striped
|
||||
thead
|
||||
tr
|
||||
<%- columns.each do |column| -%>
|
||||
th= model_class.human_attribute_name(:<%= column.name %>)
|
||||
<%- end -%>
|
||||
<%- belongs_to_associations.each do |association| -%>
|
||||
th= <%= association.options[:class_name] %>.model_name.human
|
||||
<%- end -%>
|
||||
th= model_class.human_attribute_name(:created_at)
|
||||
th=t 'helpers.actions'
|
||||
tbody
|
||||
- @<%= plural_resource_name %>.each do |<%= resource_name %>|
|
||||
tr
|
||||
<%- columns.each.with_index do |column, index| -%>
|
||||
<%- if index.zero? -%>
|
||||
td= link_to <%= resource_name %>.<%= column.name %>, <%= resource_name %>
|
||||
<%- else -%>
|
||||
td= <%= resource_name %>.<%= column.name %>
|
||||
<%- end end -%>
|
||||
<%- belongs_to_associations.each do |association| -%>
|
||||
td= link_to <%= resource_name %>.<%= association.name %>.<%= association.name_property %>, <%= resource_name %>.<%= association.name %>
|
||||
<%- end -%>
|
||||
td=l <%= resource_name %>.created_at, format: :short
|
||||
td
|
||||
= link_to t('helpers.links.edit'), [:edit, <%= resource_name %>], class: 'btn btn-mini'
|
||||
'
|
||||
= link_to t("helpers.links.destroy"), <%= resource_name %>, method: :delete, data: {confirm: are_you_sure? }, class: 'btn btn-mini btn-danger'
|
||||
- else
|
||||
= no_content_given model_class
|
||||
= link_to t("helpers.links.new"), new_<%= singular_controller_routing_path %>_path, class: 'btn btn-primary'
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
- model_class = <%= resource_name.classify %>
|
||||
.page-header
|
||||
= title :new, model_class
|
||||
= render 'form'
|
||||
@@ -0,0 +1,20 @@
|
||||
- model_class = <%= resource_name.classify %>
|
||||
.page-header= title :show, @<%= resource_name %>
|
||||
|
||||
dl.dl-horizontal.show-list
|
||||
<%- columns.each do |column| -%>
|
||||
dt= model_class.human_attribute_name(:<%= column.name %>)
|
||||
dd= @<%= resource_name %>.<%= column.name %>
|
||||
<%- end -%>
|
||||
<%- belongs_to_associations.each do |association| -%>
|
||||
- if @<%= resource_name %>.<%= association.name %>.present?
|
||||
dt= <%= association.options[:class_name] %>.model_name.human
|
||||
dd= link_to @<%= resource_name %>.<%= association.name %>.<%= association.name_property %>, @<%= resource_name %>.<%= association.name %>
|
||||
<%- end -%>
|
||||
|
||||
.form-actions
|
||||
= link_to t("helpers.links.back"), <%= controller_routing_path %>_path, class: 'btn'
|
||||
'
|
||||
= link_to t('helpers.links.edit'), [:edit, @<%= resource_name %>], class: 'btn'
|
||||
'
|
||||
= link_to t("helpers.links.destroy"), @<%= resource_name %>, method: :delete, data: {confirm: are_you_sure? }, class: 'btn btn-danger'
|
||||
@@ -0,0 +1,119 @@
|
||||
require 'rails/generators'
|
||||
require 'rails/generators/generated_attribute'
|
||||
|
||||
module Bootstrap
|
||||
module Generators
|
||||
class ThemedGenerator < ::Rails::Generators::Base
|
||||
NAME_COLUMNS = %w[name title number].map(&:to_sym)
|
||||
source_root File.expand_path('../templates', __FILE__)
|
||||
argument :controller_path, :type => :string
|
||||
argument :model_name, :type => :string, :required => false
|
||||
argument :layout, :type => :string, :default => "application",
|
||||
:banner => "Specify application layout"
|
||||
|
||||
def initialize(args, *options)
|
||||
super(args, *options)
|
||||
initialize_views_variables
|
||||
end
|
||||
|
||||
def copy_views
|
||||
generate_views
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def initialize_views_variables
|
||||
@base_name, @controller_class_path, @controller_file_path, @controller_class_nesting, @controller_class_nesting_depth = extract_modules(controller_path)
|
||||
@controller_routing_path = @controller_file_path.gsub(/\//, '_')
|
||||
@model_name = @controller_class_nesting + "::#{@base_name.singularize.camelize}" unless @model_name
|
||||
@model_name = @model_name.camelize
|
||||
end
|
||||
|
||||
def controller_routing_path
|
||||
@controller_routing_path
|
||||
end
|
||||
|
||||
def singular_controller_routing_path
|
||||
@controller_routing_path.singularize
|
||||
end
|
||||
|
||||
def model_name
|
||||
@model_name
|
||||
end
|
||||
|
||||
def plural_model_name
|
||||
@model_name.pluralize
|
||||
end
|
||||
|
||||
def resource_name
|
||||
@model_name.demodulize.underscore
|
||||
end
|
||||
|
||||
def plural_resource_name
|
||||
resource_name.pluralize
|
||||
end
|
||||
|
||||
def name_columns
|
||||
NAME_COLUMNS
|
||||
end
|
||||
|
||||
def columns
|
||||
begin
|
||||
excluded_column_names = %w[id created_at updated_at]
|
||||
@model_name.constantize.columns.reject{|c| excluded_column_names.include?(c.name) }.collect{|c| ::Rails::Generators::GeneratedAttribute.new(c.name, c.type)}
|
||||
rescue NoMethodError
|
||||
begin
|
||||
@model_name.constantize.fields.collect{|c| c[1]}.reject{|c| excluded_column_names.include?(c.name) }.collect{|c| ::Rails::Generators::GeneratedAttribute.new(c.name, c.type.to_s)}
|
||||
rescue NoMethodError
|
||||
# SimplyStored/CouchPotato
|
||||
@model_name.constantize.properties.reject{|p| !(p.is_a?(CouchPotato::Persistence::SimpleProperty)) || p.name =~ /_ids?$/ || excluded_column_names.include?(p.name.to_s) }.collect{|c| ::Rails::Generators::GeneratedAttribute.new(c.name, c.type.to_s)}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def belongs_to_associations
|
||||
#@model_name.constantize.properties.select{|p| p.is_a?(SimplyStored::Couch::BelongsTo::Property)}
|
||||
associations= @model_name.constantize.properties.select{|p| p.is_a?(SimplyStored::Couch::BelongsTo::Property) && (p.options[:class_name].constantize.property_names & name_columns).any?}
|
||||
for association in associations
|
||||
def association.name_property
|
||||
(options[:class_name].constantize.property_names & Bootstrap::Generators::ThemedGenerator::NAME_COLUMNS).first || :name
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def extract_modules(name)
|
||||
modules = name.include?('/') ? name.split('/') : name.split('::')
|
||||
name = modules.pop
|
||||
path = modules.map { |m| m.underscore }
|
||||
file_path = (path + [name.underscore]).join('/')
|
||||
nesting = modules.map { |m| m.camelize }.join('::')
|
||||
[name, path, file_path, nesting, modules.size]
|
||||
end
|
||||
|
||||
def generate_views
|
||||
views = {
|
||||
"index.html.#{ext}" => File.join('app/views', @controller_file_path, "index.html.#{ext}"),
|
||||
"new.html.#{ext}" => File.join('app/views', @controller_file_path, "new.html.#{ext}"),
|
||||
"edit.html.#{ext}" => File.join('app/views', @controller_file_path, "edit.html.#{ext}"),
|
||||
"#{form_builder}_form.html.#{ext}" => File.join('app/views', @controller_file_path, "_form.html.#{ext}"),
|
||||
"show.html.#{ext}" => File.join('app/views', @controller_file_path, "show.html.#{ext}")}
|
||||
selected_views = views
|
||||
options.engine == generate_erb(selected_views)
|
||||
end
|
||||
|
||||
def generate_erb(views)
|
||||
views.each do |template_name, output_path|
|
||||
template template_name, output_path
|
||||
end
|
||||
end
|
||||
|
||||
def ext
|
||||
::Rails.application.config.generators.options[:rails][:template_engine] || :erb
|
||||
end
|
||||
|
||||
def form_builder
|
||||
defined?(::SimpleForm) ? 'simple_form/' : ''
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user