active_metadata 0.7.4 → 0.7.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,6 +3,25 @@ require File.expand_path("../../../spec/dummy/config/environment.rb", __FILE__)
3
3
  ENV["RAILS_ROOT"] ||= File.dirname(__FILE__) + "../../../spec/dummy"
4
4
 
5
5
  require 'cucumber/rails'
6
+ require_relative "../../spec/support/migrations"
6
7
 
7
8
  ENGINE_RAILS_ROOT=File.join(File.dirname(__FILE__), '../../')
8
- Dir[File.join(ENGINE_RAILS_ROOT, "spec/support/**/*.rb")].each {|f| require f }
9
+
10
+
11
+ Before do
12
+ TestDb.up
13
+ Dir[File.join(ENGINE_RAILS_ROOT, "spec/support/**/*.rb")].each {|f| require f }
14
+ end
15
+
16
+ After do
17
+ [Document, Section, Chapter, User, ActiveMetadata::Note,
18
+ ActiveMetadata::Watcher, ActiveMetadata::Attachment, ActiveMetadata::History].each do |i|
19
+ i.delete_all
20
+ end
21
+ FileUtils.remove_dir File.expand_path('public/system/') if Dir.exist?(File.expand_path('public/system/'))
22
+ Rails.cache.clear
23
+ end
24
+
25
+ at_exit do
26
+ TestDb.down
27
+ end
@@ -3,6 +3,10 @@ module ActiveMetadata
3
3
  CONFIG = File.exists?('config/active_metadata.yml') ? YAML.load_file('config/active_metadata.yml')[Rails.env] : {}
4
4
  CONFIG['cache_expires_in'] ||= 60
5
5
 
6
+ def self.skip_history?
7
+ false
8
+ end
9
+
6
10
  ## Define ModelMethods
7
11
  module Base
8
12
 
@@ -20,12 +24,16 @@ module ActiveMetadata
20
24
  module Config
21
25
 
22
26
  def acts_as_metadata *args
27
+ options = args.extract_options!
28
+ options[:ancestors] ||= []
29
+ options[:persists_ancestor] ||= false
30
+
23
31
  before_update :manage_concurrency
24
32
  after_save :save_history
25
33
  attr_accessor :conflicts
26
34
  attr_accessor :active_metadata_timestamp
27
35
 
28
- class_variable_set("@@active_metadata_ancestors", args.empty? ? nil : args[0][:active_metadata_ancestors])
36
+ instance_variable_set("@active_metadata_options", options)
29
37
 
30
38
  include ActiveMetadata::Base::InstanceMethods
31
39
  include ActiveMetadata::Persistence
@@ -56,12 +64,16 @@ module ActiveMetadata
56
64
 
57
65
  end
58
66
 
59
- def metadata_id
60
- metadata_root.id
67
+ # if force is set to true go directly to the private find method
68
+ # options integrity will not be checked and ancestor will be looked also if option persists_ancestor is false
69
+ # usefull to get the ancestor instance also if is not persisted
70
+ def metadata_id force=false
71
+ force ? find_metadata_ancestor_instance.id : metadata_root.id
61
72
  end
62
73
 
63
- def metadata_class
64
- metadata_root.class.to_s
74
+ # same as #metadata_id
75
+ def metadata_class force=false
76
+ force ? find_metadata_ancestor_instance.class.to_s : metadata_root.class.to_s
65
77
  end
66
78
 
67
79
  # Normalize the active_metadata_timestamp into a float to be comparable with the history
@@ -80,14 +92,9 @@ module ActiveMetadata
80
92
  end
81
93
 
82
94
  def metadata_root
83
- active_metadata_ancestors = self.class.class_variable_get("@@active_metadata_ancestors")
84
- return self if active_metadata_ancestors.nil?
85
- receiver = self
86
- active_metadata_ancestors.each do |item|
87
- receiver = receiver.send item
88
- end
89
- raise(RuntimeError.new,"[active_metdata] - Ancestor model is not yet persisted") unless receiver
90
- receiver
95
+ options = self.class.instance_variable_get("@active_metadata_options")
96
+ return self unless (options[:persists_ancestor] && options[:ancestors].size > 0 )
97
+ find_metadata_ancestor_instance
91
98
  end
