ardm 0.2.5 → 0.2.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.travis.yml ADDED
@@ -0,0 +1,11 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - 1.9.3
5
+ - 2.0
6
+ - 2.2
7
+
8
+ script: bundle exec rake
9
+
10
+ notifications:
11
+ email: false
data/Gemfile CHANGED
@@ -10,7 +10,7 @@ group :test do
10
10
  gem 'sqlite3'
11
11
  gem 'activerecord', '~> 4.0.0'
12
12
  gem 'addressable'
13
- gem 'database_cleaner', git: "git://github.com/lanej/database_cleaner.git", branch: "datamapper-fix"
13
+ gem 'database_cleaner'
14
14
  gem 'rspec-its'
15
15
  end
16
16
 
data/lib/ardm.rb CHANGED
@@ -1,3 +1,5 @@
1
+ require 'ardm/deprecation'
2
+
1
3
  module Ardm
2
4
  NotImplemented = Class.new(RuntimeError)
3
5
 
@@ -5,19 +5,6 @@ module Ardm
5
5
  module Associations
6
6
  extend ActiveSupport::Concern
7
7
 
8
- def assign_attributes(attrs, *a)
9
- new_attrs = attrs.inject({}) do |memo,(k,v)|
10
- if assoc = self.class.reflect_on_association(k)
11
- memo[assoc.foreign_key] = v && v.send(v.class.primary_key)
12
- else
13
- memo[k] = v
14
- end
15
- memo
16
- end
17
- super new_attrs, *a
18
- end
19
-
20
-
21
8
  # Convert options from DM style to AR style.
22
9
  #
23
10
  # Keep any unknown keys to use as conditions.
@@ -27,6 +14,7 @@ module Ardm
27
14
  ar = options.dup
28
15
  ar[:class_name] = ar.delete(:model) if ar[:model]
29
16
  ar[:foreign_key] = ar.delete(:child_key) if ar[:child_key]
17
+ ar[:source] = ar.delete(:via) if ar[:via]
30
18
  ar[:foreign_key] = ar[:foreign_key].first if ar[:foreign_key].respond_to?(:to_ary)
31
19
 
32
20
  if ar[:foreign_key] && property = klass.properties[ar[:foreign_key]]
@@ -93,10 +81,12 @@ module Ardm
93
81
  options.delete(:required)
94
82
  opts = Ardm::ActiveRecord::Associations.convert_options(self, options)
95
83
  super field, *opts
96
- assoc = reflect_on_association(field)
97
- Ardm::ActiveRecord::Record.on_finalize << lambda do
98
- self.class_eval do
99
- property assoc.foreign_key, assoc.klass.key.first.class, key: false
84
+ klass = self
85
+ Ardm::ActiveRecord::Finalize.on_finalize do
86
+ assoc = reflect_on_association(field)
87
+ klass.class_eval do
88
+ # @todo String is a hack... hoping AR can convert strings to integers during save for integer keys.
89
+ property assoc.foreign_key, assoc.primary_key_column.sql_type == "Integer" ? Integer : String, key: false
100
90
  end
101
91
  end
102
92
  nil
@@ -118,18 +108,11 @@ module Ardm
118
108
  end
119
109
 
120
110
  options[:order] = Ardm::ActiveRecord::Query.order(self, options[:order]) if options[:order]
121
- opts = Ardm::ActiveRecord::Associations.convert_options(self, options, :through, :order)
111
+ opts = Ardm::ActiveRecord::Associations.convert_options(self, options, :through, :order, :source)
122
112
 
123
- if Ardm.rails3?
124
- case count
125
- when 1 then has_one name, *opts
126
- when "many" then has_many name, *opts
127
- end
128
- else
129
- case count
130
- when 1 then has_one name, *opts
131
- when "many" then has_many name, *opts
132
- end
113
+ case count
114
+ when 1 then has_one name, *opts
115
+ when "many" then has_many name, *opts
133
116
  end
134
117
  end
135
118
 
@@ -46,8 +46,16 @@ module Ardm
46
46
  !new_record?
47
47
  end
48
48
 
49
- def save_self(*args)
50
- save(*args)
49
+ def save_self(run_callbacks=true)
50
+ save(run_callbacks)
51
+ end
52
+
53
+ def save(run_callbacks=true)
54
+ unless run_callbacks
55
+ raise Ardm::NotImplemented, "ActiveRecord doesn't support saving without callbacks"
56
+ end
57
+
58
+ super() # no args!
51
59
  end
