active_metadata 0.7.0 → 0.7.1

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.
Files changed (30) hide show
  1. data/app/controllers/active_metadata/attachments_controller.rb +1 -1
  2. data/app/controllers/active_metadata/notes_controller.rb +1 -1
  3. data/app/controllers/active_metadata/stream_controller.rb +9 -0
  4. data/app/models/active_metadata/attachment.rb +7 -2
  5. data/app/models/active_metadata/note.rb +11 -0
  6. data/app/views/active_metadata/attachments/_attachment.html.erb +1 -1
  7. data/app/views/active_metadata/attachments/create.js.erb +0 -0
  8. data/app/views/active_metadata/notes/create.js.erb +0 -0
  9. data/config/routes.rb +17 -16
  10. data/db/development.sqlite3 +0 -0
  11. data/db/migrate/01_create_test_resources.rb +8 -2
  12. data/db/migrate/02_active_metadata_migrations.rb +2 -0
  13. data/lib/active_metadata/base.rb +2 -2
  14. data/lib/active_metadata/persistence/attachment.rb +12 -4
  15. data/lib/active_metadata/persistence/note.rb +10 -3
  16. data/lib/active_metadata/stream.rb +50 -0
  17. data/lib/active_metadata/version.rb +1 -1
  18. data/spec/controllers/active_metadata/attachments_controller_spec.rb +38 -20
  19. data/spec/controllers/active_metadata/notes_controller_spec.rb +23 -7
  20. data/spec/controllers/active_metadata/stream_controller_spec.rb +22 -0
  21. data/spec/dummy/db/development.sqlite3 +0 -0
  22. data/spec/dummy/db/schema.rb +8 -0
  23. data/spec/dummy/db/test.sqlite3 +0 -0
  24. data/spec/lib/attachments_spec.rb +18 -2
  25. data/spec/lib/notes_spec.rb +20 -0
  26. data/spec/lib/{streamable_spec.rb → stream_spec.rb} +18 -42
  27. data/spec/routing/active_metadata/stream_controller_routing_spec.rb +7 -2
  28. data/spec/support/chapter.rb +4 -0
  29. metadata +13 -8
  30. data/lib/active_metadata/streamable.rb +0 -40
@@ -12,7 +12,7 @@ module ActiveMetadata
12
12
 
13
13
  def create
14
14
  @document = params[:model_name].to_class.find(params[:model_id])
15
- @document.save_attachment_for(params[:field_name], params[:file])
15
+ @document.save_attachment_for(params[:field_name], params[:file], params[:starred], params[:group])
16
16
 
17
17
  #todo: if errors send back the correct answer
18
18
  respond_to do |format|
@@ -22,7 +22,7 @@ module ActiveMetadata
22
22
 
23
23
  def create
24
24
  @document = params[:model_name].to_class.find(params[:model_id])
25
- @document.create_note_for(params[:field_name], params[:note])
25
+ @document.create_note_for(params[:field_name], params[:note], params[:starred], params[:group])
26
26
  respond_to do |format|
27
27
  # TODO redirect to edit
28
28
  format.js
@@ -8,5 +8,14 @@ module ActiveMetadata
8
8
  @stream = @document.stream_for params[:field_name]
9
9
  end
10
10
 
11
+ def index_by_group
12
+ @stream = ActiveMetadata::Stream.by_group params[:group], starred_condition
13
+ render :index
14
+ end
15
+
16
+ private
17
+ def starred_condition
18
+ params[:starred].nil? ? nil : {:starred => true}
19
+ end
11
20
  end
12
21
  end
@@ -2,7 +2,6 @@ module ActiveMetadata
2
2
 
3
3
  class Attachment < ActiveRecord::Base
4
4
  self.table_name = "active_metadata_attachments"
5
-
6
5
  include ::Paperclip
7
6
  include ::Paperclip::Glue
8
7
 
@@ -21,7 +20,13 @@ module ActiveMetadata
21
20
  Paperclip.interpolates :document_class do |attachment, style|
22
21
  attachment.instance.document_class
23
22
  end
23
+ class << self
24
24
 
25
+ def by_group(group, *args)
26
+ options = args.extract_options!
27
+ order_by = options.delete(:order_by) || "created_at DESC"
28
+ ActiveMetadata::Attachment.all(:conditions => options.merge(:group => group), :order => order_by)
29
+ end
30
+ end
25
31
  end
26
-
27
32
  end
@@ -1,5 +1,16 @@
1
1
  module ActiveMetadata
2
2
  class Note < ActiveRecord::Base
3
3
  self.table_name = "active_metadata_notes"
4
+
5
+ class << self
6
+
7
+ def by_group(group, *args)
8
+ options = args.extract_options!
9
+ order_by = options.delete(:order_by) || "created_at DESC"
10
+ ActiveMetadata::Note.all(:conditions => options.merge(:group => group), :order => order_by)
11
+ end
12
+
13
+ end
14
+
4
15
  end
