lib/common_name.rb in common_name-0.1.6 vs lib/common_name.rb in common_name-0.1.7
- old
+ new
@@ -1,15 +1,10 @@
require 'active_support'
require 'active_support/version'
-%w{
- active_support/inflector
-}.each do |active_support_3_requirement|
- require active_support_3_requirement
-end if ::ActiveSupport::VERSION::MAJOR == 3
-
-if defined?(::Rails)
- require 'common_name/railtie'
+if ::ActiveSupport::VERSION::MAJOR >= 3
+ require 'active_support/core_ext/string'
+ require 'active_support/inflector'
end
# This library comes out of my frustration with seeing lines like
#
# BusCompany.name.underscore.humanize.downcase.pluralize #=> "bus companies"
@@ -30,11 +25,13 @@
model
}
# delegate instance methods to class methods
METHODS.each do |m|
- eval %{ def common_#{m}; self.class.common_#{m}; end }
+ define_method "common_#{m}" do
+ self.class.send("common_#{m}")
+ end
end
module ClassMethods
# "bus_company"
#
@@ -88,7 +85,24 @@
# See +common_human+ for notes on capitalization.
def common_plural_human; _COMMON_PLURAL_HUMAN ||= common_plural_title.downcase; end
# "BusCompanies"
def common_plural_camel; _COMMON_PLURAL_CAMEL ||= common_plural.camelcase; end
+ end
+end
+
+if defined?(::ActiveRecord)
+ ::ActiveRecord::Base.class_eval do
+ def self._common_name
+ name.underscore
+ end
+ include ::CommonName
+ end
+end
+if defined?(::ActionController)
+ ::ActionController::Base.class_eval do
+ def self._common_name
+ controller_name.singularize
+ end
+ include ::CommonName
end
end