Skip to content

Commit a74823d

Browse files
committed
Merge branch 'develop'
2 parents c838d37 + b9a23db commit a74823d

File tree

7 files changed

+296
-30
lines changed

7 files changed

+296
-30
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- if asset
2+
= image_tag(asset['style_urls'][config[:thumbnail_style]], height: '50px')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
- if asset
2+
:javascript
3+
document.addEventListener("DOMContentLoaded", function(event) {
4+
new Clipboard('#copy-asset-url');
5+
});
6+
7+
.asset-info.mdl-card.mdl-shadow--2dp
8+
.mdl-card__title
9+
%h2.mdl-card__title-text
10+
= image_tag(asset['url'], style: 'max-height: 200px;')
11+
.mdl-card__supporting-text
12+
%dl
13+
%dt Original Filename
14+
%dd
15+
= asset['file_name']
16+
%dt File Type
17+
%dd
18+
= asset['content_type']
19+
%dt File Size
20+
%dd
21+
= number_to_human_size(asset['file_size'])
22+
%dt Dimensions
23+
%dd
24+
= dimensions
25+
%dt Creator
26+
%dd
27+
= creator.fullname
28+
%dt Created
29+
%dd
30+
= created_at
31+
%dt Last Modified
32+
%dd
33+
= updated_at
34+
%dt URL
35+
%dd
36+
= link_to_asset
37+
.mdl-card__menu
38+
.mdl-button.mdl-button--icon.mdl-js-button.mdl-js-ripple-effect#copy-asset-url{data: {'clipboard-text': asset['url']}}
39+
%i.material-icons content_copy
40+
.mdl-tooltip{for: 'copy-asset-url'}
41+
Copy Asset URL
+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
module Plugins
2+
module Core
3+
class AssetInfoCell < Plugins::Core::Cell
4+
include ActionView::Helpers::NumberHelper
5+
6+
property :data
7+
property :content_item
8+
9+
def show
10+
render
11+
end
12+
13+
def index
14+
render
15+
end
16+
17+
private
18+
19+
def config
20+
@options[:config] || {}
21+
end
22+
23+
def asset
24+
data['asset']
25+
end
26+
27+
def dimensions
28+
"#{asset['dimensions']['width']} x #{asset['dimensions']['width']}"
29+
end
30+
31+
def creator
32+
content_item.creator
33+
end
34+
35+
def created_at
36+
content_item.created_at.to_formatted_s(:long_ordinal)
37+
end
38+
39+
def updated_at
40+
DateTime.parse(asset['updated_at']).to_formatted_s(:long_ordinal)
41+
end
42+
43+
def link_to_asset
44+
link_to(asset['url'], asset['url'])
45+
end
46+
end
47+
end
48+
end

app/models/asset_field_type.rb

+25-22
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ class AssetFieldType < FieldType
55
:asset_updated_at,
66
:asset
77

8-
attr_reader :dimensions
8+
attr_reader :dimensions,
9+
:existing_data
910

1011
before_save :extract_dimensions
1112

@@ -26,14 +27,16 @@ def data=(data_hash)
2627

2728
def data
2829
{
29-
'asset': {
30-
'file_name': asset_file_name,
31-
'url': asset.url,
32-
'dimensions': dimensions,
33-
'content_type': asset_content_type,
34-
'file_size': asset_file_size,
35-
'updated_at': asset_updated_at
36-
}
30+
'asset': {
31+
'file_name': asset_file_name,
32+
'url': asset.url,
33+
'style_urls': style_urls,
34+
'dimensions': dimensions,
35+
'content_type': asset_content_type,
36+
'file_size': asset_file_size,
37+
'updated_at': asset_updated_at
38+
},
39+
'asset_field_type_id': id
3740
}
3841
end
3942

@@ -59,8 +62,8 @@ def extract_dimensions
5962
unless tempfile.nil?
6063
geometry = Paperclip::Geometry.from_file(tempfile)
6164
@dimensions = {
62-
width: geometry.width.to_i,
63-
height: geometry.height.to_i
65+
width: geometry.width.to_i,
66+
height: geometry.height.to_i
6467
}
6568
end
6669
end
@@ -76,7 +79,7 @@ def mapping_field_name
7679
end
7780