5
16
  end
@@ -3,5 +3,5 @@
3
3
  <%= "Path: #{attachment.attach.path}"%>
4
4
  <%= "Size: #{attachment.attach.size}"%>
5
5
  <%= "Updated at: #{attachment.attach.instance_read(:updated_at)}"%> <br/>
6
- <%= link_to "Elimina", active_metadata_destroy_attachment_path(@document.class,@document.id,attachment.label,attachment.id), :method => :delete, :remote => true %>
6
+ <%= link_to "Elimina", active_metadata_destroy_attachment_path(attachment.document_class, attachment.document_id,attachment.label,attachment.id), :method => :delete, :remote => true %>
7
7
  </li>
File without changes
data/config/routes.rb CHANGED
@@ -3,29 +3,30 @@ Rails.application.routes.draw do
3
3
  namespace :active_metadata do
4
4
  #stream
5
5
  get ":model_name/:model_id/:field_name/stream" => 'stream#index', :as => 'stream'
6
+ get "groups/:group/stream" => 'stream#index_by_group', :as => 'group_stream'
6
7
 
7
8
  #notes
8
- match ':model_name/:model_id/:field_name/notes' => 'notes#index', :via => :get, :as => "notes"
9
- match ':model_name/:model_id/:field_name/notes' => 'notes#create', :via => :post, :as => "create_note"
10
- match ':model_name/:model_id/:field_name/notes/:id' => 'notes#destroy', :via => :delete, :as => "destroy_note"
11
- match ':model_name/:model_id/:field_name/notes/:id/edit' => 'notes#edit', :via => :get, :as => "edit_note"
12
- match ':model_name/:model_id/:field_name/notes/:id' => 'notes#update', :via => :put, :as => "update_note"
13
- match ':model_name/:model_id/:field_name/notes/:id' => 'notes#show', :via => :get, :as => "show_note"
14
- match ':model_name/:model_id/:field_name/notes/starred' => 'notes#starred', :via => :get, :as => "starred_notes"
15
- match ':model_name/:model_id/:field_name/notes/:id/star' => 'notes#star', :via => :put, :as => "star_note"
16
- match ':model_name/:model_id/:field_name/notes/:id/unstar' => 'notes#unstar', :via => :put, :as => "unstar_note"
9
+ get ':model_name/:model_id/:field_name/notes' => 'notes#index', :as => "notes"
10
+ post ':model_name/:model_id/:field_name/notes' => 'notes#create', :as => "create_note"
11
+ delete ':model_name/:model_id/:field_name/notes/:id' => 'notes#destroy', :as => "destroy_note"
12
+ get ':model_name/:model_id/:field_name/notes/:id/edit' => 'notes#edit', :as => "edit_note"
13
+ put ':model_name/:model_id/:field_name/notes/:id' => 'notes#update', :as => "update_note"
14
+ get ':model_name/:model_id/:field_name/notes/:id' => 'notes#show', :as => "show_note"
15
+ get ':model_name/:model_id/:field_name/notes/starred' => 'notes#starred', :as => "starred_notes"
16
+ put ':model_name/:model_id/:field_name/notes/:id/star' => 'notes#star', :as => "star_note"
17
+ put ':model_name/:model_id/:field_name/notes/:id/unstar' => 'notes#unstar', :as => "unstar_note"
17
18
 
18
19
  #history
19
20
  match ':model_name/:model_id/:field_name/histories' => 'histories#index', :via => :get, :as => "histories"
20
21
 
21
22
  #attachments
22
- match ':model_name/:model_id/:field_name/attachments' => 'attachments#index', :via => :get, :as => "attachments"
23
- match ':model_name/:model_id/:field_name/attachments' => 'attachments#create', :via => :post, :as => "create_attachment"
24
- match ':model_name/:model_id/:field_name/attachments/:id' => 'attachments#destroy', :via => :delete, :as => "destroy_attachment"
25
- match ':model_name/:model_id/:field_name/attachments/:id' => 'attachments#update', :via => :put, :as => "update_attachment"
26
- match ':model_name/:model_id/:field_name/attachments/starred' => 'attachments#starred', :via => :get, :as => "starred_attachments"
27
- match ':model_name/:model_id/:field_name/attachments/:id/star' => 'attachments#star', :via => :put, :as => "star_attachment"
28
- match ':model_name/:model_id/:field_name/attachments/:id/unstar' => 'attachments#unstar', :via => :put, :as => "unstar_attachment"
23
+ get ':model_name/:model_id/:field_name/attachments' => 'attachments#index', :as => "attachments"
24
+ post ':model_name/:model_id/:field_name/attachments' => 'attachments#create', :as => "create_attachment"
25
+ delete ':model_name/:model_id/:field_name/attachments/:id' => 'attachments#destroy', :as => "destroy_attachment"
26
+ put ':model_name/:model_id/:field_name/attachments/:id' => 'attachments#update', :as => "update_attachment"
27
+ get ':model_name/:model_id/:field_name/attachments/starred' => 'attachments#starred', :as => "starred_attachments"
28
+ put ':model_name/:model_id/:field_name/attachments/:id/star' => 'attachments#star', :as => "star_attachments"
29
+ put ':model_name/:model_id/:field_name/attachments/:id/unstar' => 'attachments#unstar', :as => "unstar_attachments"
29
30
 
