Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hi! I cleaned up your code for you! #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Compiled source #
###################
*.com
*.class
*.dll
*.exe
*.o
*.so
*.pyc

# Logs and databases #
######################
*.log

# OS generated files #
######################
.DS_Store*
ehthumbs.db
Icon?
Thumbs.db
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
The MIT License

Copyright (c) 2010 Michael Harrison mh@michaelharrison.ws
Paul Barry
Paul Barry
Martin Gamsjaeger

Permission is hereby granted, free of charge, to any person obtaining a copy
Expand Down
18 changes: 9 additions & 9 deletions lib/generators/mustache/controller/controller_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,34 @@ class Mustache
module Generators
class ControllerGenerator < ::Rails::Generators::NamedBase
extend TemplatePath

argument :actions, :type => :array, :default => [], :banner => "action action"

def create_view_files
model_path = File.join(class_path, file_name)

base_mustache_view_path = File.join("app/views", model_path)
empty_directory base_mustache_view_path

base_mustache_template_path = File.join("app/templates", model_path)
empty_directory base_mustache_template_path

actions.each do |action|
@action = action
mustache_view_path = File.join(base_mustache_view_path,
"#{action}.rb")
mustache_template_path = File.join(base_mustache_template_path,
"#{action}.html.mustache")

template "view.rb.erb", mustache_view_path
template "view.html.mustache.erb", mustache_template_path
template "view.html.mustache.erb", mustache_template_path
end
end

protected

# Methods not to be executed go here

end
end
end
2 changes: 1 addition & 1 deletion lib/generators/mustache/controller/templates/view.rb.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
class <%= singular_name.camelize %>::<%= @action.camelize %> < Mustache::Rails

end
Original file line number Diff line number Diff line change
Expand Up @@ -77,23 +77,23 @@ class TemplateHandler < ActionView::Template::Handler
def compile(template)
mustache_class = mustache_class_from_template(template)
mustache_class.template_file = mustache_template_file(template)

<<-MUSTACHE
mustache = ::#{mustache_class}.new
mustache.view = self
mustache[:yield] = content_for(:layout)
mustache.context.update(local_assigns)
variables = controller.instance_variable_names
variables -= %w[@template]

if controller.respond_to?(:protected_instance_variables)
variables -= controller.protected_instance_variables
end

variables.each do |name|
mustache.instance_variable_set(name, controller.instance_variable_get(name))
end

# Declaring an +attr_reader+ for each instance variable in the
# Mustache::Rails subclass makes them available to your templates.
mustache.class.class_eval do
Expand Down
20 changes: 10 additions & 10 deletions lib/generators/mustache/scaffold/scaffold_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,22 @@ def copy_view_files
views = available_views
views.delete("index") if options[:singleton]

