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

Bug Fix #28

Open
wants to merge 7 commits 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
7 changes: 6 additions & 1 deletion README.md
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ If the controller has a name different to the model used, specify the controller


rails g web_app_theme:themed admin/items post rails g web_app_theme:themed admin/items post


If the model is insite a module use the second parameter to specifie the module:

rails g wet_app_theme:themed admin/items admin::item

If you use `will_paginate` for pagination use the `--will-paginate`: If you use `will_paginate` for pagination use the `--will-paginate`:


rails g web_app_theme:themed items post --will-paginate rails g web_app_theme:themed items post --will-paginate
Expand Down Expand Up @@ -151,9 +155,10 @@ Contributors
* Bryan Woods * Bryan Woods
* Sandro Duarte * Sandro Duarte
* David Francisco * David Francisco
* Lucas Rosa Galego


Credits Credits
------- -------


* Icons: FAMFAMFAM Silk icons <http://www.famfamfam.com/lab/icons/silk/> * Icons: FAMFAMFAM Silk icons <http://www.famfamfam.com/lab/icons/silk/>
* Buttons: Particletree - Rediscovering the Button Element <http://particletree.com/features/rediscovering-the-button-element/> * Buttons: Particletree - Rediscovering the Button Element <http://particletree.com/features/rediscovering-the-button-element/>
12 changes: 8 additions & 4 deletions lib/generators/web_app_theme/themed/themed_generator.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def initialize_views_variables
@base_name, @controller_class_path, @controller_file_path, @controller_class_nesting, @controller_class_nesting_depth = extract_modules(controller_path) @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(/\//, '_') @controller_routing_path = @controller_file_path.gsub(/\//, '_')
@model_name = @base_name.singularize unless @model_name @model_name = @base_name.singularize unless @model_name
@model_name = @model_name.camelize @model_name = @model_name.split(/(::)/).map{|x| x.camelize}.join
end end


def controller_routing_path def controller_routing_path
Expand All @@ -59,7 +59,7 @@ def model_name
end end


def plural_model_name def plural_model_name
@model_name.pluralize extract_modules(@model_name)[0].pluralize
end end


def resource_name def resource_name
Expand All @@ -77,12 +77,16 @@ def plural_resource_name
def columns def columns
begin begin
excluded_column_names = %w[id created_at updated_at] excluded_column_names = %w[id created_at updated_at]
Kernel.const_get(@model_name).columns.reject{|c| excluded_column_names.include?(c.name) }.collect{|c| Rails::Generators::GeneratedAttribute.new(c.name, c.type)} model_class.columns.reject{|c| excluded_column_names.include?(c.name) }.collect{|c| Rails::Generators::GeneratedAttribute.new(c.name, c.type)}
rescue NoMethodError rescue NoMethodError
Kernel.const_get(@model_name).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)} model_class.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)}
end end
end end


def model_class
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BugFix now the code iterates over the namespace to get the model class

@model_name.split('::').inject(Kernel){|model_class, namespace| model_class.const_get(namespace.camelize)}
end

def extract_modules(name) def extract_modules(name)
modules = name.include?('/') ? name.split('/') : name.split('::') modules = name.include?('/') ? name.split('/') : name.split('::')
name = modules.pop name = modules.pop
Expand Down
5 changes: 5 additions & 0 deletions spec/spec_helper.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,5 @@
require "rubygems"
require "rspec"
require "rails"
require "rails/generators"
require File.dirname(__FILE__) + "/../lib/generators/web_app_theme/themed/themed_generator"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed the spec requires

170 changes: 170 additions & 0 deletions spec/themed_generator_spec.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,170 @@
require File.dirname(__FILE__) + "/spec_helper"

describe WebAppTheme::ThemedGenerator, "with 'script/generate themed posts'" do
before do
Post = Class.new
Post.stub!(:columns).and_return([])
options = {:destination => File.dirname(__FILE__), :quiet => true, :source => File.dirname(__FILE__)}
@generator = WebAppTheme::ThemedGenerator.new(["posts", "post"], options)
end

after do
Object::send(:remove_const, :Post)
end

it "should set the right controller_routing_path" do
@generator.instance_variable_get("@controller_routing_path").should == "posts"
end

it "should set the right singular_controller_routing_path" do
@generator.send(:singular_controller_routing_path).should == "post"
end

it "should set the right model_name" do
@generator.instance_variable_get("@model_name").should == "Post"
end

it "should set the right plural_model_name" do
@generator.send(:plural_model_name).should == "Posts"
end

it "should set the right resource_name" do
@generator.send(:resource_name).should == "post"
end

it "should set the right plural_resource_name" do
@generator.send(:plural_resource_name).should == "posts"
end

it "should get the right model class" do
@generator.send(:model_class).to_s.should == "Post"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a new spec to method model_class

end

end

describe WebAppTheme::ThemedGenerator, "with 'script/generate themed admin/gallery_items'" do
before do
GalleryItem = Class.new
GalleryItem.stub!(:columns).and_return([])
options = {:destination => File.dirname(__FILE__), :quiet => true, :source => File.dirname(__FILE__)}
@generator = WebAppTheme::ThemedGenerator.new(["admin/gallery_items"], options)
end