30
31
  #alerts
31
32
  get ':model_name/:model_id/:field_name/watchers' => 'watchers#index', :as => "watchers"
Binary file
@@ -13,6 +13,11 @@ class CreateTestResources < ActiveRecord::Migration
13
13
  t.timestamps
14
14
  end
15
15
 
16
+ create_table :chapters do |t|
17
+ t.string :title
18
+ t.timestamps
19
+ end
20
+
16
21
  create_table :users do |t|
17
22
  t.string :email
18
23
  t.string :firstname
@@ -23,8 +28,9 @@ class CreateTestResources < ActiveRecord::Migration
23
28
  end
24
29
 
25
30
  def self.down
26
- drop_table :documents
27
- drop_table :sections
28
31
  drop_table :users
32
+ drop_table :chapters
33
+ drop_table :sections
34
+ drop_table :documents
29
35
  end
30
36
  end
@@ -9,6 +9,7 @@ class ActiveMetadataMigrations < ActiveRecord::Migration
9
9
  t.integer :created_by
10
10
  t.integer :updated_by
11
11
  t.boolean :starred
12
+ t.string :group
12
13
  t.timestamps
13
14
  end
14
15
 
@@ -43,6 +44,7 @@ class ActiveMetadataMigrations < ActiveRecord::Migration
43
44
  t.integer :attach_file_size
44
45
  t.datetime :attach_updated_at
45
46
  t.boolean :starred
47
+ t.string :group
46
48
  t.timestamps
47
49
  end
48
50
 
@@ -7,7 +7,7 @@ module ActiveMetadata
7
7
  module Base
8
8
 
9
9
  require 'active_metadata/helpers'
10
- require 'active_metadata/streamable'
10
+ require 'active_metadata/stream'
11
11
  require 'paperclip'
12
12
  require 'active_metadata/persistence/persistence'
13
13
 
@@ -29,7 +29,7 @@ module ActiveMetadata
29
29
 
30
30
  include ActiveMetadata::Base::InstanceMethods
31
31
  include ActiveMetadata::Persistence
32
- include ActiveMetadata::Streamable
32
+ include ActiveMetadata::Stream
33
33
 
34
34
  end
35
35
 
@@ -6,9 +6,16 @@ module ActiveMetadata::Persistence::Attachment
6
6
 
7
7
  module InstanceMethods
8
8
 
9
- def save_attachment_for(field, file, starred=false)
10
- attachment = ActiveMetadata::Attachment.create! :document_class => metadata_class, :document_id => metadata_id, :label => field, :attach => file,
11
- :starred => starred, :created_by => current_user_id
9
+ def save_attachment_for(field, file, starred=false, group=nil)
10
+ attachment = ActiveMetadata::Attachment.create!(
11
+ :document_class => metadata_class,
12
+ :document_id => metadata_id,
13
+ :label => field,
14
+ :attach => file,
15
+ :starred => !!starred,
16
+ :created_by => current_user_id,
17
+ :group => group)
18
+
12
19
  reload_attachments_cache_for field
13
20
  self.send(:send_notification, field, "", attachment.attach.original_filename, :attachment_message, current_user_id)
14
21
  end
@@ -29,11 +36,12 @@ module ActiveMetadata::Persistence::Attachment
29
36
 
30
37
  def update_attachment(id, newfile, starred=nil)
31
38
  a = ActiveMetadata::Attachment.find(id)
39
+ puts a
32
40
  old_filename = a.attach.original_filename
33
41
  a.attach = newfile
34
42
  a.updated_by = current_user_id
35
43
  a.starred = starred if !starred.nil?
36
- a.save
44
+ a.save!
37
45
  new_filename = a.attach.original_filename
38
46
 
39
47
  reload_attachments_cache_for a.label
@@ -1,13 +1,20 @@
1
1
  module ActiveMetadata::Persistence::Note
2
2
 
3
- def self.included(receiver)
3
+ def self.included receiver
4
4
  receiver.send :include, InstanceMethods
5
5
  end
6
6
 
7
7
  module InstanceMethods
8
8
 
9
- def create_note_for(field, note, starred=false)
10
- ActiveMetadata::Note.create! :document_id => metadata_id, :document_class => metadata_class, :label => field.to_s, :note => note, :created_by => current_user_id, :starred => starred
9
+ def create_note_for(field, note, starred=nil, group=nil)
10
+ ActiveMetadata::Note.create!(
11
+ :document_id => metadata_id,
12
+ :document_class => metadata_class,
13
+ :label => field.to_s,
14
+ :note => note,
15
+ :created_by => current_user_id,
16
+ :starred => !!starred,
17
+ :group => group)
11
18
 