92
99
 
93
100
  # Resolve concurrency using the provided timestamps and the active_metadata histories.
@@ -157,6 +164,18 @@ module ActiveMetadata
157
164
 
158
165
  end
159
166
 
167
+ private
168
+
169
+ def find_metadata_ancestor_instance
170
+ receiver = self
171
+ self.class.instance_variable_get("@active_metadata_options")[:ancestors].each do |item|
172
+ res = receiver.send item
173
+ receiver = res.is_a?(Array) ? res.first : res
174
+ end
175
+ raise(RuntimeError.new,"[active_metdata] - Ancestor model is not yet persisted") unless receiver
176
+ receiver
177
+ end
178
+
160
179
  end # InstanceMethods
161
180
 
162
181
  end
@@ -7,6 +7,7 @@ module ActiveMetadata::Persistence::History
7
7
  module InstanceMethods
8
8
 
9
9
  def save_history
10
+ return if ActiveMetadata.skip_history?
10
11
  self.changes.each do |key, value|
11
12
  next if ActiveMetadata::CONFIG['history_skip_fields'].include?(key)
12
13
  ActiveMetadata::History.create! :value => value[1],:document_class => metadata_class, :document_id => metadata_id,:label => key.to_s, :created_by => current_user_id
@@ -1,3 +1,3 @@
1
1
  module ActiveMetadata
2
- VERSION = "0.7.4"
2
+ VERSION = "0.7.5"
3
3
  end
@@ -4,8 +4,10 @@ namespace :active_metadata do
4
4
  ENV['JCI'] = 'on'
5
5
  ENV['RAILS_ENV'] ||= 'test'
6
6
 
7
- task :migrate do
8
- Rake::Task["db:migrate"].invoke
7
+ task :migrate, :environment do
8
+ sh "mkdir -p spec/dummy/tmp/cache"
9
+ Rake::Task["app:db:create"].invoke
10
+ Rake::Task["app:db:migrate"].invoke
9
11
  end
10
12
 
11
13
  task :rspec do
@@ -32,7 +34,7 @@ namespace :active_metadata do
32
34
 
33
35
  puts "Copying migrations"
34
36
  ts = Time.now.utc.strftime('%Y%m%d%H%M%S')
35
- FileUtils.cp File.expand_path('../../../db/migrate/02_active_metadata_migrations.rb', __FILE__), File.expand_path("db/migrate/#{ts}_active_metadata_migrations.rb")
37
+ FileUtils.cp File.expand_path('../../../db/migrate/01_active_metadata_migrations.rb', __FILE__), File.expand_path("db/migrate/#{ts}_active_metadata_migrations.rb")
36
38
  puts "run rake db:migrate to complete the gem installation"
37
39
 
38
40
  end
File without changes
@@ -11,7 +11,7 @@
11
11
  #
12
12
  # It's strongly recommended to check this file into your version control system.
13
13
 
14
- ActiveRecord::Schema.define(:version => 2) do
14
+ ActiveRecord::Schema.define(:version => 1) do
15
15
 
16
16
  create_table "active_metadata_attachments", :force => true do |t|
17
17
  t.string "label"
@@ -82,33 +82,4 @@ ActiveRecord::Schema.define(:version => 2) do
82
82
  add_index "active_metadata_watchers", ["label"], :name => "index_active_metadata_watchers_on_label"
83
83
  add_index "active_metadata_watchers", ["owner_id"], :name => "index_active_metadata_watchers_on_owner_id"
84
84
 
