rad_kit 0.0.8 → 0.0.9
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/components/captcha.rb +4 -4
- data/lib/components/kit.rb +5 -5
- data/lib/components/kit.yml +0 -2
- data/lib/components/models.rb +2 -5
- data/lib/components/models.yml +3 -0
- data/lib/kit/controller/authorized.rb +9 -8
- data/lib/kit/controller/captcha.rb +6 -6
- data/lib/kit/controller/localized.rb +2 -2
- data/lib/kit/gems.rb +12 -8
- data/lib/kit/i18n.rb +3 -3
- data/lib/kit/i18n/locales/ru/pluralization.rb +3 -3
- data/lib/kit/kit.rb +7 -7
- data/lib/kit/kit_text_utils.rb +6 -6
- data/lib/kit/misc/prepare_model.rb +4 -4
- data/lib/kit/models.rb +22 -5
- data/lib/kit/models/attachment_file.rb +27 -0
- data/lib/kit/models/{attachments_uploader_helper.rb → attachments_helper.rb} +34 -28
- data/lib/kit/models/authorized.rb +64 -63
- data/lib/kit/models/authorized_object.rb +70 -73
- data/lib/kit/models/base_file.rb +37 -0
- data/lib/kit/models/config.rb +30 -0
- data/lib/kit/models/indexes.rb +30 -0
- data/lib/kit/models/miscellaneous.rb +7 -1
- data/lib/kit/models/role.rb +17 -17
- data/lib/kit/models/tags.rb +71 -0
- data/lib/kit/models/tags_mixin.rb +39 -0
- data/lib/kit/mongodb_model.rb +13 -0
- data/lib/kit/mongodb_model/text_processor.rb +32 -0
- data/lib/kit/spec.rb +30 -30
- data/lib/kit/spec/items_controller_crud.rb +9 -9
- data/lib/kit/support.rb +1 -1
- data/lib/kit/tasks.rb +3 -7
- data/lib/text_utils.rb +2 -2
- data/lib/text_utils/code_highlighter.rb +17 -17
- data/lib/text_utils/custom_markdown.rb +7 -7
- data/lib/text_utils/ensure_utf.rb +3 -3
- data/lib/text_utils/format_qualifier.rb +2 -2
- data/lib/text_utils/html_sanitizer.rb +9 -9
- data/lib/text_utils/markdown.rb +9 -9
- data/lib/text_utils/pipe.rb +1 -1
- data/lib/text_utils/processor.rb +3 -3
- data/lib/text_utils/support.rb +3 -3
- data/lib/text_utils/truncate.rb +4 -4
- data/readme.md +3 -1
- data/spec/controller/authorization_spec.rb +45 -45
- data/spec/controller/captcha_spec.rb +18 -18
- data/spec/controller/comments_spec.rb +16 -14
- data/spec/controller/items_spec.rb +16 -17
- data/spec/i18n/i18n_spec.rb +5 -5
- data/spec/misc/kit_text_utils_spec.rb +5 -5
- data/spec/misc/prepare_model_spec.rb +6 -6
- data/spec/misc/user_error_spec.rb +8 -8
- data/spec/models/{attachments_uploader_helper_spec.rb → attachments_helper_spec.rb} +46 -50
- data/spec/models/{attachments_uploader_helper_spec → attachments_helper_spec}/v1/a.txt +0 -0
- data/spec/models/{attachments_uploader_helper_spec → attachments_helper_spec}/v1/b.txt +0 -0
- data/spec/models/{attachments_uploader_helper_spec → attachments_helper_spec}/v2/a.txt +0 -0
- data/spec/models/attachments_spec.rb +4 -7
- data/spec/models/authorization_spec.rb +15 -15
- data/spec/models/authorized_object_spec.rb +75 -75
- data/spec/models/item_spec.rb +44 -40
- data/spec/models/role_spec.rb +4 -4
- data/spec/models/tags_spec.rb +47 -21
- data/spec/models/uploader_spec.rb +13 -23
- data/spec/mongodb_model/text_processor_spec.rb +26 -0
- data/spec/spec_helper.rb +6 -4
- data/spec/spec_helper/factories.rb +3 -3
- data/spec/spec_helper/user.rb +4 -7
- data/spec/text_utils/code_highlighter_spec.rb +7 -7
- data/spec/text_utils/custom_markdown_spec.rb +14 -14
- data/spec/text_utils/format_qualifier_spec.rb +6 -6
- data/spec/text_utils/html_sanitizer_spec.rb +15 -15
- data/spec/text_utils/markdown_spec.rb +17 -17
- data/spec/text_utils/pipe_spec.rb +5 -5
- data/spec/text_utils/spec_helper.rb +1 -1
- data/spec/text_utils/text_processor_shared.rb +1 -1
- data/spec/text_utils/truncate_spec.rb +5 -5
- metadata +118 -114
- data/lib/kit/models/attachment_uploader.rb +0 -15
- data/lib/kit/models/file_uploader.rb +0 -26
- data/lib/kit/models_after.rb +0 -27
- data/lib/kit/mongoid.rb +0 -22
- data/lib/kit/mongoid/rad_miscellaneous.rb +0 -36
- data/lib/kit/mongoid/text_processor.rb +0 -44
- data/spec/mongoid/basic_spec.rb +0 -36
data/lib/components/captcha.rb
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
class Rad::Captcha
|
2
2
|
attr_accessor :public_key, :private_key, :timeout, :enabled, :verify_url
|
3
|
-
attr_required :private_key, :public_key
|
3
|
+
attr_required :private_key, :public_key
|
4
4
|
def enabled?; !!enabled end
|
5
|
-
|
6
|
-
def verify request, params
|
5
|
+
|
6
|
+
def verify request, params
|
7
7
|
recaptcha = nil
|
8
8
|
Timeout::timeout(timeout || 3) do
|
9
9
|
recaptcha = Net::HTTP.post_form URI.parse(verify_url), {
|
@@ -19,6 +19,6 @@ class Rad::Captcha
|
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
22
|
-
rad.register :captcha, depends_on: [:template, :controller] do
|
22
|
+
rad.register :captcha, depends_on: [:template, :controller] do
|
23
23
|
Rad::Captcha.new
|
24
24
|
end
|
data/lib/components/kit.rb
CHANGED
@@ -1,16 +1,16 @@
|
|
1
|
-
rad.register :kit, depends_on: [:web, :assets, :common_interface, :models] do
|
1
|
+
rad.register :kit, depends_on: [:web, :assets, :common_interface, :models] do
|
2
2
|
require 'kit/kit'
|
3
|
-
|
3
|
+
|
4
4
|
rad.router.default_url = '/'
|
5
5
|
(rad.face.availiable_layouts[:default] ||= []) << :default
|
6
|
-
|
7
|
-
rad.configure :web, "#{__FILE__}/../../.." do |c|
|
6
|
+
|
7
|
+
rad.configure :web, "#{__FILE__}/../../.." do |c|
|
8
8
|
c.routes
|
9
9
|
c.locales
|
10
10
|
c.template_paths 'app/views'
|
11
11
|
c.asset_paths 'app/static'
|
12
12
|
c.autoload_paths 'app'
|
13
13
|
end
|
14
|
-
|
14
|
+
|
15
15
|
Rad::Kit.new
|
16
16
|
end
|
data/lib/components/kit.yml
CHANGED
data/lib/components/models.rb
CHANGED
@@ -2,19 +2,20 @@ module Rad::Controller::Authorized
|
|
2
2
|
inherited do
|
3
3
|
helper_method :can?, :owner?
|
4
4
|
end
|
5
|
-
|
5
|
+
|
6
6
|
module ClassMethods
|
7
7
|
def require_permission operation, *args, &object_proc
|
8
|
-
operation
|
9
|
-
|
8
|
+
operation.must_be.a Symbol
|
9
|
+
|
10
10
|
options = args.extract_options!
|
11
11
|
# object_proc = args.size > 0 ? args.first : lambda{}
|
12
12
|
object_proc ||= lambda{|controller|}
|
13
|
-
|
14
|
-
method = "require_permission_#{operation}"
|
13
|
+
|
14
|
+
method = "require_permission_#{operation}".to_sym
|
15
15
|
define_method method do
|
16
16
|
require_permission operation, instance_eval(&object_proc)
|
17
17
|
end
|
18
|
+
|
18
19
|
before method, options
|
19
20
|
end
|
20
21
|
end
|
@@ -22,7 +23,7 @@ module Rad::Controller::Authorized
|
|
22
23
|
protected
|
23
24
|
def can? *args
|
24
25
|
rad.user.can? *args
|
25
|
-
end
|
26
|
+
end
|
26
27
|
|
27
28
|
def owner? *args
|
28
29
|
rad.user.owner? *args
|
@@ -37,7 +38,7 @@ module Rad::Controller::Authorized
|
|
37
38
|
end
|
38
39
|
|
39
40
|
def require_permission operation, object = nil
|
40
|
-
operation
|
41
|
+
operation.must_be.a Symbol
|
41
42
|
|
42
43
|
unless rad.user.can? operation, object
|
43
44
|
rad.logger.warn "RAD access denied, #{rad.user.name} hasn't rights to #{operation}!"
|
@@ -45,7 +46,7 @@ module Rad::Controller::Authorized
|
|
45
46
|
end
|
46
47
|
end
|
47
48
|
|
48
|
-
def access_denied!
|
49
|
+
def access_denied!
|
49
50
|
raise_user_error t(:access_denied)
|
50
51
|
end
|
51
52
|
end
|
@@ -3,19 +3,19 @@ module Rad::Controller::Captcha
|
|
3
3
|
around :ensure_registered_or_human
|
4
4
|
end
|
5
5
|
|
6
|
-
protected
|
7
|
-
def ensure_registered_or_human
|
6
|
+
protected
|
7
|
+
def ensure_registered_or_human
|
8
8
|
if rad.captcha.enabled? and rad.user.anonymous? and !request.get?
|
9
|
-
# right now we support capcha for :js format only
|
10
|
-
if request.from_browser? and params.format == 'js'
|
9
|
+
# right now we support capcha for :js format only
|
10
|
+
if request.from_browser? and params.format == 'js'
|
11
11
|
if rad.captcha.verify request, params
|
12
12
|
yield
|
13
13
|
else
|
14
14
|
render '/kit/captcha/action'
|
15
15
|
end
|
16
|
-
else
|
16
|
+
else
|
17
17
|
raise_user_error t(:registered_user_or_human_required)
|
18
|
-
end
|
18
|
+
end
|
19
19
|
else
|
20
20
|
yield
|
21
21
|
end
|
@@ -3,10 +3,10 @@ module Rad::Controller::Localized
|
|
3
3
|
before :prepare_locale
|
4
4
|
end
|
5
5
|
|
6
|
-
protected
|
6
|
+
protected
|
7
7
|
def prepare_locale
|
8
8
|
language = rad.environment.language
|
9
|
-
I18n.locale = params.l || language
|
9
|
+
I18n.locale = params.l || language
|
10
10
|
# Delete l from params if language is the same as default
|
11
11
|
params.delete :l if params.l == language
|
12
12
|
end
|
data/lib/kit/gems.rb
CHANGED
@@ -1,19 +1,23 @@
|
|
1
1
|
# core gems
|
2
|
-
gem 'i18n', '0.5
|
3
|
-
gem 'redcarpet', '1.17
|
4
|
-
gem 'sanitize', '1.2
|
5
|
-
gem 'stringex', '1.2
|
6
|
-
gem 'state_machine', '0.10
|
2
|
+
gem 'i18n', '~> 0.5'
|
3
|
+
gem 'redcarpet', '~> 1.17'
|
4
|
+
gem 'sanitize', '~> 1.2'
|
5
|
+
gem 'stringex', '~> 1.2'
|
6
|
+
# gem 'state_machine', '~> 0.10'
|
7
|
+
gem 'nokogiri', '~> 1.4'
|
7
8
|
# gem 'factory_girl', '1.3.3'
|
8
|
-
gem 'recaptcha', '0.3
|
9
|
+
gem 'recaptcha', '~> 0.3'
|
10
|
+
gem 'mini_magick', '~> 3.2'
|
9
11
|
|
10
|
-
if respond_to? :fake_gem
|
12
|
+
if respond_to? :fake_gem
|
11
13
|
fake_gem 'rad_core'
|
12
14
|
fake_gem 'rad_common_interface'
|
13
15
|
fake_gem 'rad_assets'
|
14
|
-
fake_gem '
|
16
|
+
fake_gem 'mongodb_model'
|
15
17
|
end
|
16
18
|
|
19
|
+
# TODO2 remove dependencies
|
20
|
+
|
17
21
|
# old
|
18
22
|
# gem 'bluecloth', '2.0.9'
|
19
23
|
# gem 'paperclip', '2.3.1.1'
|
data/lib/kit/i18n.rb
CHANGED
@@ -2,15 +2,15 @@
|
|
2
2
|
# gem 'i18n', '>= 0.4.1'
|
3
3
|
# require 'i18n'
|
4
4
|
|
5
|
-
require "i18n/backend/pluralization"
|
5
|
+
require "i18n/backend/pluralization"
|
6
6
|
I18n::Backend::Simple.send(:include, I18n::Backend::Pluralization)
|
7
7
|
|
8
8
|
I18n.load_path += Dir["#{__FILE__.dirname}/i18n/locales/*/*.{rb,yml}"]
|
9
9
|
|
10
10
|
|
11
|
-
#
|
11
|
+
#
|
12
12
|
# Helpers for Rad
|
13
|
-
#
|
13
|
+
#
|
14
14
|
[Rad::Controller::Abstract, Rad::Controller::Context].each do |klass|
|
15
15
|
klass.delegate :t, to: I18n
|
16
16
|
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
# {
|
2
2
|
# :'ru' => {
|
3
|
-
# pluralize: lambda { |n|
|
3
|
+
# pluralize: lambda { |n|
|
4
4
|
# # Правило плюрализации для русского языка, взято из CLDR, http://unicode.org/cldr/
|
5
5
|
# #
|
6
6
|
# #
|
@@ -19,7 +19,7 @@
|
|
19
19
|
# # :other = 1.31, 2.31, 5.31...
|
20
20
|
# modulo10 = n.modulo(10)
|
21
21
|
# modulo100 = n.modulo(100)
|
22
|
-
#
|
22
|
+
#
|
23
23
|
# if modulo10 == 1 && modulo100 != 11
|
24
24
|
# :one
|
25
25
|
# elsif (modulo10 == 2 || modulo10 == 3 || modulo10 == 4) && !(modulo100 == 12 || modulo100 == 13 || modulo100 == 14)
|
@@ -54,7 +54,7 @@
|
|
54
54
|
# :few = 2-4, 22-24, 32-34...
|
55
55
|
# :many = 0, 5-20, 25-30, 35-40...
|
56
56
|
# :other = 1.31, 2.31, 5.31...
|
57
|
-
n % 10 == 1 && n % 100 != 11 ? :one : [2, 3, 4].include?(n % 10) && ![12, 13, 14].include?(n % 100) ? :few : n % 10 == 0 || [5, 6, 7, 8, 9].include?(n % 10) || [11, 12, 13, 14].include?(n % 100) ? :many : :other
|
57
|
+
n % 10 == 1 && n % 100 != 11 ? :one : [2, 3, 4].include?(n % 10) && ![12, 13, 14].include?(n % 100) ? :few : n % 10 == 0 || [5, 6, 7, 8, 9].include?(n % 10) || [11, 12, 13, 14].include?(n % 100) ? :many : :other
|
58
58
|
}
|
59
59
|
}
|
60
60
|
}
|
data/lib/kit/kit.rb
CHANGED
@@ -2,9 +2,9 @@ require 'kit/support'
|
|
2
2
|
|
3
3
|
# Configs
|
4
4
|
class Rad::Kit
|
5
|
-
attr_accessor :default_item, :
|
6
|
-
attr_required :tags_count
|
7
|
-
def items; @items ||= [] end
|
5
|
+
attr_accessor :default_item, :tags_count, :use_code_highlighter
|
6
|
+
attr_required :tags_count
|
7
|
+
def items; @items ||= [] end
|
8
8
|
end
|
9
9
|
|
10
10
|
rad.router.class.class_eval do
|
@@ -12,21 +12,21 @@ rad.router.class.class_eval do
|
|
12
12
|
attr_required :default_url
|
13
13
|
end
|
14
14
|
|
15
|
-
rad.config.class.class_eval do
|
15
|
+
rad.config.class.class_eval do
|
16
16
|
def custom_roles; @custom_roles ||= [] end
|
17
17
|
def permissions; @permissions ||= {} end
|
18
18
|
def default_viewers; @default_viewers ||= [] end
|
19
19
|
end
|
20
20
|
|
21
21
|
# Kit
|
22
|
-
#
|
22
|
+
#
|
23
23
|
# TODO3 move :text_utils to standalone gem
|
24
|
-
%w(
|
24
|
+
%w(
|
25
25
|
support
|
26
26
|
controller
|
27
27
|
i18n
|
28
28
|
kit_text_utils
|
29
|
-
|
29
|
+
|
30
30
|
misc/prepare_model
|
31
31
|
misc/user_error
|
32
32
|
).each{|f| require "kit/#{f}"}
|
data/lib/kit/kit_text_utils.rb
CHANGED
@@ -3,20 +3,20 @@ require 'text_utils'
|
|
3
3
|
module TextUtils
|
4
4
|
class << self
|
5
5
|
# TODO2 rename
|
6
|
-
def markup data
|
6
|
+
def markup data
|
7
7
|
ps = []
|
8
8
|
ps << EnsureUtf
|
9
9
|
ps << HtmlSanitizer
|
10
|
-
ps << FormatQualifier
|
10
|
+
ps << FormatQualifier
|
11
11
|
ps << CodeHighlighter if rad.config.use_code_highlighter
|
12
12
|
ps << CustomMarkdown
|
13
|
-
ps << Markdown
|
13
|
+
ps << Markdown
|
14
14
|
|
15
15
|
markup = Pipe.new *ps
|
16
16
|
markup.call data
|
17
17
|
end
|
18
|
-
|
19
|
-
|
18
|
+
|
19
|
+
|
20
20
|
def truncate data, length
|
21
21
|
truncate = Pipe.new(
|
22
22
|
EnsureUtf,
|
@@ -26,5 +26,5 @@ module TextUtils
|
|
26
26
|
)
|
27
27
|
truncate.call data
|
28
28
|
end
|
29
|
-
end
|
29
|
+
end
|
30
30
|
end
|
@@ -1,12 +1,12 @@
|
|
1
1
|
Rad::Controller::Abstract::ClassMethods.class_eval do
|
2
2
|
def prepare_model aclass, opt = {}
|
3
3
|
opt = opt.symbolize_keys
|
4
|
-
id = opt.delete(:id) || :id
|
4
|
+
id = opt.delete(:id) || :id
|
5
5
|
variable = opt.delete(:variable) || aclass.alias.underscore
|
6
|
-
|
6
|
+
|
7
7
|
finder = opt.delete(:finder) || :find!
|
8
|
-
|
9
|
-
method = "prepare_#{variable}"
|
8
|
+
|
9
|
+
method = "prepare_#{variable}".to_sym
|
10
10
|
define_method method do
|
11
11
|
model = aclass.send finder, params[id]
|
12
12
|
instance_variable_set "@#{variable}", model
|
data/lib/kit/models.rb
CHANGED
@@ -1,8 +1,25 @@
|
|
1
1
|
require 'kit/support'
|
2
|
-
require 'kit/
|
3
|
-
require 'state_machine'
|
4
|
-
StateMachine::Integrations::Mongoid.defaults[:action] = nil
|
2
|
+
require 'kit/mongodb_model'
|
5
3
|
|
6
4
|
class Rad::Models
|
7
|
-
attr_accessor :
|
8
|
-
|
5
|
+
attr_accessor :db
|
6
|
+
attr_required :db
|
7
|
+
|
8
|
+
attr_accessor :fs
|
9
|
+
attr_required :fs
|
10
|
+
end
|
11
|
+
|
12
|
+
%w(
|
13
|
+
config
|
14
|
+
tags
|
15
|
+
tags_mixin
|
16
|
+
miscellaneous
|
17
|
+
role
|
18
|
+
authorized
|
19
|
+
authorized_object
|
20
|
+
base_file
|
21
|
+
attachment_file
|
22
|
+
attachments_helper
|
23
|
+
).each{|n| require "kit/models/#{n}"}
|
24
|
+
|
25
|
+
Mongo::Model.inherit Mongo::Model::AttachmentsHelper, Models::Miscellaneous
|
@@ -0,0 +1,27 @@
|
|
1
|
+
class Models::AttachmentFile < Models::BaseFile
|
2
|
+
rad.extension :attachment_file_path, self do
|
3
|
+
define_method :build_path do |*args|
|
4
|
+
"#{rad.models.fs['prefix']}/#{model_id}" + build_standard_path(*args)
|
5
|
+
end
|
6
|
+
|
7
|
+
define_method :build_url do |*args|
|
8
|
+
"#{rad.models.fs['prefix']}/#{model_id}" + build_standard_url(*args)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
version :icon do
|
13
|
+
def process &block
|
14
|
+
mini_magic block do |image|
|
15
|
+
image.resize '50x50'
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
version :thumb do
|
21
|
+
def process &block
|
22
|
+
mini_magic block do |image|
|
23
|
+
image.resize '150x150'
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -1,79 +1,85 @@
|
|
1
|
-
module
|
2
|
-
extend ActiveSupport::Concern
|
3
|
-
|
1
|
+
module Mongo::Model::AttachmentsHelper
|
4
2
|
class FileHelper
|
5
3
|
attr_reader :object
|
6
4
|
def initialize object
|
7
5
|
@object = object
|
8
6
|
end
|
9
|
-
|
7
|
+
|
10
8
|
def file?
|
11
|
-
object.is_a?(Hash) or object.is_a?(IO)
|
9
|
+
object.is_a?(Hash) or object.is_a?(IO) or object.is_a?(Vfs::File)
|
12
10
|
end
|
13
|
-
|
11
|
+
|
14
12
|
def name
|
15
13
|
if object.is_a?(Hash)
|
16
14
|
object['filename'] || object[:filename]
|
17
15
|
elsif object.is_a?(IO)
|
18
16
|
File.basename(object.path)
|
17
|
+
elsif object.is_a?(Vfs::File)
|
18
|
+
object.name
|
19
19
|
else
|
20
20
|
object
|
21
21
|
end
|
22
22
|
end
|
23
23
|
end
|
24
|
-
|
24
|
+
|
25
25
|
def get_attachments association_name, field_name
|
26
26
|
send(association_name).
|
27
|
-
sort{|a, b| a.send(field_name).name <=> b.send(field_name).name}.
|
28
|
-
collect{|o| {name: o.send(field_name).name, url: o.send(field_name).url}.to_openobject}
|
27
|
+
sort{|a, b| a.send(field_name).file.name <=> b.send(field_name).file.name}.
|
28
|
+
collect{|o| {name: o.send(field_name).file.name, url: o.send(field_name).url}.to_openobject}
|
29
29
|
end
|
30
|
-
|
31
|
-
def set_attachments association_name, field_name, values
|
30
|
+
|
31
|
+
def set_attachments association_name, field_name, values, &block
|
32
32
|
association = send(association_name)
|
33
|
-
existing_names = association.collect{|o| o.send(field_name).name}.sort
|
34
|
-
|
35
|
-
add = values.select do |o|
|
33
|
+
existing_names = association.collect{|o| o.send(field_name).file.name}.sort
|
34
|
+
|
35
|
+
add = values.select do |o|
|
36
36
|
h = FileHelper.new o
|
37
37
|
h.file? and !existing_names.include?(h.name)
|
38
|
-
end
|
39
|
-
update = values.select do |o|
|
38
|
+
end
|
39
|
+
update = values.select do |o|
|
40
40
|
h = FileHelper.new o
|
41
|
-
h.file? and existing_names.include?(h.name)
|
42
|
-
end
|
41
|
+
h.file? and existing_names.include?(h.name)
|
42
|
+
end
|
43
43
|
remove = association.select do |model|
|
44
44
|
values.none? do |o|
|
45
45
|
h = FileHelper.new o
|
46
46
|
model.send(field_name).name == h.name
|
47
47
|
end
|
48
|
-
end
|
48
|
+
end
|
49
49
|
|
50
|
-
add.each
|
50
|
+
add.each do |file|
|
51
|
+
model = block.call
|
52
|
+
model._parent = self
|
53
|
+
model.send "#{field_name}=", file
|
54
|
+
association << model
|
55
|
+
end
|
51
56
|
update.each do |file|
|
52
57
|
h = FileHelper.new file
|
53
58
|
association.each do |model|
|
54
|
-
if model.send(field_name).name == h.name
|
59
|
+
if model.send(field_name).file.name == h.name
|
55
60
|
model.send "#{field_name}=", file
|
56
61
|
break
|
57
62
|
end
|
58
63
|
end
|
59
|
-
end
|
64
|
+
end
|
60
65
|
remove.each do |model|
|
61
|
-
association.
|
66
|
+
association.delete model
|
67
|
+
# association.where(_id: model._id).destroy_all
|
62
68
|
end
|
63
69
|
end
|
64
70
|
|
65
71
|
module ClassMethods
|
66
|
-
def
|
72
|
+
def mount_attachments association_name, field_name, &block
|
67
73
|
define_method "#{association_name}_as_attachments" do
|
68
74
|
get_attachments association_name, field_name
|
69
75
|
end
|
70
|
-
|
76
|
+
|
71
77
|
define_method "#{association_name}_as_attachments=" do |values|
|
72
|
-
set_attachments association_name, field_name, values
|
78
|
+
set_attachments association_name, field_name, values, &block
|
73
79
|
end
|
74
|
-
|
80
|
+
|
75
81
|
# we can't allow to destroy model with changed attachments because it's too complicated to support this case.
|
76
|
-
before_destroy{|o| raise "Can't destroy item with changed attachments!" if o.changes.include? association_name}
|
82
|
+
# before_destroy{|o| raise "Can't destroy item with changed attachments!" if o.changes.include? association_name}
|
77
83
|
end
|
78
84
|
end
|
79
85
|
end
|