12
19
  reload_notes_cache_for field
13
20
  self.send(:send_notification, field, "", note, :note_message, current_user_id)
@@ -0,0 +1,50 @@
1
+ module ActiveMetadata
2
+ module Stream
3
+
4
+ def self.included(base)
5
+ base.send :include, InstanceMethods
6
+ end
7
+ class << self
8
+ # same as #stream_for but not filtered by field
9
+ def by_group group, *args
10
+ options = args.extract_options!
11
+ order_by = options.delete(:order_by) || :created_at
12
+ sort_stream(collect_stream_items_by_group(group, options), order_by)
13
+ end
14
+
15
+ def sort_stream stream, order_by
16
+ stream.sort { |a, b| a.send(order_by) <=> b.send(order_by) }
17
+ end
18
+
19
+ def collect_stream_items_by_group group, options
20
+ res = []
21
+ ActiveMetadata::CONFIG['streamables'].each do |model|
22
+ res.concat "ActiveMetadata::#{model.to_s.capitalize}".to_class.send(:by_group, group, options).collect { |el| el }
23
+ end
24
+ res
25
+ end
26
+ end
27
+
28
+ module InstanceMethods
29
+
30
+ # return the streamables items by field in an ordered array
31
+ # ActiveMetadata::CONFIG['streamables'] defines what models will be retrieved
32
+ def stream_for(field, order_by = :created_at)
33
+ ActiveMetadata::Stream.sort_stream(collect_stream_data(field), order_by)
34
+ end
35
+
36
+ def collect_stream_data field
37
+ res = []
38
+ ActiveMetadata::CONFIG['streamables'].each do |model|
39
+ res.concat self.send(stream_collect_method(model.to_s), field).collect { |el| el }
40
+ end
41
+ res
42
+ end
43
+
44
+ def stream_collect_method model
45
+ model.to_s == 'note' ? 'notes_for' : 'attachments_for'
46
+ end
47
+
48
+ end
49
+ end
50
+ end
@@ -1,3 +1,3 @@
1
1
  module ActiveMetadata
2
- VERSION = "0.7.0"
2
+ VERSION = "0.7.1"
3
3
  end
@@ -2,36 +2,54 @@ require 'spec_helper'
2
2
 
3
3
  describe ActiveMetadata::AttachmentsController do
4
4
 
5
- context "given 2 attachments for @document#name" do
5
+ render_views
6
+
7
+ before(:each) do
8
+ @document = Document.create! { |d| d.name = "John" }
9
+ end
6
10
 
7
- render_views
11
+
12
+ describe "GET 'index'" do
8
13
 
9
14
  before(:each) do
10
- @document = Document.create! { |d| d.name = "John" }
11
15
  (1..2).each do |i|
12
- @document.save_attachment_for(:name,test_pdf("pdf_test_#{i}"))
13
- end
16
+ @document.save_attachment_for(:name, test_pdf("pdf_test_#{i}"))
17
+ end
14
18
  end
15
19
 
16
- describe "GET 'index'" do
20
+ it "should success" do
21
+ get 'index', :model_name => 'document', :model_id => @document.id, :field_name => 'name'
22
+ response.should be_success
23
+ end
17
24
 
18
- it "should success" do
19
- get 'index', :model_name => 'document', :model_id => @document.id, :field_name => 'name'
20
- response.should be_success
21
- end
25
+ it "should assign attachments" do
26
+ get 'index', :model_name => 'document', :model_id => @document.id, :field_name => 'name'
27
+ assigns(:attachments).should_not be_nil
28
+ assigns(:attachments).size.should eq 2
29
+ end
22
30
 
23
- it "should assign attachments" do
24
- get 'index', :model_name => 'document', :model_id => @document.id, :field_name => 'name'
25
- assigns(:attachments).should_not be_nil
26
- assigns(:attachments).size.should eq 2
27
- end
31
+ it "should display 3 notes" do
32
+ get 'index', :model_name => 'document', :model_id => @document.id, :field_name => 'name'
33
+ response.body.should match(/pdf_test_1.pdf/)
34
+ response.body.should match(/pdf_test_2.pdf/)
35
+ end
28
36
 
29
- it "should display 3 notes" do
30
- get 'index', :model_name => 'document', :model_id => @document.id, :field_name => 'name'
31
- response.body.should match(/pdf_test_1.pdf/)
32
- response.body.should match(/pdf_test_2.pdf/)
33
- end
37
+ end
38
+
39
+ describe "#create" do
40
+
41
+ it "should create an attachment for a passed group" do
42
+ post :create, :model_name => 'document', :model_id => @document.id, :field_name => 'name', :group => "my_group", :format => :js,
43
+ :file => test_pdf
44
+ response.should be_success
45
+ ActiveMetadata::Stream.by_group("my_group").count.should eq 1
46
+ end
34
47
 
