ack_rails_admin_jcrop 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (33) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.markdown +149 -0
  4. data/Rakefile +27 -0
  5. data/app/assets/javascripts/rails_admin/ra.jcrop.js +149 -0
  6. data/app/assets/stylesheets/rails_admin/ra.jcrop.sass +6 -0
  7. data/app/controllers/rails_admin/jcrop_controller.rb +111 -0
  8. data/app/views/rails_admin/jcrop/edit.html.slim +14 -0
  9. data/app/views/rails_admin/main/_form_jcrop.html.slim +27 -0
  10. data/config/locales/en.yml +7 -0
  11. data/config/locales/ru.yml +7 -0
  12. data/config/routes.rb +10 -0
  13. data/lib/ack_rails_admin_jcrop.rb +1 -0
  14. data/lib/rails_admin_jcrop.rb +5 -0
  15. data/lib/rails_admin_jcrop/asset_engine.rb +5 -0
  16. data/lib/rails_admin_jcrop/asset_engine/carrier_wave.rb +66 -0
  17. data/lib/rails_admin_jcrop/asset_engine/paperclip.rb +89 -0
  18. data/lib/rails_admin_jcrop/engine.rb +9 -0
  19. data/lib/rails_admin_jcrop/image_helper.rb +21 -0
  20. data/lib/rails_admin_jcrop/orm.rb +36 -0
  21. data/lib/rails_admin_jcrop/orm/active_record.rb +0 -0
  22. data/lib/rails_admin_jcrop/orm/mongoid.rb +0 -0
  23. data/lib/rails_admin_jcrop/rails_admin.rb +39 -0
  24. data/lib/rails_admin_jcrop/version.rb +3 -0
  25. data/vendor/assets/images/rails_admin/Jcrop.gif +0 -0
  26. data/vendor/assets/javascripts/rails_admin/jquery.Jcrop.js +1699 -0
  27. data/vendor/assets/javascripts/rails_admin/jquery.Jcrop.min.js +22 -0
  28. data/vendor/assets/javascripts/rails_admin/jquery.color.js +661 -0
  29. data/vendor/assets/javascripts/rails_admin/jquery.min.js +4 -0
  30. data/vendor/assets/stylesheets/rails_admin/Jcrop.gif +0 -0
  31. data/vendor/assets/stylesheets/rails_admin/jquery.Jcrop.css +165 -0
  32. data/vendor/assets/stylesheets/rails_admin/jquery.Jcrop.min.css +29 -0
  33. metadata +119 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1c45c721d62676e69f072cb3ed919257336a9ee0
