hancock_cms_gallery 1.0.0 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. checksums.yaml +4 -4
  2. data/app/controllers/concerns/hancock/gallery/load_gallery.rb +9 -1
  3. data/app/controllers/concerns/hancock/gallery/load_gallery_images.rb +9 -1
  4. data/app/controllers/{concerns/rails_admin → rails_admin}/jcrop_controller.rb +34 -1
  5. data/app/models/concerns/hancock/gallery/active_record_paperclip.rb +59 -17
  6. data/app/models/concerns/hancock/gallery/auto_crop.rb +6 -2
  7. data/app/models/concerns/hancock/gallery/decorators/gallery.rb +7 -0
  8. data/app/models/concerns/hancock/gallery/decorators/image.rb +7 -0
  9. data/app/models/concerns/hancock/gallery/mongoid_paperclip.rb +62 -18
  10. data/app/models/concerns/hancock/gallery/watermarkable.rb +113 -34
  11. data/app/views/rails_admin/jcrop/edit.html.slim +9 -0
  12. data/app/views/rails_admin/main/_form_hancock_image.html.slim +56 -0
  13. data/config/initializers/hancock_gallery.rb +0 -20
  14. data/hancock_cms_gallery.gemspec +2 -2
  15. data/lib/generators/hancock/gallery/config/{install_generator.rb → config_generator.rb} +0 -0
  16. data/lib/generators/hancock/gallery/config/templates/hancock_gallery.erb +6 -0
  17. data/lib/generators/hancock/gallery/migrations/{migration_generator.rb → migrations_generator.rb} +1 -1
  18. data/lib/generators/hancock/gallery/models/gallery_generator.rb +1 -1
  19. data/lib/generators/hancock/gallery/models/templates/embedded_image.erb +5 -5
  20. data/lib/generators/hancock/gallery/models/templates/gallery.erb +12 -5
  21. data/lib/generators/hancock/gallery/models/templates/image.erb +12 -5
  22. data/lib/generators/hancock/gallery/models/templates/original_image.erb +5 -5
  23. data/lib/hancock/gallery/admin.rb +26 -5
  24. data/lib/hancock/gallery/admin/gallery.rb +5 -0
  25. data/lib/hancock/gallery/admin/image.rb +4 -1
  26. data/lib/hancock/gallery/admin/original_image.rb +7 -0
  27. data/lib/hancock/gallery/configuration.rb +12 -0
  28. data/lib/hancock/gallery/models/gallery.rb +15 -3
  29. data/lib/hancock/gallery/models/image.rb +14 -3
  30. data/lib/hancock/gallery/models/mongoid/gallery.rb +3 -0
  31. data/lib/hancock/gallery/models/mongoid/image.rb +3 -0
  32. data/lib/hancock/gallery/models/mongoid/original_image.rb +7 -0
  33. data/lib/hancock/gallery/models/original_image.rb +55 -5
  34. data/lib/hancock/gallery/paperclip_patch.rb +34 -0
  35. data/lib/hancock/gallery/rails_admin_ext/hancock_image.rb +41 -1
  36. data/lib/hancock/gallery/version.rb +1 -1
  37. data/lib/hancock_cms_gallery.rb +7 -10
  38. metadata +13 -11
@@ -9,19 +9,19 @@ class <%= camelcased_class_name %> < Hancock::Gallery::OriginalImage
9
9
  super(config)
10
10
  end
11
11
 
12
- def admin_can_user_defined_actions
12
+ def self.admin_can_user_defined_actions
13
13
  [].freeze
14
14
  end
15
- def admin_cannot_user_defined_actions
15
+ def self.admin_cannot_user_defined_actions
16
16
  [].freeze
17
17
  end
18
- def manager_can_user_defined_actions
18
+ def self.manager_can_user_defined_actions
19
19
  [].freeze
20
20
  end
21
- def manager_cannot_user_defined_actions
21
+ def self.manager_cannot_user_defined_actions
22
22
  [].freeze
23
23
  end
24
- def rails_admin_user_defined_visible_actions
24
+ def self.rails_admin_user_defined_visible_actions
25
25
  [].freeze
26
26
  end
27
27
 
@@ -1,20 +1,41 @@
1
1
  module Hancock::Gallery
2
2
  module Admin
3
3
 
