11class AssetFieldType < FieldType
2- VALIDATION_TYPES = {
3- presence : :valid_presence_validation? ,
4- size : :valid_size_validation? ,
5- content_type : :valid_content_type_validation?
6- } . freeze
7-
82 attr_accessor :asset_file_name ,
93 :asset_content_type ,
104 :asset_file_size ,
115 :asset_updated_at ,
12- :field_name
6+ :asset
137
14- attr_reader :data , :validations , : dimensions
8+ attr_reader :dimensions
159
16- has_attached_file :asset
17- do_not_validate_attachment_file_type :asset
1810 before_save :extract_dimensions
1911
12+ do_not_validate_attachment_file_type :asset
2013 validates :asset , attachment_presence : true , if : :validate_presence?
14+ validate :validate_asset_size , if : :validate_size?
15+ validate :validate_asset_content_type , if : :validate_content_type?
2116
22- def validations = ( validations_hash )
23- @validations = validations_hash . deep_symbolize_keys
17+ def metadata = ( metadata_hash )
18+ @metadata = metadata_hash . deep_symbolize_keys
19+ Paperclip ::HasAttachedFile . define_on ( self . class , :asset , metadata )
2420 end
2521
2622 def data = ( data_hash )
@@ -40,10 +36,6 @@ def data
4036 }
4137 end
4238
43- def acceptable_validations?
44- valid_types? && valid_options?
45- end
46-
4739 def field_item_as_indexed_json_for_field_type ( field_item , options = { } )
4840 json = { }
4941 json [ mapping_field_name ] = asset_file_name
@@ -72,43 +64,53 @@ def extract_dimensions
7264 end
7365 end
7466
67+ def allowed_content_types
68+ validations [ :allowed_extensions ] . collect do |allowed_content_type |
69+ MimeMagic . by_extension ( allowed_content_type ) . type
70+ end
71+ end
72+
7573 def mapping_field_name
7674 "#{ field_name . parameterize ( '_' ) } _asset_file_name"
7775 end
7876
79- def valid_types?
80- validations . all? do |type , options |
81- VALIDATION_TYPES . include? ( type . to_sym )
82- end
77+ def validate_presence?
78+ @validations . key? :presence
8379 end
8480
85- def valid_options?
86- validations . all? do |type , options |
87- self . send ( VALIDATION_TYPES [ type ] )
88- end
81+ def attachment_size_validator
82+ AttachmentSizeValidator . new ( validations [ :size ] . merge ( attributes : :asset ) )
8983 end
9084
91- def validate_presence?
92- @validations . key? :presence
85+ def attachment_content_type_validator
86+ AttachmentContentTypeValidator . new ( { content_type : allowed_content_types } . merge ( attributes : :asset ) )
9387 end
9488
9589 alias_method :valid_presence_validation? , :validate_presence?
9690
97- def valid_size_validation ?
91+ def validate_size ?
9892 begin
99- AttachmentSizeValidator . new ( validations [ :size ] . merge ( attributes : :asset ) )
93+ attachment_size_validator
10094 true
10195 rescue ArgumentError , NoMethodError
10296 false
10397 end
10498 end
10599
106- def valid_content_type_validation ?
100+ def validate_content_type ?
107101 begin
108- AttachmentContentTypeValidator . new ( validations [ :content_type ] . merge ( attributes : :asset ) )
102+ attachment_content_type_validator
109103 true
110104 rescue ArgumentError , NoMethodError
111105 false
112106 end
113107 end
108+
109+ def validate_asset_size
110+ attachment_size_validator . validate_each ( self , :asset , asset )
111+ end
112+
113+ def validate_asset_content_type
114+ attachment_content_type_validator . validate_each ( self , :asset , asset )
115+ end
114116end
0 commit comments