views.each do |view|
template "#{view}.rb.erb",
views.each do |view|
template "#{view}.rb.erb",
File.join("app/views", controller_file_path, "#{view}.rb")
template "#{view}.html.mustache.erb",
File.join("app/templates",
controller_file_path,
template "#{view}.html.mustache.erb",
File.join("app/templates",
controller_file_path,
"#{view}.html.mustache")
end
template "_form.html.mustache.erb",
File.join("app/templates",
controller_file_path,
template "_form.html.mustache.erb",
File.join("app/templates",
controller_file_path,
"_form.html.mustache")
end

private

def available_views
%w(index edit show new)
end
Expand Down
22 changes: 11 additions & 11 deletions lib/generators/mustache/scaffold/templates/edit.rb.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,24 @@ class <%= plural_name.camelize %>::Edit < Mustache::Rails
def errors_display_div
return "" unless <%= singular_name %>.errors.any?
content_tag("div", :id=>"errorExplanation", :class=>"errorExplanation") do
content_tag("h2", error_header) + content_tag("ul") do
content_tag("h2", error_header) + content_tag("ul") do
<%= singular_name %>.errors.full_messages.inject("") do |memo,msg|
memo += content_tag("li", msg)
end
end
end
end

def <%= singular_name %>_form_tag
form_tag(update_path, :class => "<%= singular_name %>_form", :method => :put, :id => "edit_<%= singular_name %>_#{<%= singular_name %>.id}_form")
end

<% for attribute in attributes -%>
def <%= attribute.name %>_label
label :<%= singular_name %>, :<%= attribute.name %>
end
<%# TODO: Different fields for different attribute types -%>

def <%= attribute.name %>_text_field
text_field(:<%= singular_name %>, :<%= attribute.name %>, :id => "<%= attribute.name %>_text_field")
end
Expand All @@ -31,23 +31,23 @@ def <%= attribute.name %>_text_field
def form_submit
submit_tag "Update"
end

def show_path
<%= singular_name %>_path(<%= singular_name %>)
end
end

def index_path
<%= plural_name %>_path
end

private

def update_path
<%= singular_name %>_path(<%= singular_name %>)
end

def error_header
"u r dong it rong"
end

end
4 changes: 2 additions & 2 deletions lib/generators/mustache/scaffold/templates/index.rb.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class <%= plural_name.camelize %>::Index < Mustache::Rails
def new_path
new_<%= singular_name %>_path()
end

def listing
<%= plural_name %>.collect do |record|
{
Expand All @@ -14,5 +14,5 @@ class <%= plural_name.camelize %>::Index < Mustache::Rails
}
end
end

end
18 changes: 9 additions & 9 deletions lib/generators/mustache/scaffold/templates/new.rb.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,24 @@ class <%= plural_name.camelize %>::New < Mustache::Rails
def errors_display_div
return "" unless <%= singular_name %>.errors.any?
content_tag("div", :id=>"errorExplanation", :class=>"errorExplanation") do
content_tag("h2", error_header) + content_tag("ul") do
content_tag("h2", error_header) + content_tag("ul") do
<%= singular_name %>.errors.full_messages.inject("") do |memo,msg|
memo += content_tag("li", msg)
end
end
end
end

def <%= singular_name %>_form_tag
form_tag(create_path, :class => "<%= singular_name %>_form", :id => "edit_<%= singular_name %>_#{<%= singular_name %>.id}_form")
end

<% for attribute in attributes -%>
def <%= attribute.name %>_label
label :<%= singular_name %>, :<%= attribute.name %>
end
<%# TODO: Different fields for different attribute types -%>

def <%= attribute.name %>_text_field
text_field(:<%= singular_name %>, :<%= attribute.name %>, :id => "<%= attribute.name %>_text_field")
end
Expand All @@ -32,19 +32,19 @@ def <%= attribute.name %>_text_field
submit_tag "Create"
end


def index_path
<%= plural_name %>_path
end

private

def create_path
<%= plural_name %>_path
end

def error_header
"u r dong it rong"
end

end
8 changes: 4 additions & 4 deletions lib/generators/mustache/scaffold/templates/show.rb.erb
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
class <%= plural_name.camelize %>::Show < Mustache::Rails

<% for attribute in attributes -%>
def <%= attribute.name %>
<%= singular_name %>.<%= attribute.name %>
end

<% end -%>

def edit_path
edit_<%= singular_name %>_path(@<%= singular_name %>)
end

def index_path
<%= plural_name %>_path
end

end
8 changes: 4 additions & 4 deletions lib/mustache_rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,23 +77,23 @@ class TemplateHandler < ActionView::Template::Handler
def compile(template)
mustache_class = mustache_class_from_template(template)
mustache_class.template_file = mustache_template_file(template)

<<-MUSTACHE
mustache = ::#{mustache_class}.new
mustache.view = self
mustache[:yield] = content_for(:layout)
mustache.context.update(local_assigns)
variables = controller.instance_variable_names
variables -= %w[@template]

if controller.respond_to?(:protected_instance_variables)
variables -= controller.protected_instance_variables
end

variables.each do |name|
mustache.instance_variable_set(name, controller.instance_variable_get(name))
end

# Declaring an +attr_reader+ for each instance variable in the
# Mustache::Rails subclass makes them available to your templates.
mustache.class.class_eval do
Expand Down