4
+ data.tar.gz: 983bc219258fb286d126992fbcd836567c9f4ea7
5
+ SHA512:
6
+ metadata.gz: f9968c92d86cc7c4b5b68bd93e8be7ae21665c97b19df14947e3dcb7f1094fc3ebd50b330596ce307b87ee5fac0f537f11aaba63273ec9cf160643c3bed950d9
7
+ data.tar.gz: 372e2985f1122b124d586bd1bf80eeaa6afe2f8278e2fdf3cc5ef7cc4a282fa422bda4d4c079e8259f37a9f68ab36eb4521ef181c4a24ff1867dcf1f635b65ce
@@ -0,0 +1,20 @@
1
+ Copyright 2015 ack43
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,149 @@
1
+ # RailsAdmin Jcrop Plugin
2
+
3
+ ## Image cropping made easy! ##
4
+
5
+ 1. Add it to your Gemfile and run `bundle install`:
6
+
7
+ ```ruby
8
+ gem 'rails_admin'
9
+ # Because rails_admin_jcrop autoload modules by checking plugins you use, it's
10
+ # recommended to require it explictly before rails_admin_jcrop
11
+ # e.g. if you use carrierwave
12
+ # gem 'carrierwave', :require => 'carrierwave'
13
+ gem 'ack_rails_admin_jcrop' #, git: 'git://github.com/ack43/rails_admin_jcrop.git'
14
+ ```
15
+
16
+ 2. Configure your model field to use Jcrop:
17
+
18
+ ```ruby
19
+ # RAILS_ROOT/config/initializers/rails_admin.rb
20
+ config.model User do
21
+ configure :avatar, :jcrop
22
+
23
+ # Below is optional
24
+ edit do
25
+ field :avatar do
26
+ jcrop_options aspectRatio: 500.0/320.0
27
+ end
28
+ end
29
+ end
30
+ ```
31
+
32
+ 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:
33
+
34
+ ```ruby
35
+ class AvatarUploader < CarrierWave::Uploader::Base
36
+
37
+ version :thumb do
38
+ process :rails_admin_crop
39
+ process resize_to_fill: [500,320]
40
+ end
41
+
42
+ end
43
+ ```
44
+
45
+ 4. Done! Click the image on your RailsAdmin model edit page and enjoy cropping!
46
+ ![Cropping Screenshot](https://github.com/ack43/rails_admin_jcrop/raw/master/screenshots/example.png)
47
+
48
+ ### Tips ###
49
+
50
+ * Crop is done when you successfully return from the modal box, you don't need to save the whole record.
51
+ * If you don't see the cropped image on rails admin edit page, the browser is possibly displaying cached version. Try refresh your browser.
52
+ * `rails_admin_jcrop >= 1.0.0` works with `rails_admin ~> 0.3.x`. For older rails_admin, use rails_admin_jcrop 0.2.1.
53
+
54
+ ## Field Options ##
55
+
56
+ ### jcrop_options ###
57
+
58
+ You can pass any Jcrop plugin allowed options here, for example, use `aspectRatio` to fix the ratio of selection box:
59
+
60
+ ```ruby
61
+ field :avatar do
62
+ jcrop_options aspectRatio: 500.0/320.0
63
+ end
64
+ ```
65
+
66
+ Please check [Jcrop document](http://deepliquid.com/content/Jcrop_Manual.html#Setting_Options) for more available options.
67
+
68
+ ### fit_image ###
69
+
70
+ 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.
71
+
72
+ ```ruby
73
+ field :avatar do
74
+ fit_image true
75
+ end
76
+ ```
77
+
78
+ Check screenshots below to see the difference:
79
+
80
+ When `fit_image` is false (default)
81
+
82
+ ![fit_image false](https://github.com/janx/rails_admin_jcrop/raw/master/screenshots/fit_image_false.png)
83
+
84
+ When `fit_image` is true
85
+
86
+ ![fit_image true](https://github.com/janx/rails_admin_jcrop/raw/master/screenshots/fit_image_true.png)
87
+
88
+ ###### P.S. ######
89
+ this text is copied from [source gem](https://github.com/janx/rails_admin_jcrop). Doesn't work for me. it's fixed from trueSize option and css.
90
+
91
+ ## Localization ##
92
+
93
+ Localize the crop form by adding these entries:
94
+
95
+ zh:
96
+ admin:
97
+ actions:
98
+ crop:
99
+ title: '剪裁'
100
+ menu: '剪裁'
101
+
102
+ ## Dependencies ##
103
+
104
+ * MRI 1.9.3 (All above 1.8.6 should work, I only tested on 1.9.3)
105
+ * Rails 3.x
106
+ * MiniMagick
107
+
108
+ ## Supported ORM ##
109
+
110
+ * ActiveRecord
111
+ * Mongoid
112
+
113
+ ## Supported Asset Plugin ##
114
+
115
+ * CarrierWave
116
+ * Paperclip
117
+
118
+ ## TODO ##
119
+
120
+ * make sure rails_admin_jcrop load after paperclip/carrierwave load
121
+ * automate :rails_admin_crop for CarrierWave uploader
122
+
123
+ ## Contributing ##
124
+
125
+ Any help is encouraged. Here are some ways you can contribute:
126
+
127
+ * by using it
128
+ * by telling people
129
+ * by reporting bugs or suggesting new features on github issue tracker
130
+ * by fixing bugs or implementing features
131
+
132
+ ## Thanks ##
133
+
134
+ ### Contributors ###
135
+
136
+ * [Jan Xie](https://github.com/janx)
137
+ * [Alan Rosin Sikora](https://github.com/alansikora) ([alansikora](https://github.com/alansikora))
138
+ * [cec](https://github.com/cec)
139
+ * [Alex Chien](https://github.com/AlexChien)
140
+ * [Mitsuhiro Shibuya](https://github.com/mshibuya)
141
+ * [anant anil kolvankar](https://github.com/anantkolvankar)
142
+
143
+ ### And ... ###
144
+
145
+ Life is easier with you.
146
+
147
+ * [RailsAdmin](https://github.com/sferik/rails_admin/)
148
+ * [Jcrop](http://deepliquid.com/content/Jcrop.html)
149
+ * [ParentRailsAdminJcrop](https://github.com/janx/rails_admin_jcrop)
@@ -0,0 +1,27 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'AckRailsAdminJcrop'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+
24
+
25
+
26
+ Bundler::GemHelper.install_tasks
27
+
@@ -0,0 +1,149 @@
1
+ (function($) {
2
+ $.widget("ra.jcropForm", {
3
+
4
+ _create: function() {
5
+ var widget = this;
6
+ var dom_widget = widget.element;
7
+
8
+ var thumbnailLink = dom_widget.find('img.img-polaroid').parent();
9
+ thumbnailLink.unbind().bind("click", function(e){
10
+ widget._bindModalOpening(e, dom_widget.find('a.jcrop_handle').data('link'));
11
+ return false;
12
+ });
13
+ },
14
+
15
+ _bindModalOpening: function(e, url) {
16
+ e.preventDefault();
17
+ widget = this;
18
+ if($("#modal").length)
19
+ return false;
20
+
21
+ var dialog = this._getModal();
22
+
23
+ setTimeout(function(){ // fix race condition with modal insertion in the dom (Chrome => Team/add a new fan => #modal not found when it should have). Somehow .on('show') is too early, tried it too.
24
+ $.ajax({
25
+ url: url,
26
+ beforeSend: function(xhr) {
27
+ xhr.setRequestHeader("Accept", "text/javascript");
28
+ },
29
+ success: function(data, status, xhr) {
30
+ dialog.find('.modal-body').html(data);
31
+ widget._bindFormEvents();
32
+ },
33
+ error: function(xhr, status, error) {
34
+ dialog.find('.modal-body').html(xhr.responseText);
35
+ },
36
+ dataType: 'text'
37
+ });
38
+ },100);
39
+
40
+ },
41
+
42
+ _bindFormEvents: function() {
43
+ var widget = this,
44
+ dialog = this._getModal(),
45
+ form = dialog.find("form"),
46
+ saveButtonText = dialog.find(":submit[name=_save]").html(),
47
+ cancelButtonText = dialog.find(":submit[name=_continue]").html();
48
+ dialog.find('.form-actions').remove();
49
+
50
+ var jcrop_options = $.extend({
51
+ bgColor: 'white',
52
+ keySupport: false,
53
+ onSelect: widget.updateCoordinates
54
+ }, widget.element.find('input[data-rails-admin-jcrop-options]').data('rails-admin-jcrop-options'));
55
+ dialog.find('img.jcrop-subject').Jcrop(jcrop_options)
56
+
57
+ form.attr("data-remote", true);
58
+ dialog.find('.modal-header-title').text(form.data('title'));
59
+ dialog.find('.cancel-action').unbind().click(function(){
60
+ dialog.modal('hide');
61
+ return false;
62
+ }).html(cancelButtonText);
63
+
64
+ dialog.find('.save-action').unbind().click(function(){
65
+ form.submit();
66
+ return false;
67
+ }).html(saveButtonText);
68
+
69
+ $(document).trigger('rails_admin.dom_ready');
70
+
71
+ form.bind("ajax:complete", function(xhr, data, status) {
72
+ if (status == 'error') {
73
+ dialog.find('.modal-body').html(data.responseText);
74
+ widget._bindFormEvents();
75
+ } else {
76
+ var json = $.parseJSON(data.responseText);
77
+ var select = widget.element.find('select').filter(":hidden");
78
+
79
+ thumb = widget.element.find('a.jcrop_handle').data('thumb');
80
+ widget.element.find('img.img-polaroid').removeAttr('src').attr('src', json.urls[thumb] + '?' + new Date().valueOf());
81
+
82
+ widget._trigger("success");
83
+ dialog.modal("hide");
84
+ }
85
+ });
86
+ },
87
+
88
+ updateCoordinates: function(c) {
89
+ var rx = 100/c.w;
90
+ var ry = 100/c.h;
91
+ var lw = $('img.jcrop-subject').width();
92
+ var lh = $('img.jcrop-subject').height();
93
+ var ratio = $('img.jcrop-subject').data('geometry').split(',')[0] / lw ;
94
+
95
+ $('#preview').css({
96
+ width: Math.round(rx * lw) + 'px',
97
+ height: Math.round(ry * lh) + 'px',
98
+ marginLeft: '-' + Math.round(rx * c.x) + 'px',
99
+ marginTop: '-' + Math.round(ry * c.y) + 'px'
100
+ });
101
+
102
+ $("#crop_x").val(Math.round(c.x * ratio));
103
+ $("#crop_y").val(Math.round(c.y * ratio));
104
+ $("#crop_w").val(Math.round(c.w * ratio));
105
+ $("#crop_h").val(Math.round(c.h * ratio));
106
+ },
107
+
108
+ _getModal: function() {
109
+ var widget = this;
110
+ if (!widget.dialog) {
111
+ widget.dialog = $('' +
112
+ '<div id="modal" class="modal fade">' +
113
+ '<div class="modal-dialog">' +
114
+ '<div class="modal-content">' +
115
+ '<div class="modal-header">' +
116
+ '<a href="#" class="close" data-dismiss="modal">&times;</a>' +
117
+ '<h3 class="modal-header-title">Please wait...</h3>' +
118
+ '</div>' +
119
+ '<div class="modal-body">...</div>' +
120
+ '<div class="modal-footer">' +
121
+ '<a href="#" class="btn cancel-action">...</a>' +
122
+ '<a href="#" class="btn btn-primary save-action">...</a>' +
123
+ '</div>' +
124
+ '</div>' +
125
+ '</div>' +
126
+ '</div>');
127
+ widget.dialog.modal({
128
+ keyboard: true,
129
+ backdrop: true,
130
+ show: true
131
+ })
132
+ .on('hidden.bs.modal', function(){
133
+ $(document).unbind("keyup")
134
+ widget.dialog.remove(); // We don't want to reuse closed modals
135
+ widget.dialog = null;
136
+ });
137
+ $(document).unbind("keyup").bind("keyup", function(e) {
138
+ if (e.keyCode == 13) $('#modal .modal-footer .save-action').click(); // enter
139
+ if (e.keyCode == 27) $('#modal .modal-footer .cancel-action').click(); // esc
140
+ });
141
+ }
142
+ return this.dialog;
143
+ }
144
+ });
145
+ })(jQuery);
146
+
147
+ $(function() {
148
+ $('div.jcrop_type').jcropForm();
149
+ });
@@ -0,0 +1,6 @@
1
+ .jcrop_type
2
+ .img-polaroid
3
+ min-width: 50px
4
+
5
+ .jcrop-holder, .jcrop-subject
6
+ max-width: 960px !important
@@ -0,0 +1,111 @@
1
+ require 'mini_magick'
2
+
3
+ module RailsAdmin
4
+
5
+ class JcropController < RailsAdmin::ApplicationController
6
+ skip_before_action :get_model
7
+ before_action :get_model, :get_object, :get_field, :get_fit_image
8
+
9
+ helper_method :abstract_model, :geometry
10
+
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
+ @file_path=''
19
+ #Condition for Carrierwave.
20
+ if @object.send(@field).class.to_s =~ /Uploader/
21
+
22
+ if @object.send(@field)._storage.to_s =~ /Fog/
23
+
24
+ @file_path=@object.send(@field).url
25
+ else
26
+
27
+ @file_path=@object.send(@field).path
28
+ end
29
+ #Condition for Paperclip.
30
+ elsif @object.send(@field).class.to_s =~ /Paperclip/
31
+
32
+ if (@object.send(@field).options[:storage].to_s =='s3')
33
+
34
+ @file_path=@object.send(@field).url
35
+ else
36
+
37
+ @file_path=@object.send(@field).path
38
+ end
39
+ end
40
+
41
+ @image_tag_options[:'data-geometry'] = geometry(@file_path).join(",")
42
+
43
+ if @fit_image_geometry
44
+ fit_image_geometry = fit_image_geometry(@file_path)
45
+
46
+ @form_options[:'style'] = "margin-left: #{375 - (fit_image_geometry[0]/2) - 15}px;"
47
+
48
+ @image_tag_options[:style] = ""
49
+ @image_tag_options[:style] << "width: #{fit_image_geometry[0]}px !important;"
50
+ @image_tag_options[:style] << "height: #{fit_image_geometry[1]}px !important;"
51
+ @image_tag_options[:style] << "border: 1px solid #AAA !important;"
52
+ end
53
+
54
+ respond_to do |format|
55
+ format.html
56
+ format.js { render :edit, :layout => false }
57
+ end
58
+ end
59
+
60
+ def update
61
+ @object.rails_admin_crop! params
62
+
63
+ respond_to do |format|
64
+ format.html { redirect_to_on_success }
65
+ format.js do
66
+ asset = @object.send @field
67
+ urls = {:original => asset.url}
68
+ thumbnail_names.each {|name| urls[name] = asset.url(name)}
69
+
70
+ render :json => {
71
+ :id => @object.id,
72
+ :label => @model_config.with(:object => @object).object_label,
73
+ :field => @field,
74
+ :urls => urls
75
+ }
76
+ end
77
+ end
78
+ end
79
+
80
+ private
81
+
82
+ def thumbnail_names
83
+ RailsAdminJcrop::AssetEngine.thumbnail_names(@object, @field)
84
+ end
85
+
86
+ def get_fit_image
87
+ @fit_image = params[:fit_image] == "true" ? true : false
88
+ end
89
+
90
+ def get_field
91
+ @field = params[:field]
92
+ end
93
+
94
+ def geometry(image_path)
95
+ image = MiniMagick::Image.open(image_path)
96
+ [image[:width], image[:height]]
97
+ end
98
+
99
+ def fit_image_geometry(image_path)
100
+ image = MiniMagick::Image.open(image_path)
101
+ # Magic number origin: https://github.com/janx/rails_admin_jcrop/pull/2
102
+ image.resize "720x400"
103
+ [image[:width], image[:height]]
104
+ end
105
+
106
+ def cropping?
107
+ [:crop_x, :crop_y, :crop_w, :crop_h].all? {|c| params[c].present?}
108
+ end
109
+ end
110
+
111
+ end