7881
def validate_presence?
79-
@validations.key? :presence
82+
validations.key? :presence
8083
end
8184

8285
def attachment_size_validator
@@ -115,21 +118,21 @@ def validate_asset_content_type
115118
attachment_content_type_validator.validate_each(self, :asset, asset)
116119
end
117120

121+
def style_urls
122+
if existing_data.empty?
123+
(metadata[:styles].map { |key, value| [key, asset.url(key)] }).to_h
124+
else
125+
existing_data[:asset][:style_urls]
126+
end
127+
end
128+
118129
def existing_metadata
119130
metadata.except!(:existing_data)
120131

121-
unless @existing_data.empty?
122-
metadata[:path] = updated_url(@existing_data['asset']['url'])
132+
unless existing_data.empty?
133+
metadata[:path].gsub(":id", existing_data['asset_field_type_id']) if metadata[:path]
123134
end
124135

125136
metadata
126137
end
127-
128-
def updated_url(path)
129-
# Take the parse path of the existing URL and drop the first '/',
130-
# that will be added later and we don't want to duplicate it
131-
# Then remove the old file extension and replace it with the paperclipp'd new one
132-
new_path = URI.parse(path).path.slice(1..-1)
133-
new_path.gsub(File.extname(path), ".:extension")
134-
end
135138
end

app/models/asset_field_type.rb.orig

+154
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
class AssetFieldType < FieldType
2+
attr_accessor :asset_file_name,
3+
:asset_content_type,
4+
:asset_file_size,
5+
:asset_updated_at,
6+
:asset
7+
8+
attr_reader :dimensions,
9+
:existing_data
10+
11+
before_save :extract_dimensions
12+
13+
do_not_validate_attachment_file_type :asset
14+
validates :asset, attachment_presence: true, if: :validate_presence?
15+
validate :validate_asset_size, if: :validate_size?
16+
validate :validate_asset_content_type, if: :validate_content_type?
17+
18+
def metadata=(metadata_hash)
19+
@metadata = metadata_hash.deep_symbolize_keys
20+
@existing_data = metadata_hash[:existing_data]
21+
Paperclip::HasAttachedFile.define_on(self.class, :asset, existing_metadata)
22+
end
23+
24+
def data=(data_hash)
25+
self.asset = data_hash.deep_symbolize_keys[:asset]
26+
end
27+
28+
def data
29+
{
30+
<<<<<<< HEAD
31+
'asset': {
32+
'file_name': asset_file_name,
33+
'url': asset.url,
34+
'dimensions': dimensions,
35+
'content_type': asset_content_type,
36+
'file_size': asset_file_size,
37+
'updated_at': asset_updated_at
38+
},
39+
'asset_field_type_id': id
40+
=======
41+
'asset': {
42+
'file_name': asset_file_name,
43+
'url': asset.url,
44+
'style_urls': style_urls,
45+
'dimensions': dimensions,
46+
'content_type': asset_content_type,
47+
'file_size': asset_file_size,
48+
'updated_at': asset_updated_at
49+
}
50+
>>>>>>> Implement storage of thumbnail/style URLs, asset wizard info box, and asset thumbnail in index
51+
}
52+
end
53+
54+
def field_item_as_indexed_json_for_field_type(field_item, options = {})
55+
json = {}
56+
json[mapping_field_name] = asset_file_name
57+
json
58+
end
59+
60+
def mapping
61+
{name: mapping_field_name, type: :string, analyzer: :keyword}
62+
end
63+
64+
private
65+
66+
def image?
67+
asset_content_type =~ %r{^(image|(x-)?application)/(bmp|gif|jpeg|jpg|pjpeg|png|x-png)$}
68+
end
69+
70+
def extract_dimensions
71+
return unless image?
72+
tempfile = asset.queued_for_write[:original]
73+
unless tempfile.nil?
74+
geometry = Paperclip::Geometry.from_file(tempfile)
75+
@dimensions = {
76+
width: geometry.width.to_i,
77+
height: geometry.height.to_i
78+
}
79+
end
80+
end
81+
82+
def allowed_content_types
83+
validations[:allowed_extensions].collect do |allowed_content_type|
84+
MimeMagic.by_extension(allowed_content_type).type
85+
end
86+
end
87+
88+
def mapping_field_name
89+
"#{field_name.parameterize('_')}_asset_file_name"
90+
end
91+
92+
def validate_presence?
93+
validations.key? :presence
94+
end
95+
96+
def attachment_size_validator
97+
AttachmentSizeValidator.new(validations[:size].merge(attributes: :asset))
98+
end
99+
100+
def attachment_content_type_validator
101+
AttachmentContentTypeValidator.new({content_type: allowed_content_types}.merge(attributes: :asset))
102+
end
103+
104+
alias_method :valid_presence_validation?, :validate_presence?
105+
106+
def validate_size?
107+
begin
108+
attachment_size_validator
109+
true
110+
rescue ArgumentError, NoMethodError
111+
false
112+
end
113+
end
114+
115+
def validate_content_type?
116+
begin
117+
attachment_content_type_validator
118+
true
119+
rescue ArgumentError, NoMethodError
120+
false
121+
end
122+
end
123+
124+
def validate_asset_size
125+
attachment_size_validator.validate_each(self, :asset, asset)
126+
end
127+
128+
def validate_asset_content_type
129+
attachment_content_type_validator.validate_each(self, :asset, asset)
130+
end
131+
132+
def style_urls
133+
if existing_data.empty?
134+
(metadata[:styles].map { |key, value| [key, asset.url(key)] }).to_h
135+
else
136+
existing_data[:asset][:style_urls]
137+
end
138+
end
139+
140+
def existing_metadata
141+
metadata.except!(:existing_data)
142+
143+
<<<<<<< HEAD
144+
unless @existing_data.empty?
145+
metadata[:path].gsub(":id", @existing_data['asset_field_type_id']) if metadata[:path]
146+
=======
147+
unless existing_data.empty?
148+
metadata[:path] = updated_url(existing_data['asset']['url'])
149+
>>>>>>> Implement storage of thumbnail/style URLs, asset wizard info box, and asset thumbnail in index
150+
end
151+
152+
metadata
153+
end
154+
end

