ardm 0.2.4 → 0.2.5
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/ardm/active_record/associations.rb +6 -0
- data/lib/ardm/active_record/base.rb +2 -0
- data/lib/ardm/active_record/persistence.rb +70 -0
- data/lib/ardm/active_record/property.rb +25 -0
- data/lib/ardm/active_record/query.rb +4 -0
- data/lib/ardm/active_record/record.rb +0 -92
- data/lib/ardm/active_record/relation.rb +4 -4
- data/lib/ardm/version.rb +1 -1
- metadata +3 -3
- data/spec/ardm/record_forwarding_spec.rb +0 -20
@@ -54,6 +54,12 @@ module Ardm
|
|
54
54
|
end
|
55
55
|
|
56
56
|
module ClassMethods
|
57
|
+
# The reflections returned here don't look like datamapper relationships.
|
58
|
+
# @todo improve this if needed with a wrapper
|
59
|
+
def relationships
|
60
|
+
reflections
|
61
|
+
end
|
62
|
+
|
57
63
|
def dump_associations_hash(options)
|
58
64
|
options.inject({}) do |new_attrs, (key, value)|
|
59
65
|
if reflection = reflect_on_association(key.to_sym)
|
@@ -6,6 +6,7 @@ require 'ardm/active_record/finalize'
|
|
6
6
|
require 'ardm/active_record/hooks'
|
7
7
|
require 'ardm/active_record/is'
|
8
8
|
require 'ardm/active_record/inheritance'
|
9
|
+
require 'ardm/active_record/persistence'
|
9
10
|
require 'ardm/active_record/property'
|
10
11
|
require 'ardm/active_record/query'
|
11
12
|
require 'ardm/active_record/repository'
|
@@ -41,6 +42,7 @@ module Ardm
|
|
41
42
|
include Ardm::ActiveRecord::Dirty
|
42
43
|
include Ardm::ActiveRecord::Is
|
43
44
|
include Ardm::ActiveRecord::Inheritance
|
45
|
+
include Ardm::ActiveRecord::Persistence
|
44
46
|
include Ardm::ActiveRecord::Property
|
45
47
|
include Ardm::ActiveRecord::Query
|
46
48
|
include Ardm::ActiveRecord::Repository
|
@@ -0,0 +1,70 @@
|
|
1
|
+
require 'active_support/concern'
|
2
|
+
|
3
|
+
module Ardm
|
4
|
+
module ActiveRecord
|
5
|
+
module Persistence
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
included do
|
9
|
+
class_attribute :raise_on_save_failure, instance_accessor: true
|
10
|
+
self.raise_on_save_failure = false
|
11
|
+
end
|
12
|
+
|
13
|
+
module ClassMethods
|
14
|
+
def update(*a)
|
15
|
+
options = dump_properties_hash(a.first)
|
16
|
+
options = dump_associations_hash(options)
|
17
|
+
assert_valid_attributes(options)
|
18
|
+
update_all(options) != 0
|
19
|
+
end
|
20
|
+
|
21
|
+
def update!(*a)
|
22
|
+
options = dump_properties_hash(a.first)
|
23
|
+
options = dump_associations_hash(options)
|
24
|
+
assert_valid_attributes(options)
|
25
|
+
update_all(options) != 0
|
26
|
+
end
|
27
|
+
|
28
|
+
def destroy(*a)
|
29
|
+
destroy_all
|
30
|
+
end
|
31
|
+
|
32
|
+
def destroy!(*a)
|
33
|
+
delete_all
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def destroy
|
38
|
+
self.class.delete(self.send(self.class.primary_key))
|
39
|
+
end
|
40
|
+
|
41
|
+
def new?
|
42
|
+
new_record?
|
43
|
+
end
|
44
|
+
|
45
|
+
def saved?
|
46
|
+
!new_record?
|
47
|
+
end
|
48
|
+
|
49
|
+
def save_self(*args)
|
50
|
+
save(*args)
|
51
|
+
end
|
52
|
+
|
53
|
+
def save!(*args)
|
54
|
+
save(*args) || (raise_on_save_failure && raise(Ardm::SaveFailureError, "Save Failed"))
|
55
|
+
end
|
56
|
+
|
57
|
+
def update(*a)
|
58
|
+
if a.size == 1
|
59
|
+
update_attributes(*a)
|
60
|
+
else
|
61
|
+
super
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def update!(*a)
|
66
|
+
update_attributes!(*a)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
@@ -87,6 +87,19 @@ module Ardm
|
|
87
87
|
@properties ||= PropertySet.new
|
88
88
|
end
|
89
89
|
|
90
|
+
def timestamps(at=:at)
|
91
|
+
case at
|
92
|
+
when :at
|
93
|
+
property :created_at, DateTime
|
94
|
+
property :updated_at, DateTime
|
95
|
+
when :on
|
96
|
+
property :created_on, Date
|
97
|
+
property :updated_on, Date
|
98
|
+
else
|
99
|
+
raise ArgumentError, "Unknown argument: timestamps(#{at.inspect})"
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
90
103
|
def initialize_attributes(attributes, options = {})
|
91
104
|
super(attributes, options)
|
92
105
|
|
@@ -306,6 +319,18 @@ module Ardm
|
|
306
319
|
end
|
307
320
|
end
|
308
321
|
|
322
|
+
def assign_attributes(attrs, *a)
|
323
|
+
new_attrs = attrs.inject({}) do |memo,(name,value)|
|
324
|
+
if property = self.class.properties[name]
|
325
|
+
memo[property.field] = property.typecast(value)
|
326
|
+
else
|
327
|
+
memo[name] = value
|
328
|
+
end
|
329
|
+
memo
|
330
|
+
end
|
331
|
+
super new_attrs, *a
|
332
|
+
end
|
333
|
+
|
309
334
|
# Retrieve the key(s) for this resource.
|
310
335
|
#
|
311
336
|
# This always returns the persisted key value,
|
@@ -26,6 +26,10 @@ module Ardm
|
|
26
26
|
end
|
27
27
|
|
28
28
|
module ClassMethods
|
29
|
+
def execute_sql(sql)
|
30
|
+
connection.execute(sql)
|
31
|
+
end
|
32
|
+
|
29
33
|
# hook into query engine in the most general way possible
|
30
34
|
def expand_hash_conditions_for_aggregates(options)
|
31
35
|
complex, simple = options.partition {|k,v| Ardm::Query::Operator === k }
|
@@ -9,15 +9,6 @@ module Ardm
|
|
9
9
|
|
10
10
|
self.abstract_class = true
|
11
11
|
|
12
|
-
class_attribute :raise_on_save_failure
|
13
|
-
self.raise_on_save_failure = false
|
14
|
-
|
15
|
-
JSON = Json
|
16
|
-
|
17
|
-
def self.execute_sql(sql)
|
18
|
-
connection.execute(sql)
|
19
|
-
end
|
20
|
-
|
21
12
|
def self.property(property_name, property_type, options={})
|
22
13
|
prop = super
|
23
14
|
begin
|
@@ -29,89 +20,6 @@ module Ardm
|
|
29
20
|
end
|
30
21
|
prop
|
31
22
|
end
|
32
|
-
|
33
|
-
# no-op in active record
|
34
|
-
def self.timestamps(at=:at)
|
35
|
-
case at
|
36
|
-
when :at
|
37
|
-
property :created_at, DateTime
|
38
|
-
property :updated_at, DateTime
|
39
|
-
when :on
|
40
|
-
property :created_on, Date
|
41
|
-
property :updated_on, Date
|
42
|
-
else
|
43
|
-
raise ArgumentError, "Unknown argument: timestamps(#{at.inspect})"
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
# The reflections returned here don't look like datamapper relationships.
|
48
|
-
# @todo improve this if needed with a wrapper
|
49
|
-
def self.relationships
|
50
|
-
reflections
|
51
|
-
end
|
52
|
-
|
53
|
-
def self.update(*a)
|
54
|
-
options = dump_properties_hash(a.first)
|
55
|
-
options = dump_associations_hash(options)
|
56
|
-
assert_valid_attributes(options)
|
57
|
-
update_all(options) != 0
|
58
|
-
end
|
59
|
-
|
60
|
-
def self.update!(*a)
|
61
|
-
options = dump_properties_hash(a.first)
|
62
|
-
options = dump_associations_hash(options)
|
63
|
-
assert_valid_attributes(options)
|
64
|
-
update_all(options) != 0
|
65
|
-
end
|
66
|
-
|
67
|
-
def self.destroy(*a)
|
68
|
-
destroy_all
|
69
|
-
end
|
70
|
-
|
71
|
-
def self.destroy!(*a)
|
72
|
-
delete_all
|
73
|
-
end
|
74
|
-
|
75
|
-
def destroy
|
76
|
-
self.class.delete(self.send(self.class.primary_key))
|
77
|
-
end
|
78
|
-
|
79
|
-
def new?
|
80
|
-
new_record?
|
81
|
-
end
|
82
|
-
|
83
|
-
def saved?
|
84
|
-
!new_record?
|
85
|
-
end
|
86
|
-
|
87
|
-
def save_self(*args)
|
88
|
-
save(*args)
|
89
|
-
end
|
90
|
-
|
91
|
-
def save!(*args)
|
92
|
-
save(*args) || (raise_on_save_failure && raise(Ardm::SaveFailureError, "Save Failed"))
|
93
|
-
end
|
94
|
-
|
95
|
-
def update(*a)
|
96
|
-
if a.size == 1
|
97
|
-
update_attributes(*a)
|
98
|
-
else
|
99
|
-
super
|
100
|
-
end
|
101
|
-
end
|
102
|
-
|
103
|
-
def update!(*a)
|
104
|
-
update_attributes!(*a)
|
105
|
-
end
|
106
|
-
|
107
|
-
#active record internals for detecting if this method should exist as an attribute
|
108
|
-
#if attribute_method_matcher(:open)
|
109
|
-
# def open
|
110
|
-
# read_attribute(:open)
|
111
|
-
# #attribute_missing(match, *args, &block)
|
112
|
-
# end
|
113
|
-
#end
|
114
|
-
|
115
23
|
end
|
116
24
|
end
|
117
25
|
end
|
@@ -32,15 +32,15 @@ module Ardm
|
|
32
32
|
end
|
33
33
|
|
34
34
|
def first_or_create(attributes = nil, options = {}, &block)
|
35
|
-
|
35
|
+
all(attributes).first || all(attributes).create(options, &block)
|
36
36
|
end
|
37
37
|
|
38
38
|
def first_or_create!(attributes = nil, options = {}, &block)
|
39
|
-
|
39
|
+
all(attributes).first || all(attributes).create!(options, &block)
|
40
40
|
end
|
41
41
|
|
42
42
|
def first_or_initialize(attributes = nil, options = {}, &block)
|
43
|
-
|
43
|
+
all(attributes).first || all(attributes).create(options, &block)
|
44
44
|
end
|
45
45
|
end
|
46
46
|
end
|
@@ -62,7 +62,7 @@ module Ardm
|
|
62
62
|
# We used to just patch this, like above, but we need to copy it over
|
63
63
|
# completely for rails4 since it no longer supports the old style finder
|
64
64
|
# methods that act more like the datamapper finders.
|
65
|
-
def apply_finder_options(options)
|
65
|
+
def apply_finder_options(options, *args)
|
66
66
|
relation = clone
|
67
67
|
return relation if options.nil?
|
68
68
|
|
data/lib/ardm/version.rb
CHANGED
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.
|
4
|
+
version: 0.2.5
|
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-
|
13
|
+
date: 2014-12-01 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: activesupport
|
@@ -193,6 +193,7 @@ files:
|
|
193
193
|
- lib/ardm/active_record/inheritance.rb
|
194
194
|
- lib/ardm/active_record/is.rb
|
195
195
|
- lib/ardm/active_record/is/state_machine.rb
|
196
|
+
- lib/ardm/active_record/persistence.rb
|
196
197
|
- lib/ardm/active_record/predicate_builder.rb
|
197
198
|
- lib/ardm/active_record/predicate_builder/array_handler.rb
|
198
199
|
- lib/ardm/active_record/predicate_builder/rails3.rb
|
@@ -275,7 +276,6 @@ files:
|
|
275
276
|
- lib/ardm/support/subject_set.rb
|
276
277
|
- lib/ardm/version.rb
|
277
278
|
- spec/ardm/datamapper_constants_spec.rb
|
278
|
-
- spec/ardm/record_forwarding_spec.rb
|
279
279
|
- spec/fixtures/api_user.rb
|
280
280
|
- spec/fixtures/article.rb
|
281
281
|
- spec/fixtures/bookmark.rb
|
@@ -1,20 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
require 'ardm/data_mapper/record'
|
3
|
-
|
4
|
-
describe Ardm::DataMapper::Record do
|
5
|
-
it 'responds to finalize' do
|
6
|
-
expect(described_class.respond_to?(:finalize)).to be_truthy
|
7
|
-
end
|
8
|
-
|
9
|
-
it 'responds to repository' do
|
10
|
-
expect(described_class.respond_to?(:repository)).to be_truthy
|
11
|
-
end
|
12
|
-
|
13
|
-
it 'responds to logger' do
|
14
|
-
expect(described_class.respond_to?(:logger)).to be_truthy
|
15
|
-
end
|
16
|
-
|
17
|
-
it 'responds to logger=' do
|
18
|
-
expect(described_class.respond_to?(:logger=)).to be_truthy
|
19
|
-
end
|
20
|
-
end
|