4
- def self.images_block(is_active = false, fields = [:images])
4
+ # images_block(is_active = false, fields = [:images], options = {})
5
+ # images_block(is_active = false, options = {field: [:images]})
6
+ # images_block(fields = [:images], options = {active: false})
7
+ # images_block(options = {fields: [:images], active: false})
8
+ # images_block(is_active = false, fields = [:images], options = {})
9
+
10
+ def self.images_block(is_active = false, fields = [:images], options = {})
5
11
  if is_active.is_a?(Array) or is_active.is_a?(String) or is_active.is_a?(Symbol)
6
12
  is_active, fields = false, [is_active].flatten
7
13
  elsif is_active.is_a?(Hash)
8
14
  is_active, fields = (is_active[:active] || false), (is_active[:fields] || [:images])
15
+ elsif fields.is?(Hash)
16
+ is_active, fields, options = (fields[:active] || false), (is_active[:fields] || [:images]), fields
17
+ end
18
+ if fields.is_a?(Array)
19
+ fields.map! { |f|
20
+ if f.is_a?(Hash)
21
+ f
22
+ elsif f.is_a?(Array)
23
+ _ret = {"#{f.first}" => f[1..-1]}
24
+ else
25
+ { "#{f}": nil }
26
+ end
27
+ }
28
+ fields = fields.reduce({}, :merge)
29
+
9
30
  end
10
31
 