lib/cortex/plugins/core/version.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module Cortex
22
module Plugins
33
module Core
4-
VERSION = '0.4.5'
4+
VERSION = '0.4.6'
55
end
66
end
77
end

lib/tasks/cortex/core/media.rake

+25-7
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ namespace :cortex do
7373
"description": "Provide details and metadata that will enhance search or inform end-users.",
7474
"columns": [
7575
{
76-
"grid_width": 12,
76+
"grid_width": 6,
7777
"elements": [
7878
{
7979
"id": media.fields[1].id
@@ -95,6 +95,20 @@ namespace :cortex do
9595
"id": media.fields[5].id
9696
}
9797
]
98+
},
99+
{
100+
"grid_width": 6,
101+
"elements": [
102+
{
103+
"plugin": {
104+
"class_name": "plugins/core/asset_info",
105+
"render_method": "show",
106+
"data": {
107+
"field_id": media.fields.find_by_name('Asset').id
108+
}
109+
}
110+
},
111+
]
98112
}
99113
]
100114
}
@@ -118,12 +132,16 @@ namespace :cortex do
118132
"name": "Thumbnail",
119133
"cells": [{
120134
"field": {
121-
"method": "author_image"
122-
},
123-
"display": {
124-
"classes": [
125-
"circular"
126-
]
135+
"plugin": {
136+
"class_name": "plugins/core/asset_info",
137+
"render_method": "index",
138+
"data": {
139+
"field_id": media.fields.find_by_name('Asset').id
140+
},
141+
"config": {
142+
"thumbnail_style": "mini"
143+
}
144+
}
127145
}
128146
}]
129147
},

0 commit comments

Comments
 (0)