85
- create_table "chapters", :force => true do |t|
86
- t.string "title"
87
- t.datetime "created_at", :null => false
88
- t.datetime "updated_at", :null => false
89
- end
90
-
91
- create_table "documents", :force => true do |t|
92
- t.string "name"
93
- t.string "surname"
94
- t.boolean "keep_alive"
95
- t.datetime "created_at", :null => false
96
- t.datetime "updated_at", :null => false
97
- end
98
-
99
- create_table "sections", :force => true do |t|
100
- t.string "title"
101
- t.integer "document_id"
102
- t.datetime "created_at", :null => false
103
- t.datetime "updated_at", :null => false
104
- end
105
-
106
- create_table "users", :force => true do |t|
107
- t.string "email"
108
- t.string "firstname"
109
- t.string "lastname"
110
- t.datetime "created_at", :null => false
111
- t.datetime "updated_at", :null => false
112
- end
113
-
114
85
  end
@@ -4,19 +4,19 @@ require "time"
4
4
 
5
5
  describe ActiveMetadata do
6
6
 
7
- context "model methods" do
7
+ context "class methods" do
8
8
 
9
9
  it "should exist a method acts_as_metadata in the model" do
10
10
  Document.respond_to?(:acts_as_metadata).should be_true
11
11
  end
12
12
 
13
- it "should find the active_metadata_ancestors if no active_metadata_ancestors params has been provided" do
13
+ it "should find the ancestors if no ancestors params has been provided" do
14
14
  @document = Document.create! { |d| d.name = "John" }
15
15
  @document.metadata_id.should eq @document.id
16
16
  @document.metadata_class.should eq @document.class.to_s
17
17
  end
18
18
 
19
- it "should find the metadata_root.id if an active_metadata_ancestors params has been specified" do
19
+ it "should find the metadata_root.id if an ancestors params has been specified and persists_ancestor is true" do
20
20
  @document = Document.create! { |d| d.name = "John" }
21
21
  @section = @document.create_section :title => "new section"
22
22
  @section.metadata_id.should eq @document.id
@@ -33,5 +33,26 @@ describe ActiveMetadata do
33
33
 
34
34
  end
35
35
 
36
+ context "ancestors" do
37
+
38
+ it "should return the base instance class following the ancestor chain" do
39
+ @document = Document.create! { |d| d.name = "John" }
40
+ @chapter = Chapter.create(title: "title")
41
+ @section = @document.create_section
42
+ @section.metadata_id(true).should eq @document.id
43
+ @section.metadata_class(true).should eq "Document"
44
+ end
45
+
46
+ it "should return the base instance class following the ancestor chain passing has_many associations" do
47
+ @document = Document.create! { |d| d.name = "John" }
48
+ @chapter = Chapter.create(title: "title")
49
+ @section = @document.create_section chapter_id: @chapter.id
50
+
51
+ @chapter.metadata_id(true).should eq @document.id
52
+ @chapter.metadata_class(true).should eq "Document"
53
+ end
54
+
55
+ end
56
+
36
57
 
37
58
  end
@@ -125,9 +125,9 @@ describe ActiveMetadata do
125
125
  end
126
126
 
127
127
  it "should has_notes_for verify if defined field has attachments" do
128
- @document.has_attachments_for(:name).should be_false
129
- @document.save_attachment_for(:name, @attachment)
130
- @document.has_attachments_for(:name).should be_true
128
+ @document.has_attachments_for(:surname).should be_false
129
+ @document.save_attachment_for(:surname, @attachment)
130
+ @document.has_attachments_for(:surname).should be_true
131
131
  end
132
132
 
133
133
  end
@@ -5,7 +5,6 @@ describe ActiveMetadata do
5
5
 
6
6
  before(:each) do
7
7
  @document = Document.create! { |d| d.name = "John" }
8
- @document.reload
9
8
  end
10
9
 
11
10
  describe "notes" do
data/spec/spec_helper.rb CHANGED
@@ -8,10 +8,9 @@ require File.expand_path("../dummy/config/environment.rb", __FILE__)
8
8
  require 'rails/all'
9
9
  require 'active_metadata'
10
10
  require 'rack/test/uploaded_file'
11
+ require 'support/migrations'
11
12
 
12
13
  ENGINE_RAILS_ROOT=File.join(File.dirname(__FILE__), '../')
13
- Dir[File.join(ENGINE_RAILS_ROOT, "spec/support/**/*.rb")].each {|f| require f }
14
-
15
14
  require 'rspec/rails'
