rails_admin_jcrop 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -16,13 +16,12 @@
16
16
  # Below is optional
17
17
  edit do
18
18
  field :avatar do
19
- # use jcrop_options to pass any options you like to .Jcrop call in javascript
20
19
  jcrop_options aspectRatio: 500.0/320.0
21
20
  end
22
21
  end
23
22
  end
24
23
 
25
- 3. Call :rails_admin_crop in your uploader:
24
+ 3. If you use Paperclip, you need to do nothing here, rails_admin_jcrop will append RailsAdminJcropper processor to your attachment automatically. If CarrierWave is used, please invoke :rails_admin_crop in your uploader:
26
25
 
27
26
  class AvatarUploader < CarrierWave::Uploader::Base
28
27
 
@@ -34,7 +33,37 @@
34
33
  end
35
34
 
36
35
  4. Done! Click the image on your RailsAdmin model edit page and enjoy cropping!
37
- ![Cropping Screenshot](https://github.com/janx/rails_admin_jcrop/raw/master/screenshot.png)
36
+ ![Cropping Screenshot](https://github.com/janx/rails_admin_jcrop/raw/master/screenshots/example.png)
37
+
38
+ ## Field Options ##
39
+
40
+ ### jcrop_options ###
41
+
42
+ You can pass any Jcrop plugin allowed options here, for example, use `aspectRatio` to fix the ratio of selection box:
43
+
44
+ field :avatar do
45
+ jcrop_options aspectRatio: 500.0/320.0
46
+ end
47
+
48
+ Please check [Jcrop document](http://deepliquid.com/content/Jcrop_Manual.html#Setting_Options) for more available options.
49
+
50
+ ### fit_image ###
51
+
52
+ By default, image is scaled properly to make cropping more easy, but sometimes the image is still too large to fit in the modal window, you may need to scroll image up/down to crop. If you set `fit_image` to true, image will always be resized to fit in modal window.
53
+
54
+ field :avatar do
55
+ fit_image true
56
+ end
57
+
58
+ Check screenshots below to see the difference:
59
+
60
+ When `fit_image` is false (default)
61
+
62
+ ![fit_image false](https://github.com/janx/rails_admin_jcrop/raw/master/screenshots/fit_image_false.png)
63
+
64
+ When `fit_image` is true
65
+
66
+ ![fit_image true](https://github.com/janx/rails_admin_jcrop/raw/master/screenshots/fit_image_true.png)
38
67
 
39
68
  ## Localization ##
40
69
 
@@ -51,31 +80,40 @@ Localize the crop form by adding these entries:
51
80
 
52
81
  * MRI 1.9.3 (All above 1.8.6 should work, I only tested on 1.9.3)
53
82
  * Rails 3.x
54
- * ORM
55
- - ActiveRecord
56
- * Upload plugin
57
- - CarrierWave
58
- * Image processor
59
- - MiniMagick
83
+ * MiniMagick
84
+
85
+ ## Supported ORM ##
86
+
87
+ * ActiveRecord
88
+ * Mongoid
89
+
90
+ ## Supported Asset Plugin ##
91
+
92
+ * CarrierWave
93
+ * Paperclip
60
94
 
61
95
  ## TODO ##
62
96
 
63
- * MongoDB support
64
- * Paperclip support
65
- * RMagick support
97
+ * automate :rails_admin_crop for CarrierWave uploader
98
+
66
99
 
67
100
  ## Contributing ##
68
101
 
69
102
  Any help is encouraged. Here are some ways you can contribute:
70
103
 
71
104
  * by using it
72
- * by telling people this plugin
105
+ * by telling people
73
106
  * by reporting bugs or suggesting new features on github issue tracker
74
107
  * by fixing bugs or implementing features
75
- * by giving author a hug (especially if you're girl)
76
108
 
77
109
  ## Thanks ##
78
110
 
111
+ ### Contributors ###
112
+
113
+ * [Alan Rosin Sikora](https://github.com/alansikora) ([alansikora](https://github.com/alansikora))
114
+
115
+ ### And ... ###
116
+
79
117
  Life is easier with you.
80
118
 
81
119
  * [RailsAdmin](https://github.com/sferik/rails_admin/)
@@ -1,12 +1,33 @@
1
+ require 'mini_magick'
2
+
1
3
  module RailsAdmin
2
4
 
3
5
  class JcropController < RailsAdmin::ApplicationController
4
6
  skip_before_filter :get_model
5
- before_filter :get_model, :get_object, :get_field
7
+ before_filter :get_model, :get_object, :get_field, :get_fit_image
6
8
 
7
9
  helper_method :abstract_model, :geometry
8
10
 
9
11
  def edit
12
+ @form_options = {}
13
+ @form_options[:method] = :put
14
+ @form_options[:'data-title'] = "#{I18n.t("admin.actions.crop.menu").capitalize} #{abstract_model.model.human_attribute_name @field}"
15
+
16
+ @image_tag_options = {}
17
+ @image_tag_options[:class] = "jcrop-subject"
18
+ @image_tag_options[:'data-geometry'] = geometry(@object.send(@field).path).join(",")
19
+
20
+ if @fit_image
21
+ fit_image_geometry = fit_image_geometry(@object.send(@field).path)
22
+
23
+ @form_options[:'style'] = "margin-left: #{375 - (fit_image_geometry[0]/2) - 15}px;"
24
+
25
+ @image_tag_options[:style] = ""
26
+ @image_tag_options[:style] << "width: #{fit_image_geometry[0]}px !important;"
27
+ @image_tag_options[:style] << "height: #{fit_image_geometry[1]}px !important;"
28
+ @image_tag_options[:style] << "border: 1px solid #AAA !important;"
29
+ end
30
+
10
31
  respond_to do |format|
11
32
  format.html
12
33
  format.js { render :edit, :layout => false }
@@ -21,7 +42,7 @@ module RailsAdmin
21
42
  format.js do
22
43
  asset = @object.send @field
23
44
  urls = {:original => asset.url}
24
- @object.class.uploaders[@field.to_sym].versions.each {|name, _| urls[name] = asset.url(name)}
45
+ thumbnail_names.each {|name| urls[name] = asset.url(name)}
25
46
 
26
47
  render :json => {
27
48
  :id => @object.id,
@@ -35,6 +56,14 @@ module RailsAdmin
35
56
 
36
57
  private
37
58
 
59
+ def thumbnail_names
60
+ RailsAdminJcrop::AssetEngine.thumbnail_names(@object, @field)
61
+ end
62
+
63
+ def get_fit_image
64
+ @fit_image = params[:fit_image] == "true" ? true : false
65
+ end
66
+
38
67
  def get_field
39
68
  @field = params[:field]
40
69
  end
@@ -44,6 +73,13 @@ module RailsAdmin
44
73
  [image[:width], image[:height]]
45
74
  end
46
75
 
76
+ def fit_image_geometry(image_path)
77
+ image = MiniMagick::Image.open(image_path)
78
+ # Magic number origin: https://github.com/janx/rails_admin_jcrop/pull/2
79
+ image.resize "720x400"
80
+ [image[:width], image[:height]]
81
+ end
82
+
47
83
  def cropping?
48
84
  [:crop_x, :crop_y, :crop_w, :crop_h].all? {|c| params[c].present?}
49
85
  end
@@ -1,5 +1,5 @@
1
- = form_tag nil, :method => :put, 'data-title' => "#{I18n.t('admin.actions.crop.menu').capitalize} #{abstract_model.model.human_attribute_name @field}" do
2
- = image_tag @object.send(@field).url, :class => 'jcrop-subject', 'data-geometry' => geometry(@object.send(@field).path).join(',')
1
+ = form_tag nil, @form_options do
2
+ = image_tag @object.send(@field).url, @image_tag_options
3
3
  = hidden_field_tag :crop_x
4
4
  = hidden_field_tag :crop_y
5
5
  = hidden_field_tag :crop_w
@@ -11,7 +11,7 @@
11
11
  - if value = field.pretty_value
12
12
  = value
13
13
  = form.file_field(field.name, field.html_attributes.reverse_merge({ :data => { :fileupload => true } }))
14
- = link_to 'Edit Image', '#', :data => {:link => jcrop_path(:model_name => form.object.class.to_param, :modal => true, :id => form.object.id, :field => field.name), :thumb => field.thumb_method}, :style => 'display:none', :class => "jcrop_handle"
14
+ = link_to 'Edit Image', '#', :data => {:link => jcrop_path(:model_name => form.object.class.to_param, :modal => true, :id => form.object.id, :field => field.name, :fit_image => field.fit_image), :thumb => field.thumb_method}, :style => 'display:none', :class => "jcrop_handle"
15
15
  - if field.optional? && field.errors.blank? && file && field.delete_method
16
16
  %a.btn.btn-info{:href => '#', :'data-toggle' => 'button', :onclick => "$(this).siblings('[type=checkbox]').click(); $(this).siblings('.toggle').toggle('slow'); jQuery(this).toggleClass('btn-danger btn-info'); return false"}
17
17
  %i.icon-white.icon-trash
@@ -1,10 +1,4 @@
1
1
  require "rails_admin_jcrop/engine"
2
-
3
- module RailsAdminJcrop
4
- module Orm
5
- autoload :ActiveRecord, 'rails_admin_jcrop/orm/active_record'
6
- end
7
- end
8
-
9
- require 'rails_admin_jcrop/extensions/carrierwave'
10
- require 'rails_admin_jcrop/extensions/rails_admin'
2
+ require 'rails_admin_jcrop/orm'
3
+ require 'rails_admin_jcrop/asset_engine'
4
+ require 'rails_admin_jcrop/rails_admin'
@@ -0,0 +1,2 @@
1
+ require 'rails_admin_jcrop/asset_engine/carrier_wave' if defined?(::CarrierWave)
2
+ require 'rails_admin_jcrop/asset_engine/paperclip' if defined?(::Paperclip)
@@ -0,0 +1,67 @@
1
+ module RailsAdminJcrop
2
+ module AssetEngine
3
+ class <<self
4
+ def thumbnail_names(obj, field)
5
+ obj.class.uploaders[field.to_sym].versions.keys
6
+ end
7
+
8
+ def crop!(obj, field)
9
+ obj.send(field).recreate_versions!
10
+ end
11
+ end
12
+
13
+ module CarrierWave
14
+ def rails_admin_crop
15
+ return unless model.rails_admin_cropping?
16
+ manipulate! do |img|
17
+ geometry = "#{model.crop_w}x#{model.crop_h}+#{model.crop_x}+#{model.crop_y}"
18
+ img.crop geometry
19
+ img
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+
26
+ module CarrierWave
27
+ module Mount
28
+ module Extension
29
+ def self.included(base)
30
+ base.uploaders.each do |name, klass|
31
+ klass.send :include, RailsAdminJcrop::AssetEngine::CarrierWave
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+
38
+ module RailsAdmin
39
+ module Config
40
+ module Fields
41
+ module Types
42
+ module UploaderMethods
43
+
44
+ def self.included(base)
45
+ base.register_instance_option(:cache_method) do
46
+ "#{name}_cache"
47
+ end
48
+
49
+ base.register_instance_option(:thumb_method) do
50
+ @thumb_method ||= ((versions = bindings[:object].send(name).versions.keys).find{|k| k.in?([:thumb, :thumbnail, 'thumb', 'thumbnail'])} || versions.first.to_s)
51
+ end
52
+
53
+ base.register_instance_option(:delete_method) do
54
+ "remove_#{name}"
55
+ end
56
+ end
57
+
58
+ def resource_url(thumb = false)
59
+ return nil unless (uploader = bindings[:object].send(name)).present?
60
+ thumb.present? ? uploader.send(thumb).url : uploader.url
61
+ end
62
+
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,86 @@
1
+ module RailsAdminJcrop
2
+ module AssetEngine
3
+ class <<self
4
+ def thumbnail_names(obj, field)
5
+ obj.send(field).styles.keys
6
+ end
7
+
8
+ def crop!(obj, field)
9
+ obj.send(field).reprocess!
10
+ end
11
+ end
12
+ end
13
+ end
14
+
15
+ module Paperclip
16
+
17
+ module NewClassMethods
18
+ def has_attached_file(*args)
19
+ super
20
+
21
+ self.attachment_definitions.each do |name, options|
22
+ options[:processors] ||= []
23
+ options[:processors] << :rails_admin_jcropper
24
+ end
25
+ end
26
+ end
27
+
28
+ module ClassMethods
29
+ def self.extended(base)
30
+ super
31
+ base.send :extend, ::Paperclip::NewClassMethods
32
+ end
33
+ end
34
+
35
+ class RailsAdminJcropper < Thumbnail
36
+ def transformation_command
37
+ if @attachment.instance.rails_admin_cropping?
38
+ ary = super
39
+ if i = ary.index('-crop')
40
+ ary.delete_at i
41
+ ary.delete_at i+1
42
+ end
43
+ ['-crop', crop_params] + ary
44
+ else
45
+ super
46
+ end
47
+ end
48
+
49
+ def crop_params
50
+ target = @attachment.instance
51
+ "'#{target.crop_w.to_i}x#{target.crop_h.to_i}+#{target.crop_x.to_i}+#{target.crop_y.to_i}'"
52
+ end
53
+ end
54
+
55
+ end
56
+
57
+ module RailsAdmin
58
+ module Config
59
+ module Fields
60
+ module Types
61
+ module UploaderMethods
62
+
63
+ def self.included(base)
64
+ base.register_instance_option(:cache_method) do
65
+ nil
66
+ end
67
+
68
+ base.register_instance_option(:thumb_method) do
69
+ @thumb_method ||= ((styles = bindings[:object].send(name).styles.keys).find{|k| k.in?([:thumb, :thumbnail, 'thumb', 'thumbnail'])} || styles.first.to_s)
70
+ end
71
+
72
+ base.register_instance_option(:delete_method) do
73
+ nil
74
+ end
75
+ end
76
+
77
+ def resource_url(thumb = false)
78
+ return nil unless (attachment = bindings[:object].send(name)).present?
79
+ thumb.present? ? attachment.url(thumb) : attachment.url
80
+ end
81
+
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,41 @@
1
+ module RailsAdminJcrop
2
+ module Orm
3
+ module Extension
4
+
5
+ CropFields = [:crop_x, :crop_y, :crop_w, :crop_h, :crop_field]
6
+
7
+ def self.included(base)
8
+ base.send :attr_accessor, *CropFields
9
+ base.after_update :rails_admin_crop_callback, :if => :rails_admin_cropping?
10
+ end
11
+
12
+ def rails_admin_crop_callback
13
+ ::RailsAdminJcrop::AssetEngine.crop!(self, self.crop_field)
14
+ end
15
+
16
+ def rails_admin_cropping?
17
+ CropFields.all? {|f| send(f).present?}
18
+ end
19
+
20
+ def rails_admin_crop!(params)
21
+ CropFields.each {|f| self.send "#{f}=", params[f] }
22
+ save!
23
+ end
24
+
25
+ end
26
+ end
27
+ end
28
+
29
+ if defined?(::ActiveRecord)
30
+ ::ActiveRecord::Base.send(:include, ::RailsAdminJcrop::Orm::Extension)
31
+ end
32
+
33
+ if defined?(::Mongoid)
34
+ module Mongoid
35
+ module Document
36
+ def self.included(base)
37
+ base.send(:include, ::RailsAdminJcrop::Orm::Extension)
38
+ end
39
+ end
40
+ end
41
+ end
@@ -1,42 +0,0 @@
1
- module RailsAdminJcrop
2
- module Orm
3
- module ActiveRecord
4
-
5
- CropFields = [:crop_x, :crop_y, :crop_w, :crop_h, :crop_field]
6
-
7
- def self.included(base)
8
- base.send :attr_accessor, *CropFields
9
- base.after_update :rails_admin_crop_callback, :if => :rails_admin_cropping?
10
-
11
- base.uploaders.each do |name, klass|
12
- klass.send :include, CarrierWaveUploaderMixin
13
- end
14
- end
15
-
16
- def rails_admin_crop_callback
17
- self.send(self.crop_field).recreate_versions!
18
- end
19
-
20
- def rails_admin_cropping?
21
- CropFields.all? {|f| send(f).present?}
22
- end
23
-
24
- def rails_admin_crop!(params)
25
- CropFields.each {|f| self.send "#{f}=", params[f] }
26
- save!
27
- end
28
-
29
- module CarrierWaveUploaderMixin
30
- def rails_admin_crop
31
- return unless model.rails_admin_cropping?
32
- manipulate! do |img|
33
- geometry = "#{model.crop_w}x#{model.crop_h}+#{model.crop_x}+#{model.crop_y}"
34
- img.crop geometry
35
- img
36
- end
37
- end
38
- end
39
-
40
- end
41
- end
42
- end
File without changes
@@ -14,26 +14,15 @@ module RailsAdmin
14
14
  :form_jcrop
15
15
  end
16
16
 
17
- register_instance_option(:thumb_method) do
18
- @thumb_method ||= ((versions = bindings[:object].send(name).versions.keys).find{|k| k.in?([:thumb, :thumbnail, 'thumb', 'thumbnail'])} || versions.first.to_s)
19
- end
20
-
21
- register_instance_option(:delete_method) do
22
- "remove_#{name}"
23
- end
24
-
25
- register_instance_option(:cache_method) do
26
- "#{name}_cache"
27
- end
28
-
29
17
  register_instance_option(:jcrop_options) do
30
18
  {}
31
19
  end
32
20
 
33
- def resource_url(thumb = false)
34
- return nil unless (uploader = bindings[:object].send(name)).present?
35
- thumb.present? ? uploader.send(thumb).url : uploader.url
21
+ register_instance_option(:fit_image) do
22
+ @fit_image ||= false
36
23
  end
24
+
25
+ include ::RailsAdmin::Config::Fields::Types::UploaderMethods
37
26
  end
38
27
  end
39
28
  end
@@ -1,3 +1,3 @@
1
1
  module RailsAdminJcrop
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_admin_jcrop
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-06-30 00:00:00.000000000 Z
12
+ date: 2012-08-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -44,14 +44,14 @@ dependencies:
44
44
  - !ruby/object:Gem::Version
45
45
  version: '0'
46
46
  - !ruby/object:Gem::Dependency
47
- name: sqlite3
47
+ name: mini_magick
48
48
  requirement: !ruby/object:Gem::Requirement
49
49
  none: false
50
50
  requirements:
51
51
  - - ! '>='
52
52
  - !ruby/object:Gem::Version
53
53
  version: '0'
54
- type: :development
54
+ type: :runtime
55
55
  prerelease: false
56
56
  version_requirements: !ruby/object:Gem::Requirement
57
57
  none: false
@@ -73,10 +73,14 @@ files:
73
73
  - config/locales/en.yml
74
74
  - config/routes.rb
75
75
  - lib/rails_admin_jcrop/version.rb
76
+ - lib/rails_admin_jcrop/asset_engine.rb
77
+ - lib/rails_admin_jcrop/asset_engine/paperclip.rb
78
+ - lib/rails_admin_jcrop/asset_engine/carrier_wave.rb
76
79
  - lib/rails_admin_jcrop/engine.rb
80
+ - lib/rails_admin_jcrop/rails_admin.rb
81
+ - lib/rails_admin_jcrop/orm.rb
77
82
  - lib/rails_admin_jcrop/orm/active_record.rb
78
- - lib/rails_admin_jcrop/extensions/carrierwave.rb
79
- - lib/rails_admin_jcrop/extensions/rails_admin.rb
83
+ - lib/rails_admin_jcrop/orm/mongoid.rb
80
84
  - lib/rails_admin_jcrop.rb
81
85
  - vendor/assets/images/rails_admin/Jcrop.gif
82
86
  - vendor/assets/javascripts/rails_admin/jquery.Jcrop.js
@@ -1,9 +0,0 @@
1
- module CarrierWave
2
- module Mount
3
- module Extension
4
- def self.included(base)
5
- base.send :include, RailsAdminJcrop::Orm::ActiveRecord
6
- end
7
- end
8
- end
9
- end