48
+ it "should create a starred attachment" do
49
+ post :create, :model_name => 'document', :model_id => @document.id, :field_name => 'name', :starred => true, :format => :js,
50
+ :file => test_pdf
51
+ response.should be_success
52
+ @document.attachments_for(:name).last.should be_starred
35
53
  end
36
54
 
37
55
  end
@@ -2,19 +2,21 @@ require 'spec_helper'
2
2
 
3
3
  describe ActiveMetadata::NotesController do
4
4
 
5
- context "given 3 notes @document#name" do
6
-
7
- render_views
8
5
 
9
6
  before(:each) do
10
7
  @document = Document.create! { |d| d.name = "John" }
11
- (1..3).each do |i|
12
- @document.create_note_for(:name, "note#{i}")
13
- end
14
8
  end
15
9
 
16
10
  describe "GET 'index'" do
17
11
 
12
+ render_views
13
+
14
+ before(:each) do
15
+ (1..3).each do |i|
16
+ @document.create_note_for(:name, "note#{i}")
17
+ end
18
+ end
19
+
18
20
  it "should success" do
19
21
  get 'index', :model_name => 'document', :model_id => @document.id, :field_name => 'name'
20
22
  response.should be_success
@@ -35,7 +37,21 @@ describe ActiveMetadata::NotesController do
35
37
 
36
38
  end
37
39
 
38
- end
40
+ describe "#create" do
41
+
42
+ it "should create a note for a passed group" do
43
+ post :create, :model_name => 'document', :model_id => @document.id, :field_name => 'name', :group => "my_group", :format => :js
44
+ response.should be_success
45
+ ActiveMetadata::Stream.by_group("my_group").count.should eq 1
46
+ end
47
+
48
+ it "should create a starred note" do
49
+ post :create, :model_name => 'document', :model_id => @document.id, :field_name => 'name', :starred => true, :format => :js
50
+ response.should be_success
51
+ @document.notes_for(:name).last.should be_starred
52
+ end
53
+
54
+ end
39
55
 
40
56
 
41
57
  end
@@ -37,6 +37,28 @@ describe ActiveMetadata::StreamController do
37
37
  response.body.should match(/nota per name john/)
38
38
  end
39
39
 
40
+ describe "GET 'index by group'" do
41
+ before(:each) do
42
+ @chapter = Chapter.create! { |d| d.title = "Cool!" }
43
+ @document.save_attachment_for(:name, test_pdf("pdf_test_1"), false, 'my_group')
44
+ @chapter.save_attachment_for(:title, test_pdf("pdf_test_2"), true, 'my_group')
45
+ @document.create_note_for(:name, "grouped nota per name john", true, 'your_group')
46
+ end
47
+
48
+ it "should return the stream of a particular group" do
49
+ get 'index_by_group', :group => 'my_group'
50
+ response.body.should match(/pdf_test_1.pdf/)
51
+ response.body.should match(/pdf_test_2.pdf/)
52
+ response.body.should_not match(/grouped nota/)
53
+ end
54
+
55
+ it "should return the stream of a particular group filtered by starred" do
56
+ get 'index_by_group', :group => 'my_group', :starred => true
57
+ response.body.should_not match(/pdf_test_1.pdf/)
58
+ response.body.should match(/pdf_test_2.pdf/)
59
+ response.body.should_not match(/grouped nota/)
60
+ end
61
+ end
40
62
  end
41
63
 
42
64
  end
Binary file
@@ -25,6 +25,7 @@ ActiveRecord::Schema.define(:version => 2) do
25
25
  t.integer "attach_file_size"
26
26
  t.datetime "attach_updated_at"
27
27
  t.boolean "starred"
28
+ t.string "group"
28
29
  t.datetime "created_at", :null => false
29
30
  t.datetime "updated_at", :null => false
30
31
  end
@@ -57,6 +58,7 @@ ActiveRecord::Schema.define(:version => 2) do
57
58
  t.integer "created_by"
58
59
  t.integer "updated_by"
59
60
  t.boolean "starred"
61
+ t.string "group"
60
62
  t.datetime "created_at", :null => false
61
63
  t.datetime "updated_at", :null => false
62
64
  end
@@ -80,6 +82,12 @@ ActiveRecord::Schema.define(:version => 2) do
80
82
  add_index "active_metadata_watchers", ["label"], :name => "index_active_metadata_watchers_on_label"
81
83
  add_index "active_metadata_watchers", ["owner_id"], :name => "index_active_metadata_watchers_on_owner_id"
82
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
+
83
91
  create_table "documents", :force => true do |t|
84
92
  t.string "name"
85
93
  t.string "surname"
Binary file
@@ -187,11 +187,27 @@ describe ActiveMetadata do
187
187
  starred.count.should eq 2
188
188
  starred.find{|att| att.attach.instance_read(:file_name) == "pdf_test_2.pdf"}.should be_nil