52
60
 
53
61
  def save!(*args)
@@ -9,6 +9,8 @@ module Ardm
9
9
  included do
10
10
  alias_method :destory_without_ardm, :destroy
11
11
  alias_method :update_without_ardm, :update
12
+ alias_method :first_without_ardm, :first
13
+ alias_method :first_without_ardm!, :first!
12
14
 
13
15
  # we need to overrite the implementation in the class
14
16
  class_eval do
@@ -31,6 +33,22 @@ module Ardm
31
33
  end
32
34
  end
33
35
 
36
+ def first(*args)
37
+ if args.any?
38
+ all(*args).first_without_ardm
39
+ else
40
+ first_without_ardm
41
+ end
42
+ end
43
+
44
+ def first!(*args)
45
+ if args.any?
46
+ all(*args).first_without_ardm!
47
+ else
48
+ first_without_ardm!
49
+ end
50
+ end
51
+
34
52
  def first_or_create(attributes = nil, options = {}, &block)
35
53
  all(attributes).first || all(attributes).create(options, &block)
36
54
  end
@@ -45,6 +63,12 @@ module Ardm
45
63
  end
46
64
  end
47
65
 
66
+ def method_missing(meth, *a, &b)
67
+ super
68
+ rescue => e
69
+ raise NoMethodError, "Relation chain? #{self}.#{meth}\n#{e}"
70
+ end
71
+
48
72
  def all(options={})
49
73
  apply_finder_options(options)
50
74
  end
@@ -1,3 +1,4 @@
1
+ require 'ardm'
1
2
  require 'ardm/data_mapper/record'
2
3
 
3
4
  module Ardm
@@ -0,0 +1,18 @@
1
+ DataMapper::Collection.class_eval do
2
+
3
+ alias_method :old_delegate_to_relationship, :delegate_to_relationship
4
+
5
+ def delegate_to_relationship(relationship, query = nil)
6
+ Ardm::Deprecation.warn("Relation chain #{model.name}.#{relationship.name}")
7
+ old_delegate_to_relationship(relationship, query)
8
+ end
9
+
10
+ def includes(*)
11
+ self
12
+ end
13
+
14
+ def references(*)
15
+ self
16
+ end
17
+ end
18
+
@@ -1,5 +1,6 @@
1
1
  require 'active_support/concern'
2
2
  require 'dm-core'
3
+ require 'ardm/data_mapper/collection'
3
4
 
4
5
  module Ardm
5
6
  module DataMapper
@@ -0,0 +1,24 @@
1
+ module Ardm
2
+ module Deprecation
3
+ def self.deprecations
4
+ @deprecations ||= begin
5
+ at_exit { print_deprecations }
6
+ {}
7
+ end
8
+ end
9
+
10
+ def self.print_deprecations
11
+ $stderr.puts 'Deprecations by count:'
12
+ $stderr.puts deprecations.sort_by { |_,v| -v }.map { |message, count| "[%5d] %s" % [count, message] }
13
+ end
14
+
15
+ def self.warn(message)
16
+ message = "DEPRECATED: #{message} at #{caller[2].sub(Rails.root.to_s,'')}"
17
+ deprecations[message] ||= 0
18
+ deprecations[message] += 1
19
+ if deprecations[message] == 1
20
+ $stderr.puts message
21
+ end
22
+ end
23
+ end
24
+ end
data/lib/ardm/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Ardm
2
- VERSION = '0.2.5'
2
+ VERSION = '0.2.6'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ardm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.5
4
+ version: 0.2.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2014-12-01 00:00:00.000000000 Z
13
+ date: 2015-01-14 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activesupport
@@ -174,6 +174,7 @@ extra_rdoc_files:
174
174
  files:
175
175
  - .gitignore
176
176
  - .ruby-version
177
+ - .travis.yml
177
178
  - Gemfile
178
179
  - LICENSE
179
180
  - README.md
@@ -208,7 +209,9 @@ files:
208
209
  - lib/ardm/active_record/storage_names.rb
209
210
  - lib/ardm/active_record/validations.rb
210
211
  - lib/ardm/data_mapper.rb
212
+ - lib/ardm/data_mapper/collection.rb
211
213
  - lib/ardm/data_mapper/record.rb
214
+ - lib/ardm/deprecation.rb
212
215
  - lib/ardm/env.rb
213
216
  - lib/ardm/property.rb
214
217
  - lib/ardm/property/api_key.rb