Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Bug Fix #28

Open
wants to merge 7 commits into from

2 participants

@lrgalego

Hi!
I fixed the bug report related here: https://github.com/pilu/web-app-theme/issues#issue/23
The problem occurs because when the user has a model that is inside a module the Kernel.const_get couldn't find the class.
I fixed it and added some new specs to deal with it.

Cheers.

Lucas Galego

@lrgalego lrgalego commented on the diff
spec/spec_helper.rb
@@ -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"

Fixed the spec requires

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@lrgalego lrgalego commented on the diff
spec/themed_generator_spec.rb
((25 lines not shown))
+ 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"

Added a new spec to method model_class

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@lrgalego lrgalego commented on the diff
spec/themed_generator_spec.rb
((114 lines not shown))
+
+ 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

Added a new case os specification receving the model with module

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@lrgalego lrgalego commented on the diff
lib/generators/web_app_theme/themed/themed_generator.rb
((9 lines not shown))
end
end
+ def model_class

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
7 README.md
@@ -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
+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`:
rails g web_app_theme:themed items post --will-paginate
@@ -151,9 +155,10 @@ Contributors
* Bryan Woods
* Sandro Duarte
* David Francisco
+* Lucas Rosa Galego
Credits
-------
* 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/>
View
12 lib/generators/web_app_theme/themed/themed_generator.rb
@@ -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)
@controller_routing_path = @controller_file_path.gsub(/\//, '_')
@model_name = @base_name.singularize unless @model_name
- @model_name = @model_name.camelize
+ @model_name = @model_name.split(/(::)/).map{|x| x.camelize}.join
end
def controller_routing_path
@@ -59,7 +59,7 @@ def model_name
end
def plural_model_name
- @model_name.pluralize
+ extract_modules(@model_name)[0].pluralize
end
def resource_name
@@ -77,12 +77,16 @@ def plural_resource_name
def columns
begin
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
- 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
+ def model_class

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ @model_name.split('::').inject(Kernel){|model_class, namespace| model_class.const_get(namespace.camelize)}
+ end
+
def extract_modules(name)
modules = name.include?('/') ? name.split('/') : name.split('::')
name = modules.pop
View
5 spec/spec_helper.rb
@@ -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"

Fixed the spec requires

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
View
170 spec/themed_generator_spec.rb
@@ -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"

Added a new spec to method model_class

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ 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

Added a new case os specification receving the model with module

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ 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
View
5 test/spec_helper.rb
@@ -1,5 +0,0 @@
-require "rubygems"
-require "spec"
-require "rails_generator"
-require "rails_generator/scripts/generate"
-require File.dirname(__FILE__) + "/../rails_generators/themed/themed_generator"
View
115 test/themed_generator_spec.rb
@@ -1,115 +0,0 @@
-require File.dirname(__FILE__) + "/spec_helper"
-
-describe 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 = ThemedGenerator.new(["posts", "post"], options)
- @generator.manifest
- 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.instance_variable_get("@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.instance_variable_get("@plural_model_name").should == "Posts"
- end
-
- it "should set the right resource_name" do
- @generator.instance_variable_get("@resource_name").should == "post"
- end
-
- it "should set the right plural_resource_name" do
- @generator.instance_variable_get("@plural_resource_name").should == "posts"
- end
-end
-
-describe 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 = ThemedGenerator.new(["admin/gallery_items"], options)
- @generator.manifest
- 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.instance_variable_get("@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.instance_variable_get("@plural_model_name").should == "GalleryItems"
- end
-
- it "should set the right resource_name" do
- @generator.instance_variable_get("@resource_name").should == "gallery_item"
- end
-
- it "should set the right plural_resource_name" do
- @generator.instance_variable_get("@plural_resource_name").should == "gallery_items"
- end
-end
-
-describe 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 = ThemedGenerator.new(["admin/gallery_items", "picture"], options)
- @generator.manifest
- 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.instance_variable_get("@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.instance_variable_get("@plural_model_name").should == "Pictures"
- end
-
- it "should set the right resource_name" do
- @generator.instance_variable_get("@resource_name").should == "picture"
- end
-
- it "should set the right plural_resource_name" do
- @generator.instance_variable_get("@plural_resource_name").should == "pictures"
- end
-end
View
8 web-app-theme.gemspec
@@ -71,8 +71,8 @@ Gem::Specification.new do |s|
"stylesheets/themes/red/style.css",
"stylesheets/themes/reidb-greenish/style.css",
"stylesheets/themes/warehouse/style.css",
- "test/spec_helper.rb",
- "test/themed_generator_spec.rb",
+ "spec/spec_helper.rb",
+ "spec/themed_generator_spec.rb",
"web-app-theme.gemspec"
]
s.homepage = %q{http://github.com/pilu/web-app-theme}
@@ -80,8 +80,8 @@ Gem::Specification.new do |s|
s.rubygems_version = %q{1.3.7}
s.summary = %q{Web app theme generator}
s.test_files = [
- "test/spec_helper.rb",
- "test/themed_generator_spec.rb"
+ "spec/spec_helper.rb",
+ "spec/themed_generator_spec.rb"
]
if s.respond_to? :specification_version then
Something went wrong with that request. Please try again.