189
189
  end
190
-
191
190
  end
192
191
 
192
+ describe "#group" do
193
+ it "should save the associated group when specified" do
194
+ @document.save_attachment_for(:name, @attachment, false, 'my_group')
195
+ @document.attachments_for(:name).last.group.should eq 'my_group'
196
+ end
193
197
 
198
+ describe ".by_group" do
194
199
 
195
- end
200
+ it "should return all the starred notes of a particular group" do
201
+ @document.save_attachment_for :name, @attachment, false, 'my_group'
202
+ @document.save_attachment_for :title, @attachment, true, 'my_group'
203
+ @document.save_attachment_for :name, @attachment, true, 'my_group'
204
+ @document.save_attachment_for :name, @attachment, false, 'your_group'
205
+ @document.save_attachment_for :name, @attachment, true, 'your_group'
196
206
 
207
+ ActiveMetadata::Attachment.by_group('my_group', :starred => true).count.should eq 2
208
+ ActiveMetadata::Attachment.by_group('your_group', :starred => true).count.should eq 1
209
+ end
210
+ end
211
+ end
212
+ end
197
213
  end
@@ -217,6 +217,26 @@ describe ActiveMetadata do
217
217
  end
218
218
 
219
219
  end
220
+ describe "#group" do
221
+ it "should save the associated group when specified" do
222
+ @document.create_note_for :name, "starred note for name", false, 'my_group'
223
+ @document.notes_for(:name).last.group.should eq 'my_group'
224
+ end
225
+
226
+ describe "notes_by_group" do
227
+ it "should return all the starred notes of a particular group" do
228
+ @document.create_note_for :name, "starred note for name", false, 'my_group'
229
+ @document.create_note_for :title, "to be returned", true, 'my_group'
230
+ @document.create_note_for :name, "to be returned", true, 'my_group'
231
+ @document.create_note_for :name, "starred note for name", false, 'your_group'
232
+ @document.create_note_for :name, "starred note for name", true, 'your_group'
233
+
234
+ ActiveMetadata::Note.by_group('my_group', :starred => true).count.should eq 2
235
+ ActiveMetadata::Note.by_group('your_group', :starred => true, :order_by => "created_at ASC").count.should eq 1
236
+ end
237
+ end
238
+
239
+ end
220
240
 
221
241
  end
222
242
  end
@@ -1,16 +1,6 @@
1
1
  require "spec_helper"
2
2
 
3
- describe ActiveMetadata::Streamable do
4
-
5
- describe "module" do
6
-
7
- it "should respond to stream_for" do
8
- ActiveMetadata::Streamable.instance_eval do
9
- self.instance_methods.grep(/stream_for/).size.should == 1
10
- end
11
- end
12
-
13
- end
3
+ describe ActiveMetadata::Stream do
14
4
 
15
5
  describe "stream_for" do
16
6
 
@@ -40,12 +30,6 @@ describe ActiveMetadata::Streamable do
40
30
 
41
31
  describe "collect_data" do
42
32
 
43
- it "should exists as private method" do
44
- ActiveMetadata::Streamable.instance_eval do
45
- self.private_instance_methods.grep(/collect_stream_data/).size.should == 1
46
- end
47
- end
48
-
49
33
  it "should return an array" do
50
34
  res = @document.send(:collect_stream_data, :name)
51
35
  res.should be_kind_of Array
@@ -69,14 +53,7 @@ describe ActiveMetadata::Streamable do
69
53
 
70
54
  describe "sort_stream" do
71
55
 
72
- it "should exists as private method" do
73
- ActiveMetadata::Streamable.instance_eval do
74
- self.private_instance_methods.grep(/sort_stream/).size.should == 1
75
- end
76
- end
77
-
78
-
79
- it "should sort the stream by updated_at DESC" do
56
+ it "should sort the stream by created_at DESC" do
80
57
  @document.save_attachment_for(:surname,test_pdf("pdf_test_1"))
81
58
  sleep 2.seconds
82
59
  @document.create_note_for(:surname, "surname note")
@@ -84,14 +61,14 @@ describe ActiveMetadata::Streamable do
84
61
  @document.save_attachment_for(:surname,test_pdf("pdf_test_2"))
85
62
 
86
63
  stream = @document.send(:collect_stream_data, :surname)
87
- res = @document.send(:sort_stream, stream, :updated_at)
64
+ res = ActiveMetadata::Stream.sort_stream(stream, :updated_at)
88
65
 
89
- res[0].attach_file_name.should eq 'pdf_test_2.pdf'
66
+ res[0].attach_file_name.should eq 'pdf_test_1.pdf'
90
67
  res[1].note.should eq 'surname note'
91
- res[2].attach_file_name.should eq 'pdf_test_1.pdf'
68
+ res[2].attach_file_name.should eq 'pdf_test_2.pdf'
92
69
  end
93
70
 
