active_repository 0.3.3 → 0.3.4

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 81105537b31e5f002fbc9ab9636c6432210dfe67
4
- data.tar.gz: 8853f27b52a7573ca205e6a4dd711623275d48f5
3
+ metadata.gz: 4066b2f88bcb0854427c407b27da4ae0a17a5b92
4
+ data.tar.gz: a39ab7fe4b4dd35ccd0af5fcbf1a8e3c9ab67376
5
5
  SHA512:
6
- metadata.gz: 00f5baf6e7e96bc125731fa6b3024ad6b775c50206705319892d971caa25a12d5de2fa534e7fea13111bf9e743ae5dcd2fb410c526b27e31dede3663b32b91e2
7
- data.tar.gz: 23c40fae93b26edf79a0efc84665a8480e0cb82383f08b6fdf3122cf7c6378cc61cc3f89ef6f922fddb943119780140b8e18d19f70bd74332225a2aa6724aaa4
6
+ metadata.gz: d47c8c5572ce3b52e1d0c4c4dbe7871777a5805995008251152cd03b658a227dfe8931f4562124a995170fa6129879db7c8c7d922d68d944c3e4411b1e99d6ef
7
+ data.tar.gz: 7626c6fc598178aad7104ac083f0e174bf7e274e25c64a43e273e197652d0d765faa8ec0d060953ee12871b329e70de28311b38bcdf90924bb84ac8fdae91ad1
data/Gemfile CHANGED
@@ -2,3 +2,5 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in active_repository.gemspec
4
4
  gemspec
5
+
6
+ gem 'sql_query_executor', ‘0.3.5’
@@ -50,7 +50,8 @@ module ActiveRepository
50
50
  include ActiveRepository::Associations
51
51
  include ActiveRepository::Writers::InstanceMethods
52
52
 
53
- class_attribute :model_class, :instance_writer => false
53
+ class_attribute :model_class, :before_save_methods, :after_save_methods, :instance_writer => false
54
+ class_attribute :before_create_methods, :after_create_methods, :instance_writer => false
54
55
  class_attribute :save_in_memory, :postfix, :instance_writer => true
55
56
 
56
57
  after_validation :set_timestamps
@@ -60,6 +61,14 @@ module ActiveRepository
60
61
  (repository? ? super : PersistenceAdapter.all(self).map { |object| serialize!(object.attributes) })
61
62
  end
62
63
 
64
+ def self.before_save(*methods, options)
65
+ self.before_save_methods = ((before_save_methods || []) + methods).flatten
66
+ end
67
+
68
+ def self.after_save(*methods, options)
69
+ self.after_save_methods = ((after_save_methods || []) + methods).flatten
70
+ end
71
+
63
72
  # Constantize class name
64
73
  def self.constantize
65
74
  self.to_s.constantize
@@ -194,21 +203,26 @@ module ActiveRepository
194
203
  end
195
204
 
196
205
  def save(force=false)
206
+ (before_save_methods || []).each { |method| self.send(method) }
207
+ result = true
208
+
197
209
  if self.class == persistence_class
198
210
  object = persistence_class.where(id: self.id).first_or_initialize
199
211
 
200
- if force || self.id.nil?
212
+ result = if force || self.id.nil?
201
213
  self.id = nil if self.id.nil?
202
214
  super
203
215
  elsif self.valid?
204
- object.attributes = self.attributes
216
+ object.attributes = self.attributes.select{ |key, value| self.class.serialized_attributes.include?(key.to_s) }
205
217
  object.save(true)
206
218
  end
207
-
208
- self.valid?
209
219
  else
210
- self.persist
220
+ result = self.persist
211
221
  end
222
+
223
+ (after_save_methods || []).each { |method| self.send(method) }
224
+
225
+ result
212
226
  end
213
227
 
214
228
  # Updates attributes from self with the attributes from the parameters
@@ -232,7 +246,7 @@ module ActiveRepository
232
246
 
233
247
  object ||= persistence_class.new
234
248
 
235
- attributes = self.attributes
249
+ attributes = self.attributes.select{ |key, value| self.class.serialized_attributes.include?(key.to_s) }
236
250
 
237
251
  attributes.delete(:id)
238
252
 
@@ -11,7 +11,8 @@ module ActiveModel
11
11
  end
12
12
 
13
13
  def validate_each(record, attribute, value)
14
- duplicate = record.class.where("id is not ?", (record.id ? record.id : 'null')).all.select do |object|
14
+ query = (record.id ? "id <> #{record.id}" : 'id is not null')
15
+ duplicate = record.class.where(query).all.select do |object|
15
16
  object.id != record.id && object.send(attribute) == record.send(attribute)
16
17
  end
17
18
 
@@ -1,3 +1,3 @@
1
1
  module ActiveRepository
2
- VERSION = "0.3.3"
2
+ VERSION = "0.3.4"
3
3
  end
@@ -19,6 +19,14 @@ end
19
19
 
20
20
  module ActiveHash
21
21
  class Base
22
+ def initialize(attributes = {})
23
+ attributes = attributes.symbolize_keys
24
+ @attributes = attributes
25
+ attributes.dup.each do |key, value|
26
+ send "#{key}=", value
27
+ end
28
+ end
29
+
22
30
  def self.insert(record)
23
31
  record_id = record.id.to_s
24
32
  record_hash = record.hash
@@ -88,6 +88,12 @@ shared_examples ".where" do
88
88
  results.last.name.should == "UK"
89
89
  end
90
90
 
91
+ it "finds elements which id's are not null" do
92
+ results = Country.where('id is not "null"').all
93
+
94
+ results.size.should == 5
95
+ end
96
+
91
97
  it "filters the records from an AR-like conditions hash" do
92
98
  first_id = Country.first.id
93
99
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_repository
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.3
4
+ version: 0.3.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Caio Torres
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-17 00:00:00.000000000 Z
11
+ date: 2014-04-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: active_hash