after do
Object::send(:remove_const, :GalleryItem)
end

it "should set the right controller_routing_path" do
@generator.instance_variable_get("@controller_routing_path").should == "admin_gallery_items"
end

it "should set the right singular_controller_routing_path" do
@generator.send(:singular_controller_routing_path).should == "admin_gallery_item"
end

it "should set the right model_name" do
@generator.instance_variable_get("@model_name").should == "GalleryItem"
end

it "should set the right plural_model_name" do
@generator.send(:plural_model_name).should == "GalleryItems"
end

it "should set the right resource_name" do
@generator.send(:resource_name).should == "gallery_item"
end

it "should set the right plural_resource_name" do
@generator.send(:plural_resource_name).should == "gallery_items"
end

it "should get the right model class" do
@generator.send(:model_class).to_s.should == "GalleryItem"
end

end

describe WebAppTheme::ThemedGenerator, "with 'script/generate themed admin/gallery_items pictures'" do
before do
Picture = Class.new
Picture.stub!(:columns).and_return([])
options = {:destination => File.dirname(__FILE__), :quiet => true, :source => File.dirname(__FILE__)}
@generator = WebAppTheme::ThemedGenerator.new(["admin/gallery_items", "picture"], options)
end

after do
Object::send(:remove_const, :Picture)
end

it "should set the right controller_routing_path" do
@generator.instance_variable_get("@controller_routing_path").should == "admin_gallery_items"
end

it "should set the right singular_controller_routing_path" do
@generator.send(:singular_controller_routing_path).should == "admin_gallery_item"
end

it "should set the right model_name" do
@generator.instance_variable_get("@model_name").should == "Picture"
end

it "should set the right plural_model_name" do
@generator.send(:plural_model_name).should == "Pictures"
end

it "should set the right resource_name" do
@generator.send(:resource_name).should == "picture"
end

it "should set the right plural_resource_name" do
@generator.send(:plural_resource_name).should == "pictures"
end

it "should get the right model class" do
@generator.send(:model_class).to_s.should == "Picture"
end

end

describe WebAppTheme::ThemedGenerator, "width 'script/generate themed admin/gallery Admin::Gallery'" do
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a new case os specification receving the model with module

before do
Admin = Module.new
Admin::Gallery = Class.new
Admin::Gallery.stub!(:columns).and_return([])
options = {:destination => File.dirname(__FILE__), :quiet => true, :source => File.dirname(__FILE__)}
@generator = WebAppTheme::ThemedGenerator.new(["admin/gallery", "admin::gallery"], options)
end

after do
Object::send(:remove_const, :Admin)
end

it "should set the right controller_routing_path" do
@generator.instance_variable_get("@controller_routing_path").should == "admin_gallery"
end

it "should set the right singular_controller_routing_path" do
@generator.send(:singular_controller_routing_path).should == "admin_gallery"
end

it "should set the right model_name" do
@generator.instance_variable_get("@model_name").should == "Admin::Gallery"
end

it "should set the right plural_model_name" do
@generator.send(:plural_model_name).should == "Galleries"
end

it "should set the right resource_name" do
@generator.send(:resource_name).should == "admin/gallery"
end

it "should set the right plural_resource_name" do
@generator.send(:plural_resource_name).should == "admin/galleries"
end

it "should get the right model class" do
@generator.send(:model_class).to_s.should == "Admin::Gallery"
end

end
5 changes: 0 additions & 5 deletions test/spec_helper.rb

This file was deleted.

115 changes: 0 additions & 115 deletions test/themed_generator_spec.rb

This file was deleted.

8 changes: 4 additions & 4 deletions web-app-theme.gemspec
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -71,17 +71,17 @@ Gem::Specification.new do |s|
"stylesheets/themes/red/style.css", "stylesheets/themes/red/style.css",
"stylesheets/themes/reidb-greenish/style.css", "stylesheets/themes/reidb-greenish/style.css",
"stylesheets/themes/warehouse/style.css", "stylesheets/themes/warehouse/style.css",
"test/spec_helper.rb", "spec/spec_helper.rb",
"test/themed_generator_spec.rb", "spec/themed_generator_spec.rb",
"web-app-theme.gemspec" "web-app-theme.gemspec"
] ]
s.homepage = %q{http://github.com/pilu/web-app-theme} s.homepage = %q{http://github.com/pilu/web-app-theme}
s.require_paths = ["lib"] s.require_paths = ["lib"]
s.rubygems_version = %q{1.3.7} s.rubygems_version = %q{1.3.7}
s.summary = %q{Web app theme generator} s.summary = %q{Web app theme generator}
s.test_files = [ s.test_files = [
"test/spec_helper.rb", "spec/spec_helper.rb",
"test/themed_generator_spec.rb" "spec/themed_generator_spec.rb"
] ]


if s.respond_to? :specification_version then if s.respond_to? :specification_version then
Expand Down