94
- it "should sort the stream by updated_at DESC" do
71
+ it "should sort the stream by created_at DESC" do
95
72
  @document.create_note_for(:surname, "surname note")
96
73
  sleep 2.seconds
97
74
  @document.save_attachment_for(:surname,test_pdf("pdf_test_2"))
@@ -99,11 +76,11 @@ describe ActiveMetadata::Streamable do
99
76
  @document.save_attachment_for(:surname,test_pdf("pdf_test_1"))
100
77
 
101
78
  stream = @document.send(:collect_stream_data, :surname)
102
- res = @document.send(:sort_stream, stream, :created_at)
79
+ res = ActiveMetadata::Stream.sort_stream(stream, :created_at)
103
80
 
104
- res[0].attach_file_name.should eq 'pdf_test_1.pdf'
81
+ res[0].note.should eq 'surname note'
105
82
  res[1].attach_file_name.should eq 'pdf_test_2.pdf'
106
- res[2].note.should eq 'surname note'
83
+ res[2].attach_file_name.should eq 'pdf_test_1.pdf'
107
84
  end
108
85
 
109
86
  end
@@ -131,31 +108,30 @@ describe ActiveMetadata::Streamable do
131
108
 
132
109
  end
133
110
 
134
- describe "#stream_all_starred" do
111
+ describe "#stream_all_starred_by_group" do
135
112
 
136
113
  context "given 2 notes (only one starred) and 2 attachments (only one starred)" do
137
114
 
138
115
  before(:each) do
139
116
  @document = Document.create! { |d| d.name = "John" }
140
117
  (1..2).each do |i|
141
- @document.save_attachment_for(:name,test_pdf("pdf_test_#{i}"), i.odd? )
142
- @document.create_note_for(:name, "note_#{i}", i.odd? )
118
+ @document.save_attachment_for(:name,test_pdf("pdf_test_#{i}"), i.odd?, 'my_group' )
119
+ @document.create_note_for(:name, "note_#{i}", i.odd?, 'my_group' )
143
120
  end
144
121
  end
145
122
 
146
123
  it "should return 2 elements" do
147
- @document.stream_all_starred.count.should eq 2
124
+ ActiveMetadata::Stream.by_group('my_group', :starred => true).count.should eq 2
148
125
  end
149
126
 
150
127
  it "should return only the starred items" do
151
- @document.stream_all_starred.collect{|el| el.starred? }.count.should eq 2
128
+ ActiveMetadata::Stream.by_group('my_group', :starred => true).collect{|el| el.starred? }.count.should eq 2
152
129
  end
153
130
 
154
- it "should return the starred stream ordered by UPDATED_AT DESC" do
155
- starred = @document.stream_all_starred
156
- starred.first.should be_kind_of ActiveMetadata::Note
157
- @document.update_attachment starred.last.id, starred.last.attach.to_file
158
- @document.stream_all_starred.first.should be_kind_of ActiveMetadata::Attachment
131
+ it "should return the starred stream ordered by created_at DESC" do
132
+ items = ActiveMetadata::Stream.by_group('my_group', :starred => true)
133
+ items.first.should be_kind_of ActiveMetadata::Attachment
134
+ ActiveMetadata::Stream.by_group('my_group', :starred => true).first.id.should eq items.first.id
159
135
  end
160
136
 
161
137
  end
@@ -7,8 +7,13 @@ describe ActiveMetadata::StreamController do
7
7
  before(:each) { @routes = Rails.application.routes }
8
8
 
9
9
  it "recognizes and generate :model_name/:model_id/:field_name/stream" do
10
- { :get => "/active_metadata/model/12/field/stream" }.should route_to(:controller => 'active_metadata/stream', :action => 'index', :model_name => 'model', :model_id => '12',
11
- :field_name =>'field')
10
+ { :get => "/active_metadata/model/12/field/stream" }.should route_to(
11
+ :controller => 'active_metadata/stream', :action => 'index', :model_name => 'model', :model_id => '12', :field_name =>'field')
12
+ end
13
+
14
+ it "recognizes and generate group/:group/stream" do
15
+ { :get => "/active_metadata/groups/my_group/stream" }.should route_to(
16
+ :controller => 'active_metadata/stream', :action => 'index_by_group', :group =>'my_group')
12
17
  end
13
18
 
14
19
 
@@ -0,0 +1,4 @@
1
+ # encoding: utf-8
2
+ class Chapter < ActiveRecord::Base
3
+ acts_as_metadata
4
+ 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.0
4
+ version: 0.7.1
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-04-17 00:00:00.000000000 Z
13
+ date: 2012-05-03 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rails
@@ -145,10 +145,12 @@ files:
145
145
  - app/models/active_metadata/note.rb
146
146
  - app/models/active_metadata/watcher.rb
147
147
  - app/views/active_metadata/attachments/_attachment.html.erb
148
+ - app/views/active_metadata/attachments/create.js.erb
148
149
  - app/views/active_metadata/attachments/index.html.erb
