lib/action_widget/extensions/rails/generators.rb in action_widget-0.6.0.pre vs lib/action_widget/extensions/rails/generators.rb in action_widget-0.6.0
- old
+ new
@@ -1,58 +1,87 @@
module ActionWidget
module Extensions
module Rails
module Generators
-
class Base < ::Rails::Generators::Base
-
def self.namespace(name=nil)
- super.sub(/extensions:rails:/, '')
+ name || "action_widget"
end
-
end
-
- class Widget < Base
+
+ class ActionWidget < Base
source_root File.expand_path('../../../../../support/templates', __FILE__)
- argument :widget_name, :type => :string
- class_option :rspec, :type => :boolean, :default => true, :description => "Generates rspec file"
+ argument :name, :type => :string
+ class_option :test, type: :boolean, default: true
def generate_widget_implementation_file
- template('widget.rb.erb', "app/widgets/#{widget_implementation_filename}")
+ template('widget.rb.erb', implementation_path)
end
- def generate_widget_spec_file
- if defined?(::RSpec) && options.rspec?
- template('widget_spec.rb.erb', "spec/widgets/#{widget_spec_filename}")
+ def generate_widget_test_file
+ return unless options.test?
+
+ if defined?(::RSpec)
+ template('widget_spec.rb.erb', spec_path)
+ else
+ template('widget_test.rb.erb', test_path)
end
end
private
- def widget_spec_filename
- "#{widget_helper_name}_spec.rb"
- end
+ def configuration
+ ::ActionWidget.configuration
+ end
- def widget_implementation_filename
- "#{widget_helper_name}.rb"
- end
+ def test_path
+ File.join('test', configuration.directory, test_filename)
+ end
- def widget_class_name
- name = /[Ww]idget$/.match(widget_name) ? widget_name : "#{widget_name}Widget"
-
- name = name.titleize
- name = name.gsub(" ", '')
- name = name.gsub("/", '::')
-
- name
- end
+ def test_filename
+ "#{helper_name}_test.rb"
+ end
- def widget_helper_name
- widget_class_name.underscore
- end
+ def spec_path
+ File.join('spec', configuration.directory, spec_filename)
+ end
+ def spec_filename
+ "#{helper_name}_spec.rb"
+ end
+
+ def implementation_path
+ File.join('app', configuration.directory, implementation_filename)
+ end
+
+ def implementation_filename
+ "#{helper_name}.rb"
+ end
+
+ def class_name
+ [configuration.prefix, basename.classify, configuration.suffix].join('')
+ end
+
+ def superclass_name
+ configuration.superclass.to_s
+ end
+
+ def minitest_superclass_name
+ (configuration.minitest_superclass || "ActionView::TestCase").to_s
+ end
+
+ def helper_name
+ class_name.underscore
+ end
+
+ def basename
+ if match = name.underscore.match(configuration.pattern)
+ match[1]
+ else
+ name
+ end
+ end
end
-
end
end
end
-end
\ No newline at end of file
+end