16
15
 
17
16
 
@@ -25,6 +24,11 @@ RSpec.configure do |config|
25
24
  # config.mock_with :rr
26
25
  config.mock_with :rspec
27
26
 
27
+ config.before(:suite) do
28
+ TestDb.up
29
+ Dir[File.join(ENGINE_RAILS_ROOT, "spec/support/**/*.rb")].each {|f| require f }
30
+ end
31
+
28
32
  # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
29
33
  # config.fixture_path = "#{::Rails.root}/spec/fixtures"
30
34
 
@@ -35,17 +39,18 @@ RSpec.configure do |config|
35
39
 
36
40
 
37
41
  config.before(:each) do
38
- Document.delete_all
39
- ActiveMetadata::Note.delete_all
40
- ActiveMetadata::Watcher.delete_all
41
- ActiveMetadata::Attachment.delete_all
42
- ActiveMetadata::History.delete_all
43
- FileUtils.remove_dir File.expand_path('public/system/') if Dir.exist?(File.expand_path('public/system/'))
42
+ [Document, Section, Chapter, User, ActiveMetadata::Note,
43
+ ActiveMetadata::Watcher, ActiveMetadata::Attachment, ActiveMetadata::History].each do |i|
44
+ i.delete_all
45
+ end
46
+ FileUtils.remove_dir File.expand_path('public/system/') if Dir.exist?(File.expand_path('public/system/'))
47
+ Rails.cache.clear
44
48
  end
45
49
 
46
- config.after(:suite) do
47
- # seems that closing the established connection isn't really necessary
50
+ config.after(:suite) do
51
+ TestDb.down
48
52
  end
53
+
49
54
  end
50
55
 
51
56
  def test_pdf name='pdf_test'
@@ -1,4 +1,5 @@
1
1
  # encoding: utf-8
2
2
  class Chapter < ActiveRecord::Base
3
- acts_as_metadata
3
+ acts_as_metadata ancestors: [:sections, :document]
4
+ has_many :sections
4
5
  end
@@ -1,7 +1,6 @@
1
1
  # encoding: utf-8
2
2
  class Document < ActiveRecord::Base
3
3
  has_one :section
4
-
5
4
  acts_as_metadata
6
5
 
7
6
  end
@@ -0,0 +1,38 @@
1
+ module TestDb
2
+
3
+ def self.up
4
+ ActiveRecord::Base.connection.create_table :documents, force: true do |t|
5
+ t.string :name
6
+ t.string :surname
7
+ t.boolean :keep_alive
8
+ t.timestamps
9
+ end
10
+
11
+ ActiveRecord::Base.connection.create_table :sections, force: true do |t|
12
+ t.string :title
13
+ t.integer :document_id
14
+ t.integer :chapter_id
15
+ t.timestamps
16
+ end
17
+
18
+ ActiveRecord::Base.connection.create_table :chapters, force: true do |t|
19
+ t.string :title
20
+ t.timestamps
21
+ end
22
+
23
+ ActiveRecord::Base.connection.create_table :users, force: true do |t|
24
+ t.string :email
25
+ t.string :firstname
26
+ t.string :lastname
27
+ t.timestamps
28
+ end
29
+ end
30
+
31
+ def self.down
32
+ ActiveRecord::Base.connection.drop_table :users
33
+ ActiveRecord::Base.connection.drop_table :chapters
34
+ ActiveRecord::Base.connection.drop_table :sections
35
+ ActiveRecord::Base.connection.drop_table :documents
36
+ end
37
+
38
+ end
@@ -1,7 +1,5 @@
1
1
  # encoding: utf-8
2
2
  class Section < ActiveRecord::Base
3
3
  belongs_to :document
4
-
5
- acts_as_metadata :active_metadata_ancestors => [:document]
6
-
4
+ acts_as_metadata :ancestors => [:document], :persists_ancestor => true
7
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_metadata
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.4
4
+ version: 0.7.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: 2012-05-18 00:00:00.000000000 Z
13
+ date: 2012-05-21 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rails
@@ -165,8 +165,7 @@ files:
165
165
  - config/cucumber.yml
