active_metadata 0.0.2 → 0.1.9
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/app/controllers/active_metadata/attachments_controller.rb +47 -0
- data/app/controllers/active_metadata/histories_controller.rb +16 -0
- data/app/controllers/active_metadata/notes_controller.rb +61 -0
- data/app/controllers/active_metadata/watchers_controller.rb +34 -0
- data/app/helpers/application_helper.rb +2 -0
- data/app/views/active_metadata/attachments/index.html.erb +11 -0
- data/app/views/active_metadata/histories/index.html.erb +5 -0
- data/app/views/active_metadata/notes/_form.html.erb +23 -0
- data/app/views/active_metadata/notes/edit.html.erb +8 -0
- data/app/views/active_metadata/notes/index.html.erb +11 -0
- data/app/views/active_metadata/notes/index.js.erb +1 -0
- data/app/views/active_metadata/notes/show.html.erb +9 -0
- data/app/views/active_metadata/watchers/create.js.erb +1 -0
- data/app/views/active_metadata/watchers/destroy.js.erb +1 -0
- data/app/views/active_metadata/watchers/index.html.erb +5 -0
- data/config/active_metadata.yml +17 -0
- data/config/application.rb +0 -0
- data/config/database.yml +11 -0
- data/config/initializers/inflections.rb +7 -0
- data/config/mongoid.yml +28 -0
- data/config/routes.rb +30 -0
- data/db/development.sqlite3 +0 -0
- data/db/migrate/001_create_document.rb +12 -0
- data/db/migrate/002_add_surname_to_document.rb +9 -0
- data/db/migrate/003_create_section.rb +13 -0
- data/db/migrate/004_create_user.rb +14 -0
- data/db/migrate/005_create_inboxes.rb +19 -0
- data/db/migrate/006_create_alert_message.rb +37 -0
- data/db/migrate/007_create_notes.rb +22 -0
- data/db/migrate/008_create_history.rb +20 -0
- data/db/migrate/009_create_watcher.rb +21 -0
- data/db/migrate/010_create_attachment.rb +26 -0
- data/db/migrate/011_add_created_by_to_histories.rb +10 -0
- data/db/test.sqlite3 +0 -0
- data/features/step_definitions/add_a_user_to_a_watcher_steps.rb +29 -0
- data/features/step_definitions/trigger_alert_on_modify_steps.rb +86 -0
- data/features/supports/file.txt +1 -0
- data/features/supports/initializer.rb +1 -0
- data/features/supports/updated_file.txt +1 -0
- data/features/watchlist/add_a_user_to_a_watcher.feature +19 -0
- data/features/watchlist/trigger_alert_on_modify.feature +90 -0
- data/lib/active_metadata/base.rb +59 -0
- data/lib/active_metadata/persistence/active_record/attachment.rb +42 -0
- data/lib/active_metadata/persistence/active_record/history.rb +22 -0
- data/lib/active_metadata/persistence/active_record/note.rb +45 -0
- data/lib/active_metadata/persistence/active_record/watcher.rb +45 -0
- data/lib/active_metadata/persistence/active_record.rb +26 -0
- data/lib/active_metadata/persistence/mongoid/attachment.rb +45 -0
- data/lib/active_metadata/persistence/mongoid/history.rb +26 -0
- data/lib/active_metadata/persistence/mongoid/note.rb +48 -0
- data/lib/active_metadata/persistence/mongoid/watcher.rb +43 -0
- data/lib/active_metadata/persistence/mongoid.rb +30 -0
- data/lib/active_metadata/persistence/persistence.rb +13 -0
- data/lib/active_metadata/railtie.rb +6 -0
- data/lib/active_metadata/version.rb +1 -1
- data/lib/active_metadata.rb +6 -12
- data/lib/application_controller.rb +28 -0
- data/lib/application_helper.rb +7 -0
- data/lib/engine.rb +29 -0
- data/lib/model/active_record/attachment.rb +18 -0
- data/lib/model/active_record/history.rb +2 -0
- data/lib/model/active_record/note.rb +2 -0
- data/lib/model/active_record/watcher.rb +4 -0
- data/lib/model/mongoid/active_meta.rb +6 -0
- data/lib/model/mongoid/attachment.rb +25 -0
- data/lib/model/mongoid/history.rb +9 -0
- data/lib/model/mongoid/label.rb +14 -0
- data/lib/model/mongoid/note.rb +10 -0
- data/lib/model/mongoid/watcher.rb +24 -0
- data/lib/rails/railties/tasks.rake +8 -0
- data/lib/rake/active_record_tasks.rb +77 -0
- data/lib/rake/task.rb +18 -0
- data/lib/templates/active_metadata.yml +17 -0
- data/lib/templates/active_metadata_migrations +67 -0
- data/lib/templates/mongoid.yml +20 -0
- data/spec/active_metadata_spec.rb +596 -0
- data/spec/benchmark_spec.rb +35 -0
- data/spec/controllers/metadata_controller_spec.rb +14 -0
- data/spec/spec_helper.rb +87 -0
- data/spec/support/document.rb +8 -0
- data/spec/support/pdf_test.pdf +0 -0
- data/spec/support/pdf_test_2.pdf +0 -0
- data/spec/support/section.rb +8 -0
- data/spec/support/user.rb +13 -0
- data/spec/support/watcher_notifier.rb +21 -0
- metadata +201 -19
- data/.gitignore +0 -4
- data/Gemfile +0 -4
- data/Rakefile +0 -1
- data/active_metadata.gemspec +0 -24
data/lib/active_metadata.rb
CHANGED
@@ -1,13 +1,7 @@
|
|
1
|
-
require "active_metadata/version"
|
2
|
-
require "mongo"
|
3
|
-
|
4
1
|
module ActiveMetadata
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
end
|
12
|
-
|
13
|
-
end
|
2
|
+
require 'engine' if defined?(Rails) && Rails::VERSION::MAJOR == 3
|
3
|
+
require 'active_metadata/base'
|
4
|
+
require "active_metadata/railtie" if defined?(Rails)
|
5
|
+
require 'application_controller'
|
6
|
+
require "active_metadata/version"
|
7
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# module ActiveMetadata
|
2
|
+
# ## Define ControllerMethods
|
3
|
+
# module Controller
|
4
|
+
# ## this one manages the usual self.included, klass_eval stuff
|
5
|
+
# extend ActiveSupport::Concern
|
6
|
+
#
|
7
|
+
# included do
|
8
|
+
# before_filter :test_controller_instance_method
|
9
|
+
# end
|
10
|
+
#
|
11
|
+
# module InstanceMethods
|
12
|
+
# def test_controller_instance_method
|
13
|
+
# puts "###### This text is coming from an application_controller before_filter that is being declared and triggered from inside the engine. This before_filter is automatically integrated in when the engine is installed into an app. Look inside lib/application_controller.rb to find it. ######"
|
14
|
+
# end
|
15
|
+
#
|
16
|
+
# # This method is available inside application_controller but it is not being
|
17
|
+
# # automatically executed. Notice the before_filter line above that is automatically
|
18
|
+
# # executing the first method.
|
19
|
+
# def second_controller_instance_method
|
20
|
+
# puts "###### This method is not automatically run inside application_controller, but it is available inside application_controller. To see this example add 'before_filter :second_controller_instance_method' at the top of your app's application_controller.rb ######"
|
21
|
+
# end
|
22
|
+
# end
|
23
|
+
# end
|
24
|
+
# end
|
25
|
+
#
|
26
|
+
# ::ActionController::Base.send :include, ActiveMetadata::Controller
|
27
|
+
#
|
28
|
+
#
|
data/lib/engine.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'active_metadata'
|
2
|
+
require 'rails'
|
3
|
+
require 'action_controller'
|
4
|
+
require 'application_helper'
|
5
|
+
|
6
|
+
module ActiveMetadata
|
7
|
+
class Engine < Rails::Engine
|
8
|
+
|
9
|
+
# Config defaults
|
10
|
+
config.mount_at = '/'
|
11
|
+
|
12
|
+
# Load rake tasks
|
13
|
+
rake_tasks do
|
14
|
+
load File.join(File.dirname(__FILE__), 'rails/railties/tasks.rake')
|
15
|
+
end
|
16
|
+
|
17
|
+
# Check the gem config
|
18
|
+
initializer "check config" do |app|
|
19
|
+
|
20
|
+
# make sure mount_at ends with trailing slash
|
21
|
+
config.mount_at += '/' unless config.mount_at.last == '/'
|
22
|
+
end
|
23
|
+
|
24
|
+
initializer "static assets" do |app|
|
25
|
+
app.middleware.use ::ActionDispatch::Static, "#{root}/public"
|
26
|
+
config.generators.orm = :active_record
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
class Attachment < ActiveRecord::Base
|
2
|
+
|
3
|
+
include ::Paperclip
|
4
|
+
include ::Paperclip::Glue
|
5
|
+
|
6
|
+
has_attached_file :attach,
|
7
|
+
:path => "#{ActiveMetadata::CONFIG['attachment_base_path']}/:document_id/:label/:id/:basename.:extension",
|
8
|
+
:url => "#{ActiveMetadata::CONFIG['attachment_base_url']}/:document_id/:label/:id/:basename.:extension"
|
9
|
+
|
10
|
+
Paperclip.interpolates :document_id do |attachment,style|
|
11
|
+
attachment.instance.document_id
|
12
|
+
end
|
13
|
+
|
14
|
+
Paperclip.interpolates :label do |attachment,style|
|
15
|
+
attachment.instance.label
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
class Attachment
|
2
|
+
include Mongoid::Document
|
3
|
+
include Mongoid::Paperclip
|
4
|
+
|
5
|
+
belongs_to :label
|
6
|
+
has_mongoid_attached_file :attach,
|
7
|
+
:path => "#{ActiveMetadata::CONFIG['attachment_base_path']}/:document_id/:label/:counter/:basename.:extension",
|
8
|
+
:url => "#{ActiveMetadata::CONFIG['attachment_base_url']}/:document_id/:label/:counter/:basename.:extension"
|
9
|
+
|
10
|
+
field :created_by, :type => Integer
|
11
|
+
field :counter, :type => Integer
|
12
|
+
|
13
|
+
Paperclip.interpolates :document_id do |attachment,style|
|
14
|
+
attachment.instance.label.active_meta.document_id
|
15
|
+
end
|
16
|
+
|
17
|
+
Paperclip.interpolates :label do |attachment,style|
|
18
|
+
attachment.instance.label.name
|
19
|
+
end
|
20
|
+
|
21
|
+
Paperclip.interpolates :counter do |attachment,style|
|
22
|
+
attachment.instance.counter
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
class Label
|
2
|
+
include Mongoid::Document
|
3
|
+
field :name
|
4
|
+
|
5
|
+
belongs_to :active_meta, :class_name => "ActiveMeta"
|
6
|
+
|
7
|
+
has_many :notes, :class_name => "Note"
|
8
|
+
has_many :histories, :class_name => "History"
|
9
|
+
has_many :attachments, :class_name => "Attachment"
|
10
|
+
has_many :watchers, :class_name => "Watcher"
|
11
|
+
|
12
|
+
index :name
|
13
|
+
|
14
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
class Watcher
|
2
|
+
include Mongoid::Document
|
3
|
+
|
4
|
+
belongs_to :label
|
5
|
+
|
6
|
+
field :owner_id, :type => Integer
|
7
|
+
|
8
|
+
def notify_changes(matched_label, old_value, new_value, model_class, model_id)
|
9
|
+
create_inbox_alert(User.find(owner_id).inbox, matched_label, old_value, new_value, model_class, model_id)
|
10
|
+
end
|
11
|
+
|
12
|
+
def create_inbox_alert(inbox, label, old_value, new_value, model_class, model_id)
|
13
|
+
Message.create! do |message|
|
14
|
+
message.label = label
|
15
|
+
message.model_class = model_class.to_s
|
16
|
+
message.model_id = model_id
|
17
|
+
message.alert_type = :model_value_changed
|
18
|
+
message.old_value = old_value
|
19
|
+
message.new_value = new_value
|
20
|
+
message.inbox_id = inbox.id
|
21
|
+
message.read = false
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
require 'logger'
|
3
|
+
require 'active_record'
|
4
|
+
|
5
|
+
namespace :db do
|
6
|
+
def create_database config
|
7
|
+
options = {:charset => 'utf8', :collation => 'utf8_unicode_ci'}
|
8
|
+
|
9
|
+
create_db = lambda do |config|
|
10
|
+
ActiveRecord::Base.establish_connection config.merge('database' => nil)
|
11
|
+
ActiveRecord::Base.connection.create_database config['database'], options
|
12
|
+
ActiveRecord::Base.establish_connection config
|
13
|
+
end
|
14
|
+
|
15
|
+
begin
|
16
|
+
create_db.call config
|
17
|
+
rescue Mysql::Error => sqlerr
|
18
|
+
if sqlerr.errno == 1405
|
19
|
+
print "#{sqlerr.error}. \nPlease provide the root password for your mysql installation\n>"
|
20
|
+
root_password = $stdin.gets.strip
|
21
|
+
|
22
|
+
grant_statement = <<-SQL
|
23
|
+
GRANT ALL PRIVILEGES ON #{config['database']}.*
|
24
|
+
TO '#{config['username']}'@'localhost'
|
25
|
+
IDENTIFIED BY '#{config['password']}' WITH GRANT OPTION;
|
26
|
+
SQL
|
27
|
+
|
28
|
+
create_db.call config.merge('database' => nil, 'username' => 'root', 'password' => root_password)
|
29
|
+
else
|
30
|
+
$stderr.puts sqlerr.error
|
31
|
+
$stderr.puts "Couldn't create database for #{config.inspect}, charset: utf8, collation: utf8_unicode_ci"
|
32
|
+
$stderr.puts "(if you set the charset manually, make sure you have a matching collation)" if config['charset']
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
task :environment do
|
38
|
+
DATABASE_ENV = ENV['DATABASE_ENV'] || 'development'
|
39
|
+
MIGRATIONS_DIR = ENV['MIGRATIONS_DIR'] || 'db/migrate'
|
40
|
+
end
|
41
|
+
|
42
|
+
task :configuration => :environment do
|
43
|
+
@config = YAML.load_file('config/database.yml')[DATABASE_ENV]
|
44
|
+
end
|
45
|
+
|
46
|
+
task :configure_connection => :configuration do
|
47
|
+
ActiveRecord::Base.establish_connection @config
|
48
|
+
ActiveRecord::Base.logger = Logger.new STDOUT if @config['logger']
|
49
|
+
end
|
50
|
+
|
51
|
+
desc 'Create the database from config/database.yml for the current DATABASE_ENV'
|
52
|
+
task :create => :configure_connection do
|
53
|
+
create_database @config
|
54
|
+
end
|
55
|
+
|
56
|
+
desc 'Drops the database for the current DATABASE_ENV'
|
57
|
+
task :drop => :configure_connection do
|
58
|
+
ActiveRecord::Base.connection.drop_database @config['database']
|
59
|
+
end
|
60
|
+
|
61
|
+
desc 'Migrate the database (options: VERSION=x, VERBOSE=false).'
|
62
|
+
task :migrate => :configure_connection do
|
63
|
+
ActiveRecord::Migration.verbose = true
|
64
|
+
ActiveRecord::Migrator.migrate MIGRATIONS_DIR, ENV['VERSION'] ? ENV['VERSION'].to_i : nil
|
65
|
+
end
|
66
|
+
|
67
|
+
desc 'Rolls the schema back to the previous version (specify steps w/ STEP=n).'
|
68
|
+
task :rollback => :configure_connection do
|
69
|
+
step = ENV['STEP'] ? ENV['STEP'].to_i : 1
|
70
|
+
ActiveRecord::Migrator.rollback MIGRATIONS_DIR, step
|
71
|
+
end
|
72
|
+
|
73
|
+
desc "Retrieves the current schema version number"
|
74
|
+
task :version => :configure_connection do
|
75
|
+
puts "Current version: #{ActiveRecord::Migrator.current_version}"
|
76
|
+
end
|
77
|
+
end
|
data/lib/rake/task.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'fileutils'
|
3
|
+
|
4
|
+
namespace :active_metadata do
|
5
|
+
task :install do
|
6
|
+
#FileUtils.cp File.expand_path('../../templates/mongoid.yml',__FILE__), File.expand_path('config/')
|
7
|
+
#puts "Installed mongoid.yml"
|
8
|
+
|
9
|
+
FileUtils.cp File.expand_path('../../templates/active_metadata.yml',__FILE__), File.expand_path('config/')
|
10
|
+
puts "Installed active_metadata.yml"
|
11
|
+
|
12
|
+
puts "Copying migrations"
|
13
|
+
ts = Time.now.utc.strftime('%Y%m%d%H%M%S')
|
14
|
+
FileUtils.cp File.expand_path('../../templates/active_metadata_migrations',__FILE__), File.expand_path("db/migrate/#{ts}_active_metadata_migrations.rb")
|
15
|
+
puts "run rake db:migrate to complete the gem installation"
|
16
|
+
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
development:
|
2
|
+
persists_with: "active_record"
|
3
|
+
history_skip_fields:
|
4
|
+
- 'id'
|
5
|
+
- 'created_at'
|
6
|
+
- 'updated_at'
|
7
|
+
attachment_base_path: "public/system/metadata_attachments"
|
8
|
+
attachment_base_url: "/system/metadata_attachments"
|
9
|
+
|
10
|
+
test:
|
11
|
+
persists_with: "active_record"
|
12
|
+
history_skip_fields:
|
13
|
+
- 'id'
|
14
|
+
- 'created_at'
|
15
|
+
- 'updated_at'
|
16
|
+
attachment_base_path: "public/system/metadata_attachments"
|
17
|
+
attachment_base_url: "/system/metadata_attachments"
|
@@ -0,0 +1,67 @@
|
|
1
|
+
class ActiveMetadataMigrations < ActiveRecord::Migration
|
2
|
+
def self.up
|
3
|
+
|
4
|
+
create_table :notes do |t|
|
5
|
+
t.text :note
|
6
|
+
t.string :label
|
7
|
+
t.integer :document_id
|
8
|
+
t.integer :created_by
|
9
|
+
t.integer :updated_by
|
10
|
+
t.timestamps
|
11
|
+
end
|
12
|
+
|
13
|
+
add_index :notes, :document_id
|
14
|
+
add_index :notes, :label
|
15
|
+
add_index :notes, :updated_at
|
16
|
+
|
17
|
+
create_table :histories do |t|
|
18
|
+
t.text :value
|
19
|
+
t.string :label
|
20
|
+
t.integer :document_id
|
21
|
+
t.integer :created_by
|
22
|
+
t.timestamps
|
23
|
+
end
|
24
|
+
|
25
|
+
add_index :histories, :document_id
|
26
|
+
add_index :histories, :label
|
27
|
+
add_index :histories, :created_at
|
28
|
+
|
29
|
+
create_table :attachments do |t|
|
30
|
+
t.string :label
|
31
|
+
t.integer :document_id
|
32
|
+
t.integer :created_by
|
33
|
+
t.integer :updated_by
|
34
|
+
t.integer :counter
|
35
|
+
t.string :attach_file_name
|
36
|
+
t.string :attach_content_type
|
37
|
+
t.integer :attach_file_size
|
38
|
+
t.datetime :attach_updated_at
|
39
|
+
t.timestamps
|
40
|
+
end
|
41
|
+
|
42
|
+
add_index :attachments, :document_id
|
43
|
+
add_index :attachments, :label
|
44
|
+
add_index :attachments, :attach_updated_at
|
45
|
+
|
46
|
+
create_table :watchers do |t|
|
47
|
+
t.integer :owner_id
|
48
|
+
t.string :label
|
49
|
+
t.integer :document_id
|
50
|
+
t.timestamps
|
51
|
+
end
|
52
|
+
|
53
|
+
add_index :watchers, :document_id
|
54
|
+
add_index :watchers, :label
|
55
|
+
add_index :watchers, :owner_id
|
56
|
+
add_index :watchers, :created_at
|
57
|
+
|
58
|
+
end
|
59
|
+
|
60
|
+
def self.down
|
61
|
+
drop_table :notes
|
62
|
+
drop_table :histories
|
63
|
+
drop_table :attachments
|
64
|
+
drop_table :watchers
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
development:
|
2
|
+
host: localhost
|
3
|
+
database: metadata_d
|
4
|
+
|
5
|
+
test:
|
6
|
+
host: localhost
|
7
|
+
database: metadata_t
|
8
|
+
|
9
|
+
# set these environment variables on your prod server
|
10
|
+
production:
|
11
|
+
host: <%= ENV['MONGOID_HOST'] %>
|
12
|
+
port: <%= ENV['MONGOID_PORT'] %>
|
13
|
+
username: <%= ENV['MONGOID_USERNAME'] %>
|
14
|
+
password: <%= ENV['MONGOID_PASSWORD'] %>
|
15
|
+
database: <%= ENV['MONGOID_DATABASE'] %>
|
16
|
+
# slaves:
|
17
|
+
# - host: slave1.local
|
18
|
+
# port: 27018
|
19
|
+
# - host: slave2.local
|
20
|
+
# port: 27019
|