32
+
11
33
  Proc.new {
12
34
  active is_active
13
- label I18n.t('hancock.images')
35
+ label options[:label] || I18n.t('hancock.images')
14
36
  field :image, :hancock_image
15
- fields.map { |f|
16
- field f
17
- }
37
+
38
+ Hancock::RailsAdminGroupPatch::hancock_cms_group(self, fields)
18
39
 
19
40
  if block_given?
20
41
  yield self
@@ -10,6 +10,7 @@ module Hancock::Gallery
10
10
 
11
11
  Proc.new {
12
12
  navigation_label(!nav_label.blank? ? nav_label : I18n.t('hancock.gallery'))
13
+
13
14
  field :enabled, :toggle do
14
15
  searchable false
15
16
  end
@@ -30,6 +31,10 @@ module Hancock::Gallery
30
31
  read_only true
31
32
  end
32
33
 
34
+ if Hancock::Gallery.config.cache_support
35
+ group :caching, &Hancock::Cache::Admin.caching_block
36
+ end
37
+
33
38
  nested_set({max_depth: 1, scopes: []})
34
39
 
35
40
  if defined?(RailsAdminMultipleFileUpload)
@@ -16,6 +16,7 @@ module Hancock::Gallery
16
16
 
17
17
  Proc.new {
18
18
  navigation_label(!nav_label.blank? ? nav_label : I18n.t('hancock.gallery'))
19
+
19
20
  field :enabled, :toggle do
20
21
  searchable false
21
22
  end
@@ -29,7 +30,9 @@ module Hancock::Gallery
29
30
  end
30
31
  field :image, :hancock_image
31
32
 
32
- group :caching, &Hancock::Admin.caching_block
33
+ if Hancock::Gallery.config.cache_support
34
+ group :caching, &Hancock::Cache::Admin.caching_block
35
+ end
33
36
 
34
37
  nested_set({max_depth: 1, scopes: []})
35
38
 
@@ -20,6 +20,13 @@ module Hancock::Gallery
20
20
 
21
21
  field :originable
22
22
  field :original
23
+ field :original_as_image do
24
+ pretty_value do
25
+ _value = value.sub(/\>\s*\z/, " style='max-width: 100%; max-height: 100%;'>")
26
+ "<div style='max-width: 300px; height: 100px;'>#{_value}</div>".html_safe
27
+ end
28
+ read_only true
29
+ end
23
30
 
24
31
  Hancock::RailsAdminGroupPatch::hancock_cms_group(self, fields)
25
32
 
@@ -9,18 +9,30 @@ module Hancock::Gallery
9
9
 
10
10
  attr_accessor :localize
11
11
 
12
+ attr_accessor :cache_support
13
+
12
14
  attr_accessor :model_settings_support
13
15
  attr_accessor :user_abilities_support
14
16
  attr_accessor :ra_comments_support
15
17
  attr_accessor :watermark_support
16
18
 
19
+ attr_accessor :original_image_hash_secret
20
+
21
+ attr_accessor :default_convert_options
22
+
17
23
  def initialize
18
24
  @localize = Hancock.config.localize
19
25
 
26
+ @cache_support = defined?(Hancock::Cache)
27
+
20
28
  @model_settings_support = defined?(RailsAdminModelSettings)
21
29
  @user_abilities_support = defined?(RailsAdminUserAbilities)
22
30
  @ra_comments_support = defined?(RailsAdminComments)
23
31
  @watermark_support = defined?(PaperclipWatermark)
32
+
33
+ @original_image_hash_secret = Rails.application.secrets.secret_key_base
34
+
35
+ @default_convert_options = {all: ["-quality", "75", "-strip"]}
24
36
  end
25
37
  end
26
38
  end
@@ -12,6 +12,10 @@ module Hancock::Gallery
12
12
  # include Hancock::Gallery::Watermarkable
13
13
  # end
14
14
 
15
+ if Hancock::Gallery.config.cache_support
16
+ include Hancock::Cache::Cacheable
17
+ end
18
+
15
19
  include Hancock::Gallery.orm_specific('Gallery')
16
20
 
17
21
  included do
@@ -37,20 +41,28 @@ module Hancock::Gallery
37
41
  belongs_to :gallerable, polymorphic: true, optional: true
38
42
  end
39
43
 
44
+ def self.rails_admin_name_synonyms
45
+ "".freeze
46
+ end
47
+ def self.rails_admin_navigation_icon
48
+ ''.freeze
49
+ end
40
50
 
41
51
  def self.manager_can_add_actions
42
- ret = [:nested_set]
52
+ ret = [:nested_set, :multiple_file_upload]
43
53
  # ret += [:multiple_file_upload, :sort_embedded] if Hancock::Gallery.mongoid?
44
54
  ret << :model_settings if Hancock::Gallery.config.model_settings_support
45
- ret << :model_accesses if Hancock::Gallery.config.user_abilities_support
55
+ # ret << :model_accesses if Hancock::Gallery.config.user_abilities_support
56
+ ret << :hancock_touch if Hancock::Gallery.config.cache_support
46
57
  ret += [:comments, :model_comments] if Hancock::Gallery.config.ra_comments_support
47
58
  ret.freeze
48
59
  end
49
60
  def self.rails_admin_add_visible_actions
50
- ret = [:nested_set]
61
+ ret = [:nested_set, :multiple_file_upload]
51
62
  # ret += [:multiple_file_upload, :sort_embedded] if Hancock::Gallery.mongoid?
52
63
  ret << :model_settings if Hancock::Gallery.config.model_settings_support
53
64
  ret << :model_accesses if Hancock::Gallery.config.user_abilities_support
65
+ ret << :hancock_touch if Hancock::Gallery.config.cache_support
54
66
  ret += [:comments, :model_comments] if Hancock::Gallery.config.ra_comments_support
55
67
  ret.freeze
56
68
  end
@@ -11,7 +11,9 @@ module Hancock::Gallery
11
11
  # include Hancock::Gallery::Watermarkable
12
12
  # end
13
13
 
14
- include Hancock::Cacheable
14
+ if Hancock::Gallery.config.cache_support
15
+ include Hancock::Cache::Cacheable
16
+ end
15
17
 
16
18
  include Hancock::Gallery.orm_specific('Image')
17
19
 
@@ -33,11 +35,19 @@ module Hancock::Gallery
33
35
  # hancock_cms_attached_file(:image)
34
36
  # end
35
37
 
38
+ def self.rails_admin_name_synonyms
39
+ "Photo Картинки Картинка Изображение Изображения".freeze
40
+ end
41
+ def self.rails_admin_navigation_icon
42
+ 'icon-picture'.freeze
43
+ end
44
+
36
45
  def self.manager_can_add_actions
37
- ret = [:nested_set, :multiple_file_upload_collection]
46
+ ret = [:nested_set]
38
47
  # ret += [:multiple_file_upload, :sort_embedded] if Hancock::Gallery.mongoid?
39
48
  ret << :model_settings if Hancock::Gallery.config.model_settings_support
40
- ret << :model_accesses if Hancock::Gallery.config.user_abilities_support
49
+ # ret << :model_accesses if Hancock::Gallery.config.user_abilities_support
50
+ ret << :hancock_touch if Hancock::Gallery.config.cache_support
41
51
  ret += [:comments, :model_comments] if Hancock::Gallery.config.ra_comments_support
42
52
  ret.freeze
43
53
  end
@@ -46,6 +56,7 @@ module Hancock::Gallery
46
56
  # ret += [:multiple_file_upload, :sort_embedded] if Hancock::Gallery.mongoid?
47
57
  ret << :model_settings if Hancock::Gallery.config.model_settings_support
48
58
  ret << :model_accesses if Hancock::Gallery.config.user_abilities_support
59
+ ret << :hancock_touch if Hancock::Gallery.config.cache_support
49
60
  ret += [:comments, :model_comments] if Hancock::Gallery.config.ra_comments_support
50
61
  ret.freeze
51
62
  end
@@ -5,6 +5,9 @@ module Hancock::Gallery
5
5
  extend ActiveSupport::Concern
6
6
 
7
7
  included do
8
+ index({gallerable_id: 1, gallerable_type: 1}, {background: true})
9
+ index({enabled: 1, lft: 1}, {background: true})
10
+
8
11
  field :name, type: String, localize: Hancock::Gallery.configuration.localize
9
12
 
10
13
  scope :sorted, -> { order_by([:lft, :asc]) }
@@ -5,6 +5,9 @@ module Hancock::Gallery
5
5
  extend ActiveSupport::Concern
6
6
 
7
7
  included do
8
+ index({gallery_id: 1}, {background: true})
9
+ index({enabled: 1, lft: 1}, {background: true})
10
+
8
11
  field :name, type: String, localize: Hancock::Gallery.configuration.localize
9
12
 
10
13
  scope :sorted, -> { order_by([:lft, :asc]) }
@@ -5,7 +5,14 @@ module Hancock::Gallery
5
5
  extend ActiveSupport::Concern
6
6
 
7
7
  included do
8
+ index({originable_id: 1, originable_type: 1}, {background: true})
9
+
8
10
  field :original, type: BSON::Binary
11
+
12
+ hancock_cms_attached_file(:image, url: "/system/:class/:attachment/:id_partition/:style/:hash.:extension",
13
+ hash_data: ":class/:attachment/:id/:style/:created_at",
14
+ hash_secret: Hancock::Gallery.config.original_image_hash_secret
15
+ )
9
16
  end
10
17
 
11
18
  end
@@ -4,18 +4,68 @@ module Hancock::Gallery
4
4
  extend ActiveSupport::Concern
5
5
 
6
6
  include Hancock::Model
7
+ include Hancock::Gallery::Paperclipable
7
8
  include Hancock::Gallery.orm_specific('OriginalImage')
8
9
 
9
10
  included do
11
+ if Hancock.rails4?
12
+ belongs_to :originable, polymorphic: true
13
+ else
14
+ belongs_to :originable, polymorphic: true, optional: true
15
+ end
16
+
17
+ def from_db_to_fs
18
+ unless self.original.blank?
19
+ _paperclip_obj = Paperclip.io_adapters.for(self.original_as_base64)
20
+ mime_type = MIME::Types[_paperclip_obj.content_type].first
21
+ _paperclip_obj.original_filename = "#{_paperclip_obj.original_filename}.#{mime_type.extensions.first}" if mime_type
22
+ self.image = _paperclip_obj
23
+ self.original = nil
24
+ return self
25
+ end
26
+ return false
27
+ end
28
+ def from_db_to_fs!
29
+ self.from_db_to_fs and self.save
30
+ end
10
31
 
11
- belongs_to :originable, polymorphic: true
32
+ def relocate_original
33
+ unless File.exists? self.image.path
34
+ _path = self.image.path
35
+ _old_file = Dir[File.dirname(self.image.path) + "/*#{File.extname(self.image.path)}"].first
36
+ if _old_file
37
+ return File.rename _old_file, _path
38
+ end
39
+ return nil
40
+ end
41
+ return false
42
+ end
43
+ def relocate_original!
44
+ self.relocate_original and self.image.reprocess!
45
+ end
12
46
 
13
- def self.admin_can_default_actions
14
- [:show, :read].freeze
47
+
48
+ def original_as_base64(content_type = nil)
49
+ _original = self.original
50
+ if _original
51
+ _data = Base64.encode64(_original.data)
52
+ _content_type = content_type
53
+ else
54
+ _data = ''
55
+ _content_type = content_type || 'jpg'
56
+ end
57
+ "data:#{_content_type};base64,#{_data}"
15
58
  end
16
- def self.manager_can_default_actions
17
- [:show, :read].freeze
59
+ def original_as_image(content_type = nil)
60
+ "<img src='#{original_as_base64(content_type)}'>".html_safe
18
61
  end
62
+
63
+ # def self.admin_can_default_actions
64
+ # [:show, :read].freeze
65
+ # end
66
+ # def self.manager_can_default_actions
67
+ # [:show, :read].freeze
68
+ # end
19
69
  def self.admin_cannot_actions
20
70
  [:new, :create, :edit, :update].freeze
21
71
  end
@@ -0,0 +1,34 @@
1
+ if defined?(Paperclip)
2
+
3
+ module Paperclip
4
+ class Attachment
5
+
6
+ def inline_data(style=:original)
7
+ if queued_for_write[style]
8
+ queued_for_write[style].read.force_encoding("UTF-8").html_safe rescue ""
9
+ elsif !path(style).blank? and File.exists?(path(style))
10
+ File.read(path(style)).force_encoding("UTF-8").html_safe rescue ""
11
+ end
12
+ end
13
+
14
+ def base64(style=:original)
15
+ _data = inline_data(style)
16
+ Base64.encode64(_data) if _data
17
+ end
18
+ def base64_as_src(style=:original)
19
+ _base64 = base64(style)
20
+ "data:#{content_type};base64,#{_base64}" unless _base64.blank?
21
+ end
22
+
23
+ def inline_svg
24
+ inline_data if svg?
25
+ end
26
+
27
+ def svg?
28
+ !!(content_type =~ /svg/)
29
+ end
30
+
31
+ end
32
+ end
33
+
34
+ end
@@ -8,14 +8,54 @@ module RailsAdmin
8
8
  # Register field type for the type loader
9
9
  RailsAdmin::Config::Fields::Types::register(self)
10
10
 
11
+ register_instance_option(:partial) do
12
+ :form_hancock_image
13
+ end
14
+
15
+ register_instance_option :process_watermark_toggler_method do
16
+ "process_watermark_#{name}"
17
+ end
18
+ register_instance_option :cancel_perform_autocrop_method do
19
+ "#{name}_autocropped" if bindings[:object].respond_to?("#{name}_autocropped")
20
+ end
21
+
22
+ register_instance_option :process_watermark_default do
23
+ true
24
+ end
25
+
26
+ register_instance_option :process_watermark_toggler do
27
+ # bindings[:object].send(name).processors.include?(:watermark) and bindings[:object].respond_to?(process_watermark_toggler_method)
28
+ bindings[:object].respond_to?(process_watermark_toggler_method)
29
+ end
30
+
31
+
32
+ register_instance_option :allowed_methods do
33
+ if process_watermark_toggler
34
+ [method_name, delete_method, cache_method, cancel_perform_autocrop_method, process_watermark_toggler_method].compact
35
+ else
36
+ [method_name, delete_method, cache_method, cancel_perform_autocrop_method].compact
37
+ end
38
+ end
39
+
11
40
  register_instance_option :help do
12
- "SVG не изменяется."
41
+ "SVG не изменяется. #{(required? ? I18n.translate('admin.form.required') : I18n.translate('admin.form.optional')) + '. '}"
13
42
  end
14
43
 
15
44
  register_instance_option :jcrop_options do
16
45
  "#{name}_jcrop_options".to_sym
17
46
  end
18
47
 
48
+ register_instance_option :svg? do
49
+ (url = resource_url.to_s) && url.split('.').last =~ /svg/i
50
+ end
51
+ register_instance_option :thumb_method do
52
+ if svg?
53
+ :original
54
+ else
55
+ @thumb_method ||= ((styles = bindings[:object].send(name).styles.keys).find{|k| k.in?([:thumb, :thumbnail, 'thumb', 'thumbnail'])} || styles.first.to_s)
56
+ end
57
+ end
58
+
19
59
  end
20
60
  end
21
61
  end