166
166
  - config/database.yml
167
167
  - config/routes.rb
168
- - db/migrate/01_create_test_resources.rb
169
- - db/migrate/02_active_metadata_migrations.rb
168
+ - db/migrate/01_active_metadata_migrations.rb
170
169
  - lib/active_metadata/base.rb
171
170
  - lib/active_metadata/engine.rb
172
171
  - lib/active_metadata/form_helper.rb
@@ -219,9 +218,8 @@ files:
219
218
  - spec/dummy/config/initializers/wrap_parameters.rb
220
219
  - spec/dummy/config/locales/en.yml
221
220
  - spec/dummy/config/routes.rb
222
- - spec/dummy/db/development.sqlite3
221
+ - spec/dummy/db/.gitkeep
223
222
  - spec/dummy/db/schema.rb
224
- - spec/dummy/db/test.sqlite3
225
223
  - spec/dummy/lib/assets/.gitkeep
226
224
  - spec/dummy/log/.gitkeep
227
225
  - spec/dummy/public/404.html
@@ -241,6 +239,7 @@ files:
241
239
  - spec/spec_helper.rb
242
240
  - spec/support/chapter.rb
243
241
  - spec/support/document.rb
242
+ - spec/support/migrations.rb
244
243
  - spec/support/pdf_test.pdf
245
244
  - spec/support/pdf_test_1.pdf
246
245
  - spec/support/pdf_test_2.pdf
@@ -262,7 +261,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
262
261
  version: '0'
263
262
  segments:
264
263
  - 0
265
- hash: -1577103634899317136
264
+ hash: -2239752898165764505
266
265
  required_rubygems_version: !ruby/object:Gem::Requirement
267
266
  none: false
268
267
  requirements:
@@ -271,7 +270,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
271
270
  version: '0'
272
271
  segments:
273
272
  - 0
274
- hash: -1577103634899317136
273
+ hash: -2239752898165764505
275
274
  requirements: []
276
275
  rubyforge_project: active_metadata
277
276
  rubygems_version: 1.8.19
@@ -315,9 +314,8 @@ test_files:
315
314
  - spec/dummy/config/initializers/wrap_parameters.rb
316
315
  - spec/dummy/config/locales/en.yml
317
316
  - spec/dummy/config/routes.rb
318
- - spec/dummy/db/development.sqlite3
317
+ - spec/dummy/db/.gitkeep
319
318
  - spec/dummy/db/schema.rb
320
- - spec/dummy/db/test.sqlite3
321
319
  - spec/dummy/lib/assets/.gitkeep
322
320
  - spec/dummy/log/.gitkeep
323
321
  - spec/dummy/public/404.html
@@ -337,6 +335,7 @@ test_files:
337
335
  - spec/spec_helper.rb
338
336
  - spec/support/chapter.rb
339
337
  - spec/support/document.rb
338
+ - spec/support/migrations.rb
340
339
  - spec/support/pdf_test.pdf
341
340
  - spec/support/pdf_test_1.pdf
342
341
  - spec/support/pdf_test_2.pdf
@@ -1,36 +0,0 @@
1
- class CreateTestResources < ActiveRecord::Migration
2
- def self.up
3
- create_table :documents do |t|
4
- t.string :name
5
- t.string :surname
6
- t.boolean :keep_alive
7
- t.timestamps
8
- end
9
-
10
- create_table :sections do |t|
11
- t.string :title
12
- t.integer :document_id
13
- t.timestamps
14
- end
15
-
16
- create_table :chapters do |t|
17
- t.string :title
18
- t.timestamps
19
- end
20
-
21
- create_table :users do |t|
22
- t.string :email
23
- t.string :firstname
24
- t.string :lastname
25
- t.timestamps
26
- end
27
-
28
- end
29
-
30
- def self.down
31
- drop_table :users
32
- drop_table :chapters
33
- drop_table :sections
34
- drop_table :documents
35
- end
36
- end
Binary file
Binary file