Permalink
Browse files

[Bug Fix] Allow relations with CamelCased classes

  • Loading branch information...
1 parent 685b31c commit d3d8563e97bd53d92effa7f6aaeba936ca6acc32 Florian Bertholin committed Nov 23, 2012
View
@@ -2,10 +2,11 @@
require "bundler/gem_tasks"
$:.unshift("/Library/RubyMotion/lib")
require 'motion/project'
+require 'bubble-wrap'
Motion::Project::App.setup do |app|
# Use `rake config' to see complete project settings.
app.delegate_class = 'FakeDelegate'
- app.files = Dir.glob('./lib/motion_model/**/*.rb')
+ app.files += Dir.glob('./lib/motion_model/**/*.rb')
app.files = (app.files + Dir.glob('./app/**/*.rb')).uniq
end
View
@@ -37,6 +37,7 @@ def reset
# Put singular-form to plural form transformations here
@plurals = [
[/^person$/, 'people'],
+ [/^identity$/, 'identities'],
[/^child$/, 'children'],
[/^(.*)ee$/i, '\1ees'], # attendee => attendees
[/^(.*)us$/i, '\1i'], # alumnus => alumni
@@ -47,6 +48,7 @@ def reset
# Put plural-form to singular form transformations here
@singulars = [
[/^people$/, 'person'],
+ [/^identities$/, 'identity'],
[/^children$/, 'child'],
[/^(.*)ees$/i, '\1ee'], # attendees => attendee
[/^(.*)es$/i, '\1'], # passes => pass
@@ -21,9 +21,9 @@ def add_attr(name, type, default = nil)
def classify
case @type
when :belongs_to
- @klass ||= Object.const_get @name.to_s.downcase.capitalize
+ @klass ||= Object.const_get(@name.to_s.camelize)
when :has_many
- @klass ||= Object.const_get @name.to_s.downcase.singularize.capitalize
+ @klass ||= Object.const_get(@name.to_s.singularize.camelize)
else
raise "#{@name} is not a relation. This isn't supposed to happen."
end
@@ -42,7 +42,7 @@ def order(field = nil, &block)
######## relational operators ########
def translate_case(item, case_sensitive)#nodoc
- item = item.downcase if case_sensitive === false && item.respond_to?(:downcase)
+ item = item.underscore if case_sensitive === false && item.respond_to?(:underscore)
item
end
@@ -198,7 +198,7 @@ def create(options)
def new(options = {})
raise ArgumentError.new("Creating on a relation requires the parent be saved first.") if @related_object.nil?
- id_field = (@related_object.class.to_s.downcase + '_id').to_sym
+ id_field = (@related_object.class.to_s.underscore + '_id').to_sym
new_obj = @klass.new(options.merge(id_field => @related_object.id))
new_obj
@@ -216,7 +216,7 @@ def length
# This both establishes the relation and saves the related
# object, so make sure the related object is valid.
def push(object)
- id_field = (@related_object.class.to_s.downcase + '_id=').to_sym
+ id_field = (@related_object.class.to_s.underscore + '_id=').to_sym
object.send(id_field, @related_object.id)
result = object.save
result ||= @related_object.save
@@ -182,7 +182,7 @@ def has_many(*relations)
end
def belongs_to_id(relation)
- (relation.downcase + '_id').to_sym
+ (relation.to_s.underscore + '_id').to_sym
end
# Use at class level, as follows
@@ -342,7 +342,7 @@ def initialize(options = {})
@cached_date_formatter = NSDateFormatter.alloc.init # Create once, as they are expensive to create
@cached_date_formatter.dateFormat = "MM-dd-yyyy HH:mm"
-
+
unless options[:id]
options[:id] = self.class.next_id
else
@@ -489,7 +489,6 @@ def relation_for(col)
# Date is a real date object.
def method_missing(method, *args, &block)
base_method = method.to_s.gsub('=', '').to_sym
-
col = column_named(base_method)
raise NoMethodError.new("nil column #{method} accessed from #{caller[1]}.") if col.nil?
View
@@ -12,5 +12,6 @@ Gem::Specification.new do |gem|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
gem.name = "motion_model"
gem.require_paths = ["lib"]
+ gem.add_dependency 'bubble-wrap', '~> 1.1.4'
gem.version = MotionModel::VERSION
end
View
@@ -13,10 +13,37 @@ class Task
end
+class User
+ include MotionModel::Model
+ columns :name => :string
+
+ has_many :email_accounts
+end
+
+class EmailAccount
+ include MotionModel::Model
+ columns :name => :string
+ belongs_to :user
+end
+
Inflector.inflections.irregular 'assignees', 'assignee'
Inflector.inflections.irregular 'assignee', 'assignees'
describe 'related objects' do
+ describe "supporting belongs_to and has_many with camelcased relations" do
+ before do
+ EmailAccount.delete_all
+ User.delete_all
+ end
+
+ it "camelcased style" do
+ t = User.create(:name => "Arkan")
+ t.email_accounts.create(:name => "Gmail")
+ EmailAccount.first.user.name.should == "Arkan"
+ User.last.email_accounts.last.name.should == "Gmail"
+ end
+ end
+
describe 'has_many' do
it "is wired up right" do
lambda {Task.new}.should.not.raise

0 comments on commit d3d8563

Please sign in to comment.