we5-browsercms 3.0.5 → 3.0.5.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/app/models/attachment.rb +34 -34
- data/browsercms.gemspec +1 -1
- data/lib/cms/behaviors/attaching.rb +16 -16
- data/lib/cms/behaviors/versioning.rb +30 -30
- metadata +2 -2
data/app/models/attachment.rb
CHANGED
@@ -23,28 +23,28 @@ class Attachment < ActiveRecord::Base
|
|
23
23
|
before_save :process_section
|
24
24
|
|
25
25
|
after_save :write_temp_file_to_storage_location
|
26
|
-
after_save :clear_ivars
|
27
|
-
|
26
|
+
after_save :clear_ivars
|
27
|
+
|
28
28
|
#----- Associations ----------------------------------------------------------
|
29
29
|
|
30
30
|
has_one :section_node, :as => :node
|
31
31
|
|
32
32
|
#----- Validations -----------------------------------------------------------
|
33
33
|
|
34
|
-
validates_presence_of :temp_file,
|
34
|
+
validates_presence_of :temp_file,
|
35
35
|
:message => "You must upload a file", :on => :create
|
36
36
|
validates_presence_of :file_path
|
37
37
|
validates_uniqueness_of :file_path
|
38
38
|
validates_presence_of :section_id
|
39
|
-
|
39
|
+
|
40
40
|
#----- Virtual Attributes ----------------------------------------------------
|
41
|
-
|
41
|
+
|
42
42
|
def section_id
|
43
43
|
@section_id ||= section_node ? section_node.section_id : nil
|
44
44
|
end
|
45
45
|
|
46
46
|
def section_id=(section_id)
|
47
|
-
if @section_id != section_id
|
47
|
+
if @section_id != section_id
|
48
48
|
dirty!
|
49
49
|
@section_id = section_id
|
50
50
|
end
|
@@ -61,38 +61,38 @@ class Attachment < ActiveRecord::Base
|
|
61
61
|
@section = section
|
62
62
|
end
|
63
63
|
end
|
64
|
-
|
64
|
+
|
65
65
|
#----- Callbacks Methods -----------------------------------------------------
|
66
|
-
|
66
|
+
|
67
67
|
def make_dirty_if_temp_file
|
68
68
|
dirty! if temp_file
|
69
69
|
end
|
70
|
-
|
71
|
-
def prepend_file_path_with_slash
|
70
|
+
|
71
|
+
def prepend_file_path_with_slash
|
72
72
|
unless file_path.blank?
|
73
73
|
self.file_path = "/#{file_path}" unless file_path =~ /^\//
|
74
74
|
end
|
75
75
|
end
|
76
|
-
|
76
|
+
|
77
77
|
def extract_file_extension_from_file_name
|
78
78
|
if file_name && file_name['.']
|
79
79
|
self.file_extension = file_name.split('.').last.to_s.downcase
|
80
|
-
end
|
80
|
+
end
|
81
81
|
end
|
82
|
-
|
82
|
+
|
83
83
|
def extract_file_type_from_temp_file
|
84
84
|
unless temp_file.blank?
|
85
85
|
self.file_type = temp_file.content_type
|
86
|
-
end
|
86
|
+
end
|
87
87
|
end
|
88
|
-
|
89
|
-
def extract_file_size_from_temp_file
|
88
|
+
|
89
|
+
def extract_file_size_from_temp_file
|
90
90
|
unless temp_file.blank?
|
91
91
|
self.file_size = temp_file.size
|
92
|
-
end
|
92
|
+
end
|
93
93
|
end
|
94
94
|
|
95
|
-
# The file will be stored on disk at
|
95
|
+
# The file will be stored on disk at
|
96
96
|
# Attachment.storage_location/year/month/day/sha1
|
97
97
|
# The sha1 is a 40 character hash based on the original_filename
|
98
98
|
# of the file uploaded and the current time
|
@@ -109,9 +109,9 @@ class Attachment < ActiveRecord::Base
|
|
109
109
|
section_node.move_to_end(Section.find(section_id))
|
110
110
|
else
|
111
111
|
build_section_node(:node => self, :section_id => section_id)
|
112
|
-
end
|
112
|
+
end
|
113
113
|
end
|
114
|
-
|
114
|
+
|
115
115
|
def write_temp_file_to_storage_location
|
116
116
|
unless temp_file.blank?
|
117
117
|
FileUtils.mkdir_p File.dirname(full_file_location)
|
@@ -120,33 +120,33 @@ class Attachment < ActiveRecord::Base
|
|
120
120
|
else
|
121
121
|
open(full_file_location, 'w') {|f| f << temp_file.read }
|
122
122
|
end
|
123
|
-
|
123
|
+
|
124
124
|
if Cms.attachment_file_permission
|
125
125
|
FileUtils.chmod Cms.attachment_file_permission, full_file_location
|
126
126
|
end
|
127
127
|
end
|
128
|
-
end
|
129
|
-
|
128
|
+
end
|
129
|
+
|
130
130
|
def clear_ivars
|
131
131
|
@temp_file = nil
|
132
132
|
@section = nil
|
133
133
|
@section_id = nil
|
134
134
|
end
|
135
|
-
|
135
|
+
|
136
136
|
#----- Class Methods ---------------------------------------------------------
|
137
|
-
|
137
|
+
|
138
138
|
def self.storage_location
|
139
139
|
@storage_location ||= File.join(Rails.root, "/tmp/uploads")
|
140
140
|
end
|
141
|
-
|
141
|
+
|
142
142
|
def self.storage_location=(storage_location)
|
143
143
|
@storage_location = storage_location
|
144
|
-
end
|
145
|
-
|
144
|
+
end
|
145
|
+
|
146
146
|
def self.find_live_by_file_path(file_path)
|
147
|
-
Attachment.published.not_archived.first(:conditions => {:file_path => file_path})
|
148
|
-
end
|
149
|
-
|
147
|
+
::Attachment.published.not_archived.first(:conditions => {:file_path => file_path})
|
148
|
+
end
|
149
|
+
|
150
150
|
#----- Instance Methods ------------------------------------------------------
|
151
151
|
|
152
152
|
def file_name
|
@@ -156,7 +156,7 @@ class Attachment < ActiveRecord::Base
|
|
156
156
|
def name
|
157
157
|
file_name
|
158
158
|
end
|
159
|
-
|
159
|
+
|
160
160
|
def icon
|
161
161
|
{
|
162
162
|
:doc => %w[doc],
|
@@ -178,9 +178,9 @@ class Attachment < ActiveRecord::Base
|
|
178
178
|
def public?
|
179
179
|
section ? section.public? : false
|
180
180
|
end
|
181
|
-
|
181
|
+
|
182
182
|
def full_file_location
|
183
|
-
File.join(Attachment.storage_location, file_location)
|
183
|
+
File.join(::Attachment.storage_location, file_location)
|
184
184
|
end
|
185
185
|
|
186
186
|
# Forces this record to be changed, even if nothing has changed
|
data/browsercms.gemspec
CHANGED
@@ -2,33 +2,33 @@ module Cms
|
|
2
2
|
module Behaviors
|
3
3
|
module Attaching
|
4
4
|
SANITIZATION_REGEXES = [ [/\s/, '_'], [/[&+()]/, '-'], [/[=?!'"{}\[\]#<>%]/, ''] ]
|
5
|
-
|
5
|
+
|
6
6
|
def self.included(model_class)
|
7
7
|
model_class.extend(MacroMethods)
|
8
8
|
end
|
9
|
-
module MacroMethods
|
9
|
+
module MacroMethods
|
10
10
|
def belongs_to_attachment?
|
11
11
|
!!@belongs_to_attachment
|
12
12
|
end
|
13
13
|
def belongs_to_attachment(options={})
|
14
14
|
@belongs_to_attachment = true
|
15
15
|
include InstanceMethods
|
16
|
-
before_validation :process_attachment
|
16
|
+
before_validation :process_attachment
|
17
17
|
before_save :update_attachment_if_changed
|
18
18
|
after_save :clear_attachment_ivars
|
19
|
-
belongs_to :attachment, :dependent => :destroy
|
20
|
-
|
19
|
+
belongs_to :attachment, :dependent => :destroy, :class_name => "::Attachment"
|
20
|
+
|
21
21
|
validates_each :attachment_file do |record, attr, value|
|
22
22
|
if record.attachment && !record.attachment.valid?
|
23
23
|
record.attachment.errors.each do |err_field, err_value|
|
24
24
|
if err_field.to_sym == :file_path
|
25
25
|
record.errors.add(:attachment_file_path, err_value)
|
26
|
-
else
|
26
|
+
else
|
27
27
|
record.errors.add(:attachment_file, err_value)
|
28
28
|
end
|
29
|
-
end
|
29
|
+
end
|
30
30
|
end
|
31
|
-
end
|
31
|
+
end
|
32
32
|
end
|
33
33
|
end
|
34
34
|
module InstanceMethods
|
@@ -92,10 +92,10 @@ module Cms
|
|
92
92
|
unless attachment_section_id.blank?
|
93
93
|
errors.add(:attachment_file, "You must upload a file")
|
94
94
|
return false
|
95
|
-
end
|
95
|
+
end
|
96
96
|
else
|
97
|
-
build_attachment if attachment.nil?
|
98
|
-
attachment.temp_file = attachment_file
|
97
|
+
build_attachment if attachment.nil?
|
98
|
+
attachment.temp_file = attachment_file
|
99
99
|
set_attachment_file_path
|
100
100
|
if attachment.file_path.blank?
|
101
101
|
errors.add(:attachment_file_path, "File Name is required for attachment")
|
@@ -113,7 +113,7 @@ module Cms
|
|
113
113
|
@attachment_file = nil
|
114
114
|
@attachment_file_path = nil
|
115
115
|
@attachment_section_id = nil
|
116
|
-
@attachment_section = nil
|
116
|
+
@attachment_section = nil
|
117
117
|
end
|
118
118
|
|
119
119
|
# Override this method if you would like to override the way the section is set
|
@@ -168,16 +168,16 @@ module Cms
|
|
168
168
|
(published? && live_version?) ? attachment_file_path : "/cms/attachments/#{attachment_id}?version=#{attachment_version}"
|
169
169
|
else
|
170
170
|
nil
|
171
|
-
end
|
171
|
+
end
|
172
172
|
end
|
173
|
-
|
173
|
+
|
174
174
|
# Forces this record to be changed, even if nothing has changed
|
175
175
|
# This is necessary if just the section.id has changed, for example
|
176
176
|
def dirty!
|
177
177
|
# Seems like a hack, is there a better way?
|
178
178
|
self.updated_at = Time.now
|
179
|
-
end
|
180
|
-
|
179
|
+
end
|
180
|
+
|
181
181
|
end
|
182
182
|
end
|
183
183
|
end
|
@@ -4,7 +4,7 @@ module Cms
|
|
4
4
|
def self.included(model_class)
|
5
5
|
model_class.extend(MacroMethods)
|
6
6
|
end
|
7
|
-
module MacroMethods
|
7
|
+
module MacroMethods
|
8
8
|
def versioned?
|
9
9
|
!!@is_versioned
|
10
10
|
end
|
@@ -24,7 +24,7 @@ module Cms
|
|
24
24
|
attr_accessor :revert_to_version
|
25
25
|
|
26
26
|
#Define the version class
|
27
|
-
const_set("Version", Class.new(ActiveRecord::Base)).class_eval do
|
27
|
+
const_set("Version", Class.new(ActiveRecord::Base)).class_eval do
|
28
28
|
class << self; attr_accessor :versioned_class end
|
29
29
|
|
30
30
|
def versioned_class
|
@@ -35,7 +35,7 @@ module Cms
|
|
35
35
|
end
|
36
36
|
def versioned_object
|
37
37
|
send(versioned_class.name.underscore.to_sym)
|
38
|
-
end
|
38
|
+
end
|
39
39
|
end
|
40
40
|
|
41
41
|
version_class.versioned_class = self
|
@@ -46,27 +46,27 @@ module Cms
|
|
46
46
|
|
47
47
|
end
|
48
48
|
end
|
49
|
-
module ClassMethods
|
49
|
+
module ClassMethods
|
50
50
|
def version_class
|
51
51
|
const_get "Version"
|
52
52
|
end
|
53
53
|
|
54
54
|
def version_class_name
|
55
|
-
"
|
56
|
-
end
|
55
|
+
"::#{name}::Version"
|
56
|
+
end
|
57
57
|
|
58
58
|
def version_foreign_key
|
59
59
|
@version_foreign_key
|
60
60
|
end
|
61
61
|
|
62
|
-
def version_table_name
|
62
|
+
def version_table_name
|
63
63
|
@version_table_name
|
64
64
|
end
|
65
65
|
|
66
66
|
def versioned_columns
|
67
|
-
@versioned_columns ||= (version_class.new.attributes.keys -
|
67
|
+
@versioned_columns ||= (version_class.new.attributes.keys -
|
68
68
|
(%w[id lock_version position version_comment created_at updated_at created_by_id updated_by_id type] + [version_foreign_key]))
|
69
|
-
end
|
69
|
+
end
|
70
70
|
end
|
71
71
|
module InstanceMethods
|
72
72
|
def initialize_version
|
@@ -77,7 +77,7 @@ module Cms
|
|
77
77
|
# First get the values from the draft
|
78
78
|
attrs = draft_attributes
|
79
79
|
|
80
|
-
# Now overwrite any changed values
|
80
|
+
# Now overwrite any changed values
|
81
81
|
self.class.versioned_columns.each do |col|
|
82
82
|
if(send("#{col}_changed?"))
|
83
83
|
attrs[col] = send(col)
|
@@ -85,7 +85,7 @@ module Cms
|
|
85
85
|
end
|
86
86
|
|
87
87
|
attrs[:version_comment] = @version_comment || default_version_comment
|
88
|
-
@version_comment = nil
|
88
|
+
@version_comment = nil
|
89
89
|
new_version = versions.build(attrs)
|
90
90
|
new_version.version = new_record? ? 1 : (draft.version.to_i + 1)
|
91
91
|
after_build_new_version(new_version) if respond_to?(:after_build_new_version)
|
@@ -97,7 +97,7 @@ module Cms
|
|
97
97
|
# Otherwise we need to use the draft
|
98
98
|
d = new_record? ? self : draft
|
99
99
|
self.class.versioned_columns.inject({}){|attrs, col| attrs[col] = d.send(col); attrs }
|
100
|
-
end
|
100
|
+
end
|
101
101
|
|
102
102
|
def default_version_comment
|
103
103
|
if new_record?
|
@@ -110,22 +110,22 @@ module Cms
|
|
110
110
|
def save(perform_validations=true)
|
111
111
|
transaction do
|
112
112
|
#logger.info "..... Calling valid?"
|
113
|
-
return false unless !perform_validations || valid?
|
114
|
-
|
113
|
+
return false unless !perform_validations || valid?
|
114
|
+
|
115
115
|
if changed?
|
116
116
|
#logger.info "..... Changes => #{changes.inspect}"
|
117
117
|
else
|
118
118
|
#logger.info "..... No Changes"
|
119
119
|
return true
|
120
120
|
end
|
121
|
-
|
121
|
+
|
122
122
|
#logger.info "..... Calling before_save"
|
123
123
|
return false if callback(:before_save) == false
|
124
124
|
|
125
125
|
if new_record?
|
126
126
|
#logger.info "..... Calling before_create"
|
127
127
|
return false if callback(:before_create) == false
|
128
|
-
else
|
128
|
+
else
|
129
129
|
#logger.info "..... Calling before_update"
|
130
130
|
return false if callback(:before_update) == false
|
131
131
|
end
|
@@ -142,12 +142,12 @@ module Cms
|
|
142
142
|
#logger.info "..... Calling after_save"
|
143
143
|
callback(:after_save)
|
144
144
|
end
|
145
|
-
|
145
|
+
|
146
146
|
if @publish_on_save
|
147
147
|
publish
|
148
148
|
@publish_on_save = nil
|
149
|
-
end
|
150
|
-
changed_attributes.clear
|
149
|
+
end
|
150
|
+
changed_attributes.clear
|
151
151
|
end
|
152
152
|
result
|
153
153
|
elsif new_version
|
@@ -158,11 +158,11 @@ module Cms
|
|
158
158
|
#logger.info "..... Calling after_update"
|
159
159
|
callback(:after_save)
|
160
160
|
end
|
161
|
-
|
161
|
+
|
162
162
|
if @publish_on_save
|
163
163
|
publish
|
164
164
|
@publish_on_save = nil
|
165
|
-
end
|
165
|
+
end
|
166
166
|
changed_attributes.clear
|
167
167
|
end
|
168
168
|
result
|
@@ -176,13 +176,13 @@ module Cms
|
|
176
176
|
end
|
177
177
|
|
178
178
|
def draft
|
179
|
-
versions.first(:order => "version desc")
|
179
|
+
versions.first(:order => "version desc")
|
180
180
|
end
|
181
|
-
|
181
|
+
|
182
182
|
def draft_version?
|
183
183
|
version == draft.version
|
184
184
|
end
|
185
|
-
|
185
|
+
|
186
186
|
def live_version
|
187
187
|
find_version(self.class.find(id).version)
|
188
188
|
end
|
@@ -194,7 +194,7 @@ module Cms
|
|
194
194
|
def current_version
|
195
195
|
find_version(self.version)
|
196
196
|
end
|
197
|
-
|
197
|
+
|
198
198
|
def find_version(number)
|
199
199
|
versions.first(:conditions => { :version => number })
|
200
200
|
end
|
@@ -225,7 +225,7 @@ module Cms
|
|
225
225
|
changed_attrs.clear
|
226
226
|
end
|
227
227
|
|
228
|
-
obj
|
228
|
+
obj
|
229
229
|
end
|
230
230
|
|
231
231
|
def revert
|
@@ -239,15 +239,15 @@ module Cms
|
|
239
239
|
raise "Could not find version #{version}" unless revert_to_version
|
240
240
|
(self.class.versioned_columns - ["version"]).each do |a|
|
241
241
|
send("#{a}=", revert_to_version.send(a))
|
242
|
-
end
|
242
|
+
end
|
243
243
|
self.version_comment = "Reverted to version #{version}"
|
244
|
-
self
|
245
|
-
end
|
244
|
+
self
|
245
|
+
end
|
246
246
|
|
247
247
|
def revert_to(version)
|
248
248
|
revert_to_without_save(version)
|
249
249
|
save
|
250
|
-
end
|
250
|
+
end
|
251
251
|
|
252
252
|
def version_comment
|
253
253
|
@version_comment
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: we5-browsercms
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.0.5
|
4
|
+
version: 3.0.5.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- BrowserMedia
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-12-14 00:00:00 +01:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|