rapid 0.0.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.
- data/Rakefile +66 -0
- data/lib/rad/http_controller/acts_as/authenticated.rb +131 -0
- data/lib/rad/http_controller/acts_as/authenticated_master_domain.rb +119 -0
- data/lib/rad/http_controller/acts_as/authorized.rb +83 -0
- data/lib/rad/http_controller/acts_as/localized.rb +27 -0
- data/lib/rad/http_controller/acts_as/multitenant.rb +53 -0
- data/lib/rad/http_controller/helpers/service_mix_helper.rb +50 -0
- data/lib/rad/http_controller.rb +15 -0
- data/lib/rad/lib/text_utils.rb +334 -0
- data/lib/rad/locales/en.yml +80 -0
- data/lib/rad/locales/ru.yml +83 -0
- data/lib/rad/locales.rb +2 -0
- data/lib/rad/models/account.rb +88 -0
- data/lib/rad/models/default_permissions.yml +26 -0
- data/lib/rad/models/micelaneous.rb +1 -0
- data/lib/rad/models/role.rb +88 -0
- data/lib/rad/models/secure_token.rb +33 -0
- data/lib/rad/models/space.rb +184 -0
- data/lib/rad/models/user.rb +158 -0
- data/lib/rad/models.rb +41 -0
- data/lib/rad/mongo_mapper/acts_as/authenticated_by_open_id.rb +29 -0
- data/lib/rad/mongo_mapper/acts_as/authenticated_by_password.rb +120 -0
- data/lib/rad/mongo_mapper/acts_as/authorized.rb +197 -0
- data/lib/rad/mongo_mapper/acts_as/authorized_object.rb +171 -0
- data/lib/rad/mongo_mapper/multitenant.rb +34 -0
- data/lib/rad/mongo_mapper/rad_micelaneous.rb +43 -0
- data/lib/rad/mongo_mapper/space_keys.rb +62 -0
- data/lib/rad/mongo_mapper/text_processor.rb +47 -0
- data/lib/rad/mongo_mapper.rb +20 -0
- data/lib/rad/paperclip/callbacks.rb +40 -0
- data/lib/rad/paperclip/extensions.rb +64 -0
- data/lib/rad/paperclip/integration.rb +165 -0
- data/lib/rad/paperclip/mime.rb +5 -0
- data/lib/rad/paperclip/validations.rb +64 -0
- data/lib/rad/paperclip.rb +11 -0
- data/lib/rad/spec/controller.rb +51 -0
- data/lib/rad/spec/model/factories.rb +65 -0
- data/lib/rad/spec/model.rb +85 -0
- data/lib/rad/spec/rem_helper.rb +145 -0
- data/lib/rad/spec.rb +4 -0
- data/lib/rad/tasks/backup.rake +64 -0
- data/lib/rad/tasks/initialize.rake +35 -0
- data/lib/rad.rb +32 -0
- data/readme.md +3 -0
- data/spec/controller/authorization_spec.rb +146 -0
- data/spec/controller/helper.rb +14 -0
- data/spec/lib/helper.rb +7 -0
- data/spec/lib/text_utils_spec.rb +238 -0
- data/spec/models/authorization_spec.rb +93 -0
- data/spec/models/authorized_object_spec.rb +258 -0
- data/spec/models/file_audit_spec/100.txt +1 -0
- data/spec/models/file_audit_spec/302.txt +3 -0
- data/spec/models/file_audit_spec.rb +168 -0
- data/spec/models/helper.rb +11 -0
- data/spec/models/space_key_spec.rb +68 -0
- data/spec/models/user_spec.rb +80 -0
- data/spec/mongo_mapper/basic_spec.rb +41 -0
- data/spec/mongo_mapper/helper.rb +10 -0
- data/spec/spec.opts +4 -0
- metadata +138 -0
@@ -0,0 +1,165 @@
|
|
1
|
+
Paperclip.class_eval do
|
2
|
+
def self.registered_classes
|
3
|
+
@registered_classes ||= Hash.new{|h, k| h[k] = []}
|
4
|
+
end
|
5
|
+
end
|
6
|
+
|
7
|
+
Paperclip::ClassMethods.class_eval do
|
8
|
+
def has_attached_file name, options = {}
|
9
|
+
name = name.to_s
|
10
|
+
|
11
|
+
Paperclip.registered_classes[self] << name
|
12
|
+
|
13
|
+
define_paperclip_keys_for name
|
14
|
+
|
15
|
+
include Paperclip::InstanceMethods
|
16
|
+
|
17
|
+
write_inheritable_attribute(:attachment_definitions, {}) if attachment_definitions.nil?
|
18
|
+
attachment_definitions[name] = {:validations => []}.merge(options)
|
19
|
+
|
20
|
+
after_save :save_attached_files
|
21
|
+
before_destroy :destroy_attached_files
|
22
|
+
|
23
|
+
define_callbacks :before_post_process, :after_post_process
|
24
|
+
define_callbacks :"before_#{name}_post_process", :"after_#{name}_post_process"
|
25
|
+
|
26
|
+
define_method name do |*args|
|
27
|
+
a = attachment_for(name)
|
28
|
+
(args.length > 0) ? a.to_s(args.first) : a
|
29
|
+
end
|
30
|
+
|
31
|
+
define_method "#{name}=" do |file|
|
32
|
+
attachment_for(name).assign(file)
|
33
|
+
end
|
34
|
+
|
35
|
+
define_method "#{name}?" do
|
36
|
+
attachment_for(name).file?
|
37
|
+
end
|
38
|
+
|
39
|
+
validates_each name, :logic => lambda {
|
40
|
+
attachment = attachment_for(name)
|
41
|
+
attachment.send(:flush_errors) unless attachment.valid?
|
42
|
+
}
|
43
|
+
end
|
44
|
+
|
45
|
+
def disable_file_audit &block
|
46
|
+
begin
|
47
|
+
Thread.current['disable_file_audit'] = true
|
48
|
+
block.call
|
49
|
+
ensure
|
50
|
+
Thread.current['disable_file_audit'] = nil
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def enable_file_audit &block
|
55
|
+
begin
|
56
|
+
Thread.current['disable_file_audit'] = false
|
57
|
+
block.call
|
58
|
+
ensure
|
59
|
+
Thread.current['disable_file_audit'] = nil
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
protected
|
64
|
+
def define_paperclip_keys_for attachment
|
65
|
+
key "#{attachment}_file_name", String
|
66
|
+
key "#{attachment}_content_type", String
|
67
|
+
key "#{attachment}_file_size", Integer
|
68
|
+
key "#{attachment}_updated_at", Time
|
69
|
+
|
70
|
+
key "#{attachment}_old_file_size"
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
Paperclip::InstanceMethods.class_eval do
|
75
|
+
protected
|
76
|
+
def disable_file_audit?
|
77
|
+
disable = Thread.current['disable_file_audit']
|
78
|
+
if disable != nil
|
79
|
+
disable
|
80
|
+
else
|
81
|
+
crystal.config.test?
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def file_size_for attachment
|
86
|
+
send("#{attachment}_file_size") || 0
|
87
|
+
end
|
88
|
+
|
89
|
+
def old_file_size_for attachment
|
90
|
+
send("#{attachment}_old_file_size") || 0
|
91
|
+
end
|
92
|
+
|
93
|
+
def set_old_file_size_for attachment, size
|
94
|
+
send("#{attachment}_old_file_size=", size)
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
Paperclip::Interpolations.class_eval do
|
99
|
+
# fix filename to works with files without extension
|
100
|
+
def filename attachment, style
|
101
|
+
unless (ext = extension(attachment, style)).blank?
|
102
|
+
"#{basename(attachment, style)}.#{ext}"
|
103
|
+
else
|
104
|
+
basename(attachment, style)
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
Paperclip::Attachment.class_eval do
|
110
|
+
# allow spaces in filename
|
111
|
+
def assign uploaded_file
|
112
|
+
ensure_required_accessors!
|
113
|
+
|
114
|
+
if uploaded_file.is_a?(Paperclip::Attachment)
|
115
|
+
uploaded_file = uploaded_file.to_file(:original)
|
116
|
+
close_uploaded_file = uploaded_file.respond_to?(:close)
|
117
|
+
end
|
118
|
+
|
119
|
+
return nil unless valid_assignment?(uploaded_file)
|
120
|
+
|
121
|
+
uploaded_file.binmode if uploaded_file.respond_to? :binmode
|
122
|
+
self.clear
|
123
|
+
|
124
|
+
return nil if uploaded_file.nil?
|
125
|
+
|
126
|
+
@queued_for_write[:original] = uploaded_file.to_tempfile
|
127
|
+
instance_write(:file_name, uploaded_file.original_filename.strip.gsub(/[^A-Za-z\d\.\-_ ]+/, '_'))
|
128
|
+
instance_write(:content_type, uploaded_file.content_type.to_s.strip)
|
129
|
+
instance_write(:file_size, uploaded_file.size.to_i)
|
130
|
+
instance_write(:updated_at, Time.now)
|
131
|
+
|
132
|
+
@dirty = true
|
133
|
+
|
134
|
+
post_process if valid?
|
135
|
+
|
136
|
+
# Reset the file size if the original file was reprocessed.
|
137
|
+
instance_write(:file_size, @queued_for_write[:original].size.to_i)
|
138
|
+
ensure
|
139
|
+
uploaded_file.close if close_uploaded_file
|
140
|
+
validate
|
141
|
+
end
|
142
|
+
|
143
|
+
alias_method :file_name, :original_filename
|
144
|
+
|
145
|
+
def blank?; file_name.blank? end
|
146
|
+
|
147
|
+
# returns nil if file_name is blank
|
148
|
+
def url_with_default *args
|
149
|
+
unless file_name.nil?
|
150
|
+
url_without_default *args
|
151
|
+
else
|
152
|
+
nil
|
153
|
+
end
|
154
|
+
end
|
155
|
+
alias_method_chain :url, :default
|
156
|
+
|
157
|
+
# Hack to use styles with no processors
|
158
|
+
def post_process_styles_with_blank
|
159
|
+
@styles.each do |name, args|
|
160
|
+
return if args[:processors].blank?
|
161
|
+
end
|
162
|
+
post_process_styles_without_blank
|
163
|
+
end
|
164
|
+
alias_method_chain :post_process_styles, :blank
|
165
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
Paperclip::ClassMethods.class_eval do
|
2
|
+
|
3
|
+
def validates_file attachment
|
4
|
+
validates_maximum_file_size attachment
|
5
|
+
validates_maximum_user_files_size attachment
|
6
|
+
validates_maximum_account_files_size attachment
|
7
|
+
end
|
8
|
+
|
9
|
+
def validates_maximum_file_size attachment
|
10
|
+
# defer do
|
11
|
+
method_name = "validates_maximum_#{attachment}_size"
|
12
|
+
define_method method_name do
|
13
|
+
max_size = Account.current? ? Account.current.max_file_size : config.max_file_size(1.megabyte)
|
14
|
+
size = file_size_for(attachment)
|
15
|
+
errors.add attachment, t(:invalid_file_size, :max_size => (max_size / 1000)) if size > max_size
|
16
|
+
end
|
17
|
+
protected method_name
|
18
|
+
validate method_name
|
19
|
+
# end
|
20
|
+
end
|
21
|
+
|
22
|
+
def validates_maximum_user_files_size attachment
|
23
|
+
method_name = "validates_maximum_user_files_size_for_#{attachment}"
|
24
|
+
define_method method_name do
|
25
|
+
return if disable_file_audit?
|
26
|
+
max_files_size = Space.current.max_user_files_size
|
27
|
+
return if max_files_size == 0
|
28
|
+
|
29
|
+
files_size = User.current.files_size
|
30
|
+
size = file_size_for(attachment)
|
31
|
+
old_size = old_file_size_for(attachment)
|
32
|
+
if size + files_size > max_files_size - old_size
|
33
|
+
errors.add(attachment, t(
|
34
|
+
:maximum_user_files_size_exceeded,
|
35
|
+
:max_files_size => (max_files_size / 1000),
|
36
|
+
:files_size => (files_size / 1000)
|
37
|
+
))
|
38
|
+
end
|
39
|
+
end
|
40
|
+
protected method_name
|
41
|
+
validate method_name
|
42
|
+
end
|
43
|
+
|
44
|
+
def validates_maximum_account_files_size attachment
|
45
|
+
method_name = "validates_maximum_account_files_size_for_#{attachment}"
|
46
|
+
define_method method_name do
|
47
|
+
return if disable_file_audit?
|
48
|
+
|
49
|
+
files_size = Account.current.files_size
|
50
|
+
max_files_size = Account.current.max_account_files_size
|
51
|
+
size = file_size_for(attachment)
|
52
|
+
old_size = old_file_size_for(attachment)
|
53
|
+
if size + files_size > max_files_size - old_size
|
54
|
+
errors.add(attachment, t(
|
55
|
+
:maximum_account_files_size_exceeded,
|
56
|
+
:max_files_size => (max_files_size / 1000),
|
57
|
+
:files_size => (files_size / 1000)
|
58
|
+
))
|
59
|
+
end
|
60
|
+
end
|
61
|
+
protected method_name
|
62
|
+
validate method_name
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'crystal/spec/controller'
|
2
|
+
|
3
|
+
#
|
4
|
+
# User
|
5
|
+
#
|
6
|
+
Spec::Example::ExampleGroup.class_eval do
|
7
|
+
def self.with_user_controller
|
8
|
+
return if @with_user_controller
|
9
|
+
@with_user_controller = true
|
10
|
+
|
11
|
+
Crystal::HttpController::Authenticated::InstanceMethods.class_eval do
|
12
|
+
def prepare_current_user_for_slave_domain_with_test
|
13
|
+
prepare_current_user_for_slave_domain_without_test if $do_not_skip_authentication
|
14
|
+
end
|
15
|
+
alias_method_chain :prepare_current_user_for_slave_domain, :test
|
16
|
+
end
|
17
|
+
|
18
|
+
Crystal::HttpController::AuthenticatedMasterDomain::InstanceMethods.class_eval do
|
19
|
+
def prepare_current_user_for_master_domain_with_test
|
20
|
+
prepare_current_user_for_master_domain_without_test if $do_not_skip_authentication
|
21
|
+
end
|
22
|
+
alias_method_chain :prepare_current_user_for_master_domain, :test
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
|
28
|
+
#
|
29
|
+
# Miltitenancy
|
30
|
+
#
|
31
|
+
Spec::Example::ExampleGroup.class_eval do
|
32
|
+
def self.with_multitenancy
|
33
|
+
return if @with_multitenancy
|
34
|
+
@with_multitenancy = true
|
35
|
+
|
36
|
+
# db_name = "#{SETTING.db_prefix!}_default_#{Rails.env}"
|
37
|
+
# global_db_name = "#{SETTING.db_prefix!}_global_#{Rails.env}"
|
38
|
+
# MongoMapper.database = db_name
|
39
|
+
Crystal::HttpController::Multitenant::InstanceMethods.class_eval do
|
40
|
+
protected
|
41
|
+
|
42
|
+
def select_account_and_space
|
43
|
+
yield
|
44
|
+
end
|
45
|
+
|
46
|
+
def select_multitenant_database
|
47
|
+
yield
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
require 'factory_girl'
|
2
|
+
|
3
|
+
#
|
4
|
+
# User
|
5
|
+
#
|
6
|
+
Factory.define :blank_user, :class => User do |u|
|
7
|
+
u.sequence(:name){|i| "user#{i}"}
|
8
|
+
end
|
9
|
+
|
10
|
+
Factory.define :new_user, :class => User do |u|
|
11
|
+
u.sequence(:name){|i| "user#{i}"}
|
12
|
+
u.sequence(:email){|i| "user#{i}@email.com"}
|
13
|
+
u.sequence(:password){|i| "user#{i}"}
|
14
|
+
u.password_confirmation{|_self| _self.password}
|
15
|
+
end
|
16
|
+
|
17
|
+
Factory.define :user, :parent => :new_user do |u|
|
18
|
+
u.state 'active'
|
19
|
+
end
|
20
|
+
|
21
|
+
Factory.define :open_id_user, :class => User do |u|
|
22
|
+
u.sequence(:name){|i| "user#{i}"}
|
23
|
+
u.sequence(:open_ids){|i| ["open_id_#{i}"]}
|
24
|
+
u.state 'active'
|
25
|
+
end
|
26
|
+
|
27
|
+
Factory.define :anonymous, :class => User, :parent => :new_user do |u|
|
28
|
+
u.name 'anonymous'
|
29
|
+
u.email "anonymous@mail.com"
|
30
|
+
u.password "anonymous_password"
|
31
|
+
u.password_confirmation{|_self| _self.password}
|
32
|
+
end
|
33
|
+
|
34
|
+
Factory.define :admin, :class => User, :parent => :new_user do |u|
|
35
|
+
u.admin_of_accounts{[Account.current.id]}
|
36
|
+
# u.roles_containers{[RolesContainer.new(:space_id => Space.current.id, :roles => %w{member manager})]}
|
37
|
+
u.space_roles{%w{member manager}}
|
38
|
+
end
|
39
|
+
|
40
|
+
Factory.define :member, :class => User, :parent => :new_user do |u|
|
41
|
+
# u.roles_containers{[RolesContainer.new(:space_id => Space.current.id, :roles => %w{member})]}
|
42
|
+
u.space_roles{%w{member}}
|
43
|
+
end
|
44
|
+
|
45
|
+
Factory.define :manager, :class => User, :parent => :member do |u|
|
46
|
+
# u.roles_containers{[RolesContainer.new(:space_id => Space.current.id, :roles => %w{member manager})]}
|
47
|
+
u.space_roles{%w{member manager}}
|
48
|
+
end
|
49
|
+
|
50
|
+
Factory.define :global_admin, :parent => :user, :parent => :new_user do |u|
|
51
|
+
u.global_admin true
|
52
|
+
end
|
53
|
+
|
54
|
+
|
55
|
+
#
|
56
|
+
# Account and Space
|
57
|
+
#
|
58
|
+
Factory.define :account do |a|
|
59
|
+
a.name "test-account"
|
60
|
+
end
|
61
|
+
|
62
|
+
Factory.define :space do |s|
|
63
|
+
s.name "default"
|
64
|
+
s.account{|a| a.association(:account)}
|
65
|
+
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
require 'mongo_mapper_ext/spec/helper'
|
2
|
+
require 'crystal/environment'
|
3
|
+
|
4
|
+
#
|
5
|
+
# User
|
6
|
+
#
|
7
|
+
Spec::Example::ExampleGroup.class_eval do
|
8
|
+
def with_user
|
9
|
+
return if @with_user
|
10
|
+
@with_user = true
|
11
|
+
|
12
|
+
User.class_eval do
|
13
|
+
def self.anonymous
|
14
|
+
@anonymous ||= Factory.build :anonymous
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def login_as user
|
20
|
+
User.current = user
|
21
|
+
# controller.stub!(:current_user).and_return(user)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
|
26
|
+
#
|
27
|
+
# Miltitenancy
|
28
|
+
#
|
29
|
+
Spec::Example::ExampleGroup.class_eval do
|
30
|
+
def set_default_space
|
31
|
+
account = Factory.create :account
|
32
|
+
|
33
|
+
Account.current = account
|
34
|
+
Space.current = account.spaces.first
|
35
|
+
end
|
36
|
+
|
37
|
+
def set_space space
|
38
|
+
if space
|
39
|
+
Space.current = space
|
40
|
+
Account.current = space.account
|
41
|
+
else
|
42
|
+
Space.current = nil
|
43
|
+
Account.current = nil
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
|
49
|
+
#
|
50
|
+
# Database
|
51
|
+
#
|
52
|
+
Spec::Example::ExampleGroup.class_eval do
|
53
|
+
def self.with_test_database
|
54
|
+
before :all do
|
55
|
+
MongoMapper.db_config = {
|
56
|
+
'global' => {'name' => 'global_test'},
|
57
|
+
'accounts' => {'name' => "accounts_test"}
|
58
|
+
}
|
59
|
+
|
60
|
+
MongoMapper.database = 'global_test'
|
61
|
+
# MongoMapper.call_deferred
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
|
67
|
+
#
|
68
|
+
# Rad
|
69
|
+
#
|
70
|
+
Spec::Example::ExampleGroup.class_eval do
|
71
|
+
def self.with_rad_model
|
72
|
+
# with_mail
|
73
|
+
with_mongo_mapper
|
74
|
+
with_test_database
|
75
|
+
# with_user
|
76
|
+
|
77
|
+
before :all do
|
78
|
+
crystal[:logger] = Logger.new nil
|
79
|
+
|
80
|
+
require 'rad/models'
|
81
|
+
require 'rad/spec/model/factories'
|
82
|
+
with_user
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
@@ -0,0 +1,145 @@
|
|
1
|
+
# require 'mongo_mapper_ext/spec/helper'
|
2
|
+
#
|
3
|
+
#
|
4
|
+
# #
|
5
|
+
# # Mail
|
6
|
+
# #
|
7
|
+
# Spec::Example::ExampleGroup.class_eval do
|
8
|
+
# def self.with_mail
|
9
|
+
# raise 'not implemented'
|
10
|
+
# end
|
11
|
+
#
|
12
|
+
# def clear_mail
|
13
|
+
# # ActionMailer::Base.delivery_method = :test
|
14
|
+
# # ActionMailer::Base.perform_deliveries = true
|
15
|
+
# # ActionMailer::Base.deliveries = []
|
16
|
+
# end
|
17
|
+
#
|
18
|
+
# def sent_letters
|
19
|
+
# # ActionMailer::Base.deliveries
|
20
|
+
# end
|
21
|
+
# end
|
22
|
+
#
|
23
|
+
#
|
24
|
+
# #
|
25
|
+
# # Routes
|
26
|
+
# #
|
27
|
+
# def default_path
|
28
|
+
# crystal.config.default_path!
|
29
|
+
# end
|
30
|
+
#
|
31
|
+
#
|
32
|
+
# #
|
33
|
+
# # User
|
34
|
+
# #
|
35
|
+
# Spec::Example::ExampleGroup.class_eval do
|
36
|
+
# def self.with_user
|
37
|
+
# return if @with_user
|
38
|
+
# @with_user = true
|
39
|
+
#
|
40
|
+
# User.class_eval do
|
41
|
+
# def self.anonymous
|
42
|
+
# @anonymous ||= Factory.build :anonymous
|
43
|
+
# end
|
44
|
+
# end
|
45
|
+
# end
|
46
|
+
#
|
47
|
+
# def self.with_user_controller
|
48
|
+
# return if @with_user_controller
|
49
|
+
# @with_user_controller = true
|
50
|
+
#
|
51
|
+
# Crystal::Remote::Authenticated::InstanceMethods.class_eval do
|
52
|
+
# def prepare_current_user_for_slave_domain_with_test
|
53
|
+
# prepare_current_user_for_slave_domain_without_test if $do_not_skip_authentication
|
54
|
+
# end
|
55
|
+
# alias_method_chain :prepare_current_user_for_slave_domain, :test
|
56
|
+
# end
|
57
|
+
#
|
58
|
+
# Crystal::Remote::AuthenticatedMasterDomain::InstanceMethods.class_eval do
|
59
|
+
# def prepare_current_user_for_master_domain_with_test
|
60
|
+
# prepare_current_user_for_master_domain_without_test if $do_not_skip_authentication
|
61
|
+
# end
|
62
|
+
# alias_method_chain :prepare_current_user_for_master_domain, :test
|
63
|
+
# end
|
64
|
+
# end
|
65
|
+
#
|
66
|
+
# def login_as user
|
67
|
+
# User.current = user
|
68
|
+
# # controller.stub!(:current_user).and_return(user)
|
69
|
+
# end
|
70
|
+
# end
|
71
|
+
#
|
72
|
+
#
|
73
|
+
# #
|
74
|
+
# # Miltitenancy
|
75
|
+
# #
|
76
|
+
# Spec::Example::ExampleGroup.class_eval do
|
77
|
+
# def self.with_multitenancy
|
78
|
+
# return if @with_multitenancy
|
79
|
+
# @with_multitenancy = true
|
80
|
+
#
|
81
|
+
# # db_name = "#{SETTING.db_prefix!}_default_#{Rails.env}"
|
82
|
+
# # global_db_name = "#{SETTING.db_prefix!}_global_#{Rails.env}"
|
83
|
+
# # MongoMapper.database = db_name
|
84
|
+
# Crystal::Remote::Multitenant::InstanceMethods.class_eval do
|
85
|
+
# protected
|
86
|
+
#
|
87
|
+
# def select_account_and_space
|
88
|
+
# yield
|
89
|
+
# end
|
90
|
+
#
|
91
|
+
# def select_multitenant_database
|
92
|
+
# yield
|
93
|
+
# end
|
94
|
+
# end
|
95
|
+
# end
|
96
|
+
#
|
97
|
+
# def set_default_space
|
98
|
+
# account = Factory.create :account
|
99
|
+
#
|
100
|
+
# Account.current = account
|
101
|
+
# Space.current = account.spaces.first
|
102
|
+
# end
|
103
|
+
#
|
104
|
+
# def set_space space
|
105
|
+
# if space
|
106
|
+
# Space.current = space
|
107
|
+
# Account.current = space.account
|
108
|
+
# else
|
109
|
+
# Space.current = nil
|
110
|
+
# Account.current = nil
|
111
|
+
# end
|
112
|
+
# end
|
113
|
+
# end
|
114
|
+
#
|
115
|
+
#
|
116
|
+
# #
|
117
|
+
# # Database
|
118
|
+
# #
|
119
|
+
# Spec::Example::ExampleGroup.class_eval do
|
120
|
+
# def self.with_test_database
|
121
|
+
# before :all do
|
122
|
+
# MongoMapper.db_config = {
|
123
|
+
# 'global' => {'name' => 'global_test'},
|
124
|
+
# 'accounts' => {'name' => "accounts_test"}
|
125
|
+
# }
|
126
|
+
#
|
127
|
+
# MongoMapper.database = 'global_test'
|
128
|
+
#
|
129
|
+
# MongoMapper.call_deferred
|
130
|
+
# end
|
131
|
+
# end
|
132
|
+
# end
|
133
|
+
#
|
134
|
+
#
|
135
|
+
# #
|
136
|
+
# # Rad
|
137
|
+
# #
|
138
|
+
# Spec::Example::ExampleGroup.class_eval do
|
139
|
+
# def self.with_rad_model
|
140
|
+
# # with_mail
|
141
|
+
# with_mongo_mapper
|
142
|
+
# with_test_database
|
143
|
+
# with_user
|
144
|
+
# end
|
145
|
+
# end
|
data/lib/rad/spec.rb
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
require 'net/ssh'
|
2
|
+
|
3
|
+
folders = {
|
4
|
+
'/apps/spaces/shared/system' => '/spaces/shared',
|
5
|
+
'/apps/service_mix/shared/system' => '/service_mix/shared'
|
6
|
+
}
|
7
|
+
|
8
|
+
databases = [
|
9
|
+
"accounts_production",
|
10
|
+
"global_production"
|
11
|
+
]
|
12
|
+
|
13
|
+
backup_directory = '/Users/alex/Documents/saas4b_backup'
|
14
|
+
user = 'web'
|
15
|
+
host = 'saas4b.com'
|
16
|
+
|
17
|
+
task :backup do
|
18
|
+
raise "Backup directory doesn't exist! (#{backup_directory})" unless File.exist? backup_directory
|
19
|
+
|
20
|
+
# Create Databases Dump
|
21
|
+
Net::SSH.start(host, user) do |ssh|
|
22
|
+
databases.each do |dbname|
|
23
|
+
command = "/opt/mongodb/bin/mongodump --db #{dbname} --out /tmp/#{dbname}.mongo.dump"
|
24
|
+
puts "Executing Remotelly: #{command}"
|
25
|
+
result = ssh.exec!(command)
|
26
|
+
puts result
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
# Copying Database
|
31
|
+
databases.each do |dbname|
|
32
|
+
dump_file = "/tmp/#{dbname}.mongo.dump"
|
33
|
+
backup_tmp_file = "#{backup_directory}/#{dbname}.tmp"
|
34
|
+
command = "rsync -e 'ssh' -al --delete --stats --progress #{user}@#{host}:#{dump_file}/ #{backup_tmp_file}"
|
35
|
+
puts "Executing: #{command}"
|
36
|
+
system command
|
37
|
+
|
38
|
+
raise "Can't find downloaded dump file for #{dbname} Database (#{backup_tmp_file})!" unless File.exist? backup_tmp_file
|
39
|
+
backup_file = "#{backup_directory}/#{dbname}.mongo.dump"
|
40
|
+
FileUtils.rm_r backup_file if File.exist? backup_file
|
41
|
+
File.rename backup_tmp_file, backup_file
|
42
|
+
end
|
43
|
+
|
44
|
+
# Remove tmp dump files
|
45
|
+
Net::SSH.start(host, user) do |ssh|
|
46
|
+
databases.each do |dbname|
|
47
|
+
command = "rm -r /tmp/#{dbname}.mongo.dump"
|
48
|
+
puts "Executing Remotelly: #{command}"
|
49
|
+
result = ssh.exec!(command)
|
50
|
+
puts result
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
# Copying Data Files
|
55
|
+
folders.each do |from, to|
|
56
|
+
to_full_path = "#{backup_directory}#{to}"
|
57
|
+
FileUtils.mkdir_p to_full_path unless File.exist? to_full_path
|
58
|
+
command = "rsync -e 'ssh' -al --delete --stats --progress #{user}@#{host}:#{from} #{to_full_path}"
|
59
|
+
puts "Executing: #{command}"
|
60
|
+
system command
|
61
|
+
end
|
62
|
+
|
63
|
+
puts "Backup finished!"
|
64
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
namespace :db do
|
2
|
+
SAMPLE_DATA_DIR = File.dirname(__FILE__) + "/sample_data"
|
3
|
+
|
4
|
+
desc "Initialize database with initial data"
|
5
|
+
task :initialize => :environment do
|
6
|
+
# Anonymous User
|
7
|
+
User.find_by_name('anonymous').try :destroy
|
8
|
+
u = User.new :name => 'anonymous',
|
9
|
+
:email => "anonymous@mail.com",
|
10
|
+
:password => "anonymous_password",
|
11
|
+
:password_confirmation => "anonymous_password"
|
12
|
+
u.save :validate => false
|
13
|
+
u.activate!
|
14
|
+
u.save :validate => false
|
15
|
+
|
16
|
+
# Admin User
|
17
|
+
User.find_by_name('admin').try :destroy
|
18
|
+
u = User.new :name => 'admin',
|
19
|
+
:email => "admin@mail.com",
|
20
|
+
:password => 'admin',
|
21
|
+
:password_confirmation => 'admin',
|
22
|
+
:global_admin => true
|
23
|
+
u.save :validate => false
|
24
|
+
u.activate!
|
25
|
+
u.save :validate => false
|
26
|
+
|
27
|
+
# Default Account
|
28
|
+
Account.find_by_name('development').try :destroy
|
29
|
+
Account.create! :name => 'development', :domains => ["localhost"]
|
30
|
+
end
|
31
|
+
|
32
|
+
desc "Rollback db:initialize action"
|
33
|
+
task :deinitialize => :environment do
|
34
|
+
end
|
35
|
+
end
|