From 1c4d28ba314c8cdd0039becf3bc9e678219b8f46 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Wed, 17 Jun 2009 10:37:39 -0500 Subject: Move model naming into ActiveModel --- activemodel/lib/active_model.rb | 2 ++ activemodel/lib/active_model/naming.rb | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 activemodel/lib/active_model/naming.rb (limited to 'activemodel/lib') diff --git a/activemodel/lib/active_model.rb b/activemodel/lib/active_model.rb index 73cee9b88f..f8e5725e9c 100644 --- a/activemodel/lib/active_model.rb +++ b/activemodel/lib/active_model.rb @@ -29,6 +29,8 @@ module ActiveModel autoload :Base, 'active_model/base' autoload :DeprecatedErrorMethods, 'active_model/deprecated_error_methods' autoload :Errors, 'active_model/errors' + autoload :Name, 'active_model/naming' + autoload :Naming, 'active_model/naming' autoload :Observer, 'active_model/observing' autoload :Observing, 'active_model/observing' autoload :StateMachine, 'active_model/state_machine' diff --git a/activemodel/lib/active_model/naming.rb b/activemodel/lib/active_model/naming.rb new file mode 100644 index 0000000000..ffb44e3824 --- /dev/null +++ b/activemodel/lib/active_model/naming.rb @@ -0,0 +1,25 @@ +require 'active_support/inflector' + +module ActiveModel + class Name < String + attr_reader :singular, :plural, :element, :collection, :partial_path + alias_method :cache_key, :collection + + def initialize(name) + super + @singular = ActiveSupport::Inflector.underscore(self).tr('/', '_').freeze + @plural = ActiveSupport::Inflector.pluralize(@singular).freeze + @element = ActiveSupport::Inflector.underscore(ActiveSupport::Inflector.demodulize(self)).freeze + @collection = ActiveSupport::Inflector.tableize(self).freeze + @partial_path = "#{@collection}/#{@element}".freeze + end + end + + module Naming + # Returns an ActiveModel::Name object for module. It can be + # used to retrieve all kinds of naming-related information. + def model_name + @_model_name ||= ActiveModel::Name.new(name) + end + end +end -- cgit v1.2.3 From d5d59230f4d8f0457fc793446a3dbcdce0057a78 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Wed, 17 Jun 2009 20:19:21 -0500 Subject: Simplify AMo validation attribute reader --- activemodel/lib/active_model/errors.rb | 6 +++--- activemodel/lib/active_model/validations.rb | 6 +----- 2 files changed, 4 insertions(+), 8 deletions(-) (limited to 'activemodel/lib') diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb index 2e643f108f..a4cf700231 100644 --- a/activemodel/lib/active_model/errors.rb +++ b/activemodel/lib/active_model/errors.rb @@ -68,7 +68,7 @@ module ActiveModel # Will add an error message to each of the attributes in +attributes+ that is empty. def add_on_empty(attributes, custom_message = nil) [attributes].flatten.each do |attribute| - value = @base.get_attribute_value(attribute) + value = @base.send(attribute) is_empty = value.respond_to?(:empty?) ? value.empty? : false add(attribute, :empty, :default => custom_message) unless !value.nil? && !is_empty end @@ -77,7 +77,7 @@ module ActiveModel # Will add an error message to each of the attributes in +attributes+ that is blank (using Object#blank?). def add_on_blank(attributes, custom_message = nil) [attributes].flatten.each do |attribute| - value = @base.get_attribute_value(attribute) + value = @base.send(attribute) add(attribute, :blank, :default => custom_message) if value.blank? end end @@ -146,7 +146,7 @@ module ActiveModel defaults = defaults.compact.flatten << :"messages.#{message}" key = defaults.shift - value = @base.get_attribute_value(attribute) + value = @base.send(attribute) options = { :default => defaults, :model => @base.class.name.humanize, diff --git a/activemodel/lib/active_model/validations.rb b/activemodel/lib/active_model/validations.rb index 6b6f51d942..5223cea135 100644 --- a/activemodel/lib/active_model/validations.rb +++ b/activemodel/lib/active_model/validations.rb @@ -64,7 +64,7 @@ module ActiveModel # Declare the validation. send(validation_method(options[:on]), options) do |record| attrs.each do |attr| - value = record.get_attribute_value(attr) + value = record.send(attr) next if (value.nil? && options[:allow_nil]) || (value.blank? && options[:allow_blank]) yield record, attr, value end @@ -93,10 +93,6 @@ module ActiveModel def invalid? !valid? end - - def get_attribute_value(attribute) - respond_to?(attribute.to_sym) ? send(attribute.to_sym) : instance_variable_get(:"@#{attribute}") - end end end -- cgit v1.2.3 From af5301089f38b9aad9a92424d5fa8f9a9b27c4e1 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Wed, 17 Jun 2009 20:36:21 -0500 Subject: Add simple attribute implementation backed by ivars --- activemodel/lib/active_model.rb | 1 + activemodel/lib/active_model/attributes.rb | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) create mode 100644 activemodel/lib/active_model/attributes.rb (limited to 'activemodel/lib') diff --git a/activemodel/lib/active_model.rb b/activemodel/lib/active_model.rb index f8e5725e9c..048f542239 100644 --- a/activemodel/lib/active_model.rb +++ b/activemodel/lib/active_model.rb @@ -26,6 +26,7 @@ $:.unshift(activesupport_path) if File.directory?(activesupport_path) require 'active_support' module ActiveModel + autoload :Attributes, 'active_model/attributes' autoload :Base, 'active_model/base' autoload :DeprecatedErrorMethods, 'active_model/deprecated_error_methods' autoload :Errors, 'active_model/errors' diff --git a/activemodel/lib/active_model/attributes.rb b/activemodel/lib/active_model/attributes.rb new file mode 100644 index 0000000000..4665525281 --- /dev/null +++ b/activemodel/lib/active_model/attributes.rb @@ -0,0 +1,17 @@ +require 'active_support/core_ext/object/instance_variables' + +module ActiveModel + module Attributes + def attributes + instance_values + end + + def read_attribute(attr_name) + instance_variable_get(:"@#{attr_name}") + end + + def write_attribute(attr_name, value) + instance_variable_set(:"@#{attr_name}", value) + end + end +end -- cgit v1.2.3 From fbdf706fffbfb17731a1f459203d242414ef5086 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Wed, 17 Jun 2009 21:27:36 -0500 Subject: Add basic JSON serializer to AMo --- activemodel/lib/active_model.rb | 4 +++ activemodel/lib/active_model/serializers/json.rb | 38 ++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 activemodel/lib/active_model/serializers/json.rb (limited to 'activemodel/lib') diff --git a/activemodel/lib/active_model.rb b/activemodel/lib/active_model.rb index 048f542239..544121c593 100644 --- a/activemodel/lib/active_model.rb +++ b/activemodel/lib/active_model.rb @@ -38,6 +38,10 @@ module ActiveModel autoload :TestCase, 'active_model/test_case' autoload :Validations, 'active_model/validations' autoload :ValidationsRepairHelper, 'active_model/validations_repair_helper' + + module Serializers + autoload :JSON, 'active_model/serializers/json' + end end I18n.load_path << File.dirname(__FILE__) + '/active_model/locale/en.yml' diff --git a/activemodel/lib/active_model/serializers/json.rb b/activemodel/lib/active_model/serializers/json.rb new file mode 100644 index 0000000000..60b5cbe948 --- /dev/null +++ b/activemodel/lib/active_model/serializers/json.rb @@ -0,0 +1,38 @@ +require 'active_support/json' +require 'active_support/core_ext/class/attribute_accessors' +require 'active_support/core_ext/hash/except' +require 'active_support/core_ext/hash/slice' + +module ActiveModel + module Serializers + module JSON + extend ActiveSupport::Concern + include ActiveModel::Attributes + + included do + cattr_accessor :include_root_in_json, :instance_writer => false + end + + def encode_json(encoder) + options = encoder.options || {} + + hash = if options[:only] + only = Array.wrap(options[:only]).map { |attr| attr.to_s } + attributes.slice(*only) + elsif options[:except] + except = Array.wrap(options[:except]).map { |attr| attr.to_s } + attributes.except(*except) + else + attributes + end + + hash = { self.class.model_name.element => hash } if include_root_in_json + ActiveSupport::JSON.encode(hash) + end + + def as_json(options = nil) + self + end + end + end +end -- cgit v1.2.3