149
150
  - app/views/active_metadata/histories/index.html.erb
150
151
  - app/views/active_metadata/notes/_form.html.erb
151
152
  - app/views/active_metadata/notes/_note.html.erb
153
+ - app/views/active_metadata/notes/create.js.erb
152
154
  - app/views/active_metadata/notes/edit.html.erb
153
155
  - app/views/active_metadata/notes/index.html.erb
154
156
  - app/views/active_metadata/notes/index.js.erb
@@ -163,6 +165,7 @@ files:
163
165
  - config/cucumber.yml
164
166
  - config/database.yml
165
167
  - config/routes.rb
168
+ - db/development.sqlite3
166
169
  - db/migrate/01_create_test_resources.rb
167
170
  - db/migrate/02_active_metadata_migrations.rb
168
171
  - lib/active_metadata/base.rb
@@ -174,7 +177,7 @@ files:
174
177
  - lib/active_metadata/persistence/note.rb
175
178
  - lib/active_metadata/persistence/persistence.rb
176
179
  - lib/active_metadata/persistence/watcher.rb
177
- - lib/active_metadata/streamable.rb
180
+ - lib/active_metadata/stream.rb
178
181
  - lib/active_metadata/version.rb
179
182
  - lib/active_metadata.rb
180
183
  - lib/tasks/active_metadata_tasks.rake
@@ -233,10 +236,11 @@ files:
233
236
  - spec/lib/concurrency_spec.rb
234
237
  - spec/lib/history_spec.rb
235
238
  - spec/lib/notes_spec.rb
236
- - spec/lib/streamable_spec.rb
239
+ - spec/lib/stream_spec.rb
237
240
  - spec/lib/watchers_spec.rb
238
241
  - spec/routing/active_metadata/stream_controller_routing_spec.rb
239
242
  - spec/spec_helper.rb
243
+ - spec/support/chapter.rb
240
244
  - spec/support/document.rb
241
245
  - spec/support/pdf_test.pdf
242
246
  - spec/support/pdf_test_1.pdf
@@ -259,7 +263,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
259
263
  version: '0'
260
264
  segments:
261
265
  - 0
262
- hash: -2284098148828860317
266
+ hash: -1818248447479050482
263
267
  required_rubygems_version: !ruby/object:Gem::Requirement
264
268
  none: false
265
269
  requirements:
@@ -268,10 +272,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
268
272
  version: '0'
269
273
  segments:
270
274
  - 0
271
- hash: -2284098148828860317
275
+ hash: -1818248447479050482
272
276
  requirements: []
273
277
  rubyforge_project: active_metadata
274
- rubygems_version: 1.8.19
278
+ rubygems_version: 1.8.21
275
279
  signing_key:
276
280
  specification_version: 3
277
281
  summary: Add metadata to fields in an active record model
@@ -328,10 +332,11 @@ test_files:
328
332
  - spec/lib/concurrency_spec.rb
329
333
  - spec/lib/history_spec.rb
330
334
  - spec/lib/notes_spec.rb
331
- - spec/lib/streamable_spec.rb
335
+ - spec/lib/stream_spec.rb
332
336
  - spec/lib/watchers_spec.rb
333
337
  - spec/routing/active_metadata/stream_controller_routing_spec.rb
334
338
  - spec/spec_helper.rb
339
+ - spec/support/chapter.rb
335
340
  - spec/support/document.rb
336
341
  - spec/support/pdf_test.pdf
337
342
  - spec/support/pdf_test_1.pdf
@@ -1,40 +0,0 @@
1
- module ActiveMetadata::Streamable
2
-
3
- # return the streamables items by field in an ordered array
4
- # ActiveMetadata::CONFIG['streamables'] defines what models will be retrieved
5
- def stream_for(field, order_by = :updated_at)
6
- sort_stream(collect_stream_data(field), order_by)
7
- end
8
-
9
- # same as #stream_for but not filtered by field
10
- def stream_all_starred order_by = :updated_at
11
- sort_stream(collect_starred_stream_data, order_by)
12
- end
13
-
14
- private
15
- def sort_stream stream, order_by
16
- stream.sort{ |a,b| b.send(order_by) <=> a.send(order_by) }
17
- end
18
-
19
- def collect_stream_data field
20
- res = []
21
- ActiveMetadata::CONFIG['streamables'].each do |model|
22
- res.concat self.send(stream_collect_method(model.to_s),field).collect { |el| el }
23
- end
24
- res
25
- end
26
-
27
- def collect_starred_stream_data
28
- res = []
29
- ActiveMetadata::CONFIG['streamables'].each do |model|
30
- res.concat self.send("starred_#{model.to_s.pluralize}".to_sym).collect { |el| el }
31
- end
32
- res
33
- end
34
-
35
- def stream_collect_method model
36
- model.to_s == 'note' ? 'notes_for' : 'attachments_for'
37
- end
38
-
39
-
40
- end