jcropper 0.3.3 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.rdoc CHANGED
@@ -50,6 +50,11 @@ Add the helpers to your view:
50
50
  <%= croppable_image @user, :avatar, :thumb -%>
51
51
  <%= croppable_image_preview @user, :avatar, :thumb -%>
52
52
 
53
+ Optional syntax, with :user being the name for the form submit.
54
+ <%= croppable_image [@site.user, :user], :avatar, :thumb -%>
55
+ <%= croppable_image_preview [@site.user, :user], :avatar, :thumb -%>
56
+
57
+
53
58
  Helper Options:
54
59
  :css_prefix => 'jcrop',
55
60
  :width => 100, :height => 100,
@@ -61,9 +66,10 @@ Helper Options:
61
66
  - jcropper currently only supports ONE cropped style per attachment, one cropped attachment per model
62
67
  - croppedImage.jcrop gets you the jcrop api object (see jcrop docs)
63
68
  - in helpers, :aspect_ratio defaults to the aspect ratio of the original image
64
- - in helpers, width and height default to the target geometry for the style
69
+ - in helpers, width and height default to the target geometry for the style. if you override one of width/height you must override both
65
70
  - you'll have to read the jcropper.js source for function docs there
66
- - tried only on rails 2.3.5 and ruby 1.8.7
71
+ - should be working on ruby 1.8/1.9 and rails 2.3/3, although it has not been thoroughly tested
72
+ - jcrop :avatar, :thumb, :maintain_aspect_ratio => true will lock aspect_ratio to the target geometry and default to a north/center max crop
67
73
  - test/code ratio is 0
68
74
 
69
75
  Copyright (c) 2010 Symbolforce, LLC., released under the MIT license
data/Rakefile CHANGED
@@ -6,7 +6,7 @@ begin
6
6
  require 'jeweler'
7
7
  Jeweler::Tasks.new do |gemspec|
8
8
  gemspec.name = "jcropper"
9
- gemspec.summary = "gem plugin wrapping jquery "
9
+ gemspec.summary = "gem plugin wrapping jquery.jcrop.js "
10
10
  gemspec.description = ""
11
11
  gemspec.email = "ryan@symbolforce.com"
12
12
  gemspec.homepage = "http://github.com/syfo/jcropper"
data/TODO CHANGED
@@ -17,4 +17,5 @@
17
17
  - is it really necc to have a paperclip processor?
18
18
  - use inline css or separate css
19
19
  - reset cropping when uploading new image
20
- - make options[:view_size] work
20
+ - make options[:view_size] work
21
+ - add a crop_updated_at field
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.3
1
+ 0.4.0
@@ -1,5 +1,5 @@
1
1
  class JcropperGenerator < Rails::Generator::NamedBase
2
- attr_accessor :attachment, :migration_name, :style
2
+ attr_accessor :attachment, :style
3
3
 
4
4
  def initialize(args, options = {})
5
5
  super
@@ -7,6 +7,10 @@ class JcropperGenerator < Rails::Generator::NamedBase
7
7
  @class_name, @attachment, @style = args[0], args[1], args[2]
8
8
  end
9
9
 
10
+ def migration_name
11
+ @migration_name.underscore
12
+ end
13
+
10
14
  def manifest
11
15
  file_name = generate_file_name
12
16
  @migration_name = file_name.camelize
@@ -1,4 +1,4 @@
1
- class <%= migration_name %> < ActiveRecord::Migration
1
+ class <%= migration_name.camelize %> < ActiveRecord::Migration
2
2
  def self.up
3
3
  add_column :<%= class_name.underscore.camelize.tableize %>, :<%= JCropper.jattr attachment, style, :x %>, :integer, :default => 0
4
4
  add_column :<%= class_name.underscore.camelize.tableize %>, :<%= JCropper.jattr attachment, style, :y %>, :integer, :default => 0
data/jcropper.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{jcropper}
8
- s.version = "0.3.3"
8
+ s.version = "0.4.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Ryan Ziegler"]
12
- s.date = %q{2010-06-11}
12
+ s.date = %q{2011-05-09}
13
13
  s.description = %q{}
14
14
  s.email = %q{ryan@symbolforce.com}
15
15
  s.extra_rdoc_files = [
@@ -28,18 +28,20 @@ Gem::Specification.new do |s|
28
28
  "generators/jcropper/templates/jcropper_migration.rb.erb",
29
29
  "install.rb",
30
30
  "jcropper.gemspec",
31
+ "lib/generators/jcropper/jcropper_generator.rb",
31
32
  "lib/jcropper.rb",
32
33
  "lib/jcropper/cropped_image.rb",
33
34
  "lib/jcropper/helpers.rb",
34
35
  "lib/jcropper/jcropper.rb",
35
36
  "lib/jcropper/tasks.rb",
36
37
  "lib/paperclip_processors/jcropper.rb",
38
+ "lib/railtie.rb",
39
+ "lib/tasks/jcropper.rake",
37
40
  "public/images/Jcrop.gif",
38
41
  "public/javascripts/jcropper.js",
39
42
  "public/javascripts/jquery.Jcrop.min.js",
40
43
  "public/stylesheets/jquery.Jcrop.css",
41
44
  "rails/init.rb",
42
- "tasks/jcropper.rake",
43
45
  "test/jcropper_test.rb",
44
46
  "test/test_helper.rb",
45
47
  "uninstall.rb"
@@ -48,7 +50,7 @@ Gem::Specification.new do |s|
48
50
  s.rdoc_options = ["--charset=UTF-8"]
49
51
  s.require_paths = ["lib"]
50
52
  s.rubygems_version = %q{1.3.7}
51
- s.summary = %q{gem plugin wrapping jquery}
53
+ s.summary = %q{gem plugin wrapping jquery.jcrop.js}
52
54
  s.test_files = [
53
55
  "test/jcropper_test.rb",
54
56
  "test/test_helper.rb"
@@ -0,0 +1,35 @@
1
+ require 'rails/generators/active_record'
2
+
3
+ class JcropperGenerator < ActiveRecord::Generators::Base
4
+ desc "Create a migration to add jcropper-specific fields to your model."
5
+
6
+ # argument :model, :required => true, :type => :string, :desc => "The name of the ActiveRecord model", :banner => "<Model>"
7
+ argument :attachment, :required => true, :type => :string, :desc => "The name of the paperclip attachment", :banner => "<attachment>"
8
+ argument :style, :required => true, :type => :string, :desc => "", :banner => "<style>"
9
+
10
+ def self.source_root
11
+ @source_root ||= File.expand_path('../../../../generators/jcropper/templates', __FILE__)
12
+ end
13
+
14
+ def generate_migration
15
+ migration_template "jcropper_migration.rb.erb", "db/migrate/#{migration_file_name}"
16
+ end
17
+
18
+ protected
19
+
20
+ def banner
21
+ "<Model>"
22
+ end
23
+
24
+ def migration_name
25
+ "add_crop_variables_for_#{attachment}_#{style}_to_#{name.underscore}"
26
+ end
27
+
28
+ def migration_file_name
29
+ "#{migration_name}.rb"
30
+ end
31
+
32
+ def migration_class_name
33
+ migration_name.camelize
34
+ end
35
+ end
data/lib/jcropper.rb CHANGED
@@ -1,5 +1,8 @@
1
- ROOT = File.join(File.dirname(__FILE__), 'jcropper')
2
- require File.join(ROOT, 'jcropper')
3
- require File.join(ROOT, 'helpers')
4
- require File.join(ROOT, 'cropped_image')
5
- Paperclip.autoload 'Jcropper', File.join(ROOT, '../paperclip_processors/jcropper.rb')
1
+ require 'railtie' if defined?(Rails) and Rails.version.split('.').first.to_i > 2
2
+ root = File.join(File.dirname(__FILE__), 'jcropper')
3
+ require File.join(root, 'jcropper')
4
+ require File.join(root, 'helpers')
5
+ require File.join(root, 'cropped_image')
6
+
7
+ #Paperclip.autoload 'Jcropper', File.join(root, '../paperclip_processors/jcropper.rb')
8
+ require File.join(root, '../paperclip_processors/jcropper.rb')
@@ -1,9 +1,10 @@
1
1
  module JCropper
2
2
  class CroppedImage
3
- attr_reader :original_geometry, :cropped_geometry, :attachment_name, :style_name, :coord_names, :starting_crop, :attachment
3
+ attr_reader :original_geometry, :cropped_geometry, :attachment_name, :style_name, :coord_names, :starting_crop, :attachment, :options
4
4
 
5
- def initialize(object)
5
+ def initialize(object, options)
6
6
  @object = object
7
+ @options = options
7
8
  @attachment_name = object.class.jcropper_defs[:attachment]
8
9
  @style_name = object.jcropper_defs[:style]
9
10
  @attachment = object.send(attachment_name)
@@ -25,9 +26,19 @@ module JCropper
25
26
  def target_geometry
26
27
  @target_geometry ||= Paperclip::Geometry.parse(@object.send(attachment_name).styles[style_name.to_sym][:geometry])
27
28
  end
28
-
29
+
29
30
  def max_crop
30
- [0, 0, original_geometry.width, original_geometry.height]
31
+ if options[:maintain_aspect_ratio]
32
+ north_center_gravity_max_crop
33
+ else
34
+ [0, 0, original_geometry.width, original_geometry.height]
35
+ end
36
+ end
37
+
38
+ def north_center_gravity_max_crop
39
+ scale = JCropper.find_bounding_scale([original_geometry.width, original_geometry.height], [target_geometry.width, target_geometry.height])
40
+ final_size = {:width => scale*target_geometry.width, :height => scale*target_geometry.height}
41
+ [(original_geometry.width - final_size[:width]) / 2, 0, final_size[:width], final_size[:height]].map &:to_i
31
42
  end
32
43
  end
33
44
  end
@@ -1,23 +1,23 @@
1
1
  module JCropper
2
2
  module Helpers
3
- def croppable_image(object_name, attachment, style, options = {})
4
- object = eval("@#{object_name.to_s}") unless object_name.is_a? ActiveRecord::Base
5
- options[:view_size] = {:width => options.delete(:width), :height => options.delete(:height)}
3
+ def croppable_image(object_and_name, attachment, style, options = {})
4
+ object, name = split_object_and_name(object_and_name)
5
+ options[:view_size] = {:width => options.delete(:width), :height => options.delete(:height)} if options[:width] and options[:height]
6
6
  return unless options = parse_options(object, options)
7
7
 
8
8
  ###CRZ - duplicated in JS!
9
- view_scale = find_bounding_scale([options[:view_size][:width], options[:view_size][:height]],
9
+ view_scale = JCropper.find_bounding_scale([options[:view_size][:width], options[:view_size][:height]],
10
10
  [options[:original_geometry][:width], options[:original_geometry][:height]])
11
11
 
12
12
  scaled_img_dims = {:width => options[:original_geometry][:width]*view_scale, :height => options[:original_geometry][:height]*view_scale}
13
13
  s = "<div class='#{options[:css_prefix]}'>#{image_tag(object.send(attachment).url, scaled_img_dims.merge(:id => options[:id]))}</div>" + "\n"
14
- s += hidden_field_tag("#{object_name}[#{@js_cropper_c_i.coord_names[:x]}]", @js_cropper_c_i.starting_crop[:x], :id => @js_cropper_c_i.coord_names[:x]) + "\n"
15
- s += hidden_field_tag("#{object_name}[#{@js_cropper_c_i.coord_names[:y]}]", @js_cropper_c_i.starting_crop[:y], :id => @js_cropper_c_i.coord_names[:y]) + "\n"
16
- s += hidden_field_tag("#{object_name}[#{@js_cropper_c_i.coord_names[:w]}]", @js_cropper_c_i.starting_crop[:w], :id => @js_cropper_c_i.coord_names[:w]) + "\n"
17
- s += hidden_field_tag("#{object_name}[#{@js_cropper_c_i.coord_names[:h]}]", @js_cropper_c_i.starting_crop[:h], :id => @js_cropper_c_i.coord_names[:h]) + "\n"
14
+ s += hidden_field_tag("#{name}[#{@js_cropper_c_i.coord_names[:x]}]", @js_cropper_c_i.starting_crop[:x], :id => @js_cropper_c_i.coord_names[:x]) + "\n"
15
+ s += hidden_field_tag("#{name}[#{@js_cropper_c_i.coord_names[:y]}]", @js_cropper_c_i.starting_crop[:y], :id => @js_cropper_c_i.coord_names[:y]) + "\n"
16
+ s += hidden_field_tag("#{name}[#{@js_cropper_c_i.coord_names[:w]}]", @js_cropper_c_i.starting_crop[:w], :id => @js_cropper_c_i.coord_names[:w]) + "\n"
17
+ s += hidden_field_tag("#{name}[#{@js_cropper_c_i.coord_names[:h]}]", @js_cropper_c_i.starting_crop[:h], :id => @js_cropper_c_i.coord_names[:h]) + "\n"
18
18
  s += <<-JS
19
19
  <script type="text/javascript">
20
- #{options[:js_object]} = new CroppedImage(jQuery('.#{options[:css_prefix]} img'),
20
+ #{options[:js_object]} = new CroppedImage('.#{options[:css_prefix]} img',
21
21
  $.extend(
22
22
  #{camelize_keys(options).to_json},
23
23
  {
@@ -28,12 +28,13 @@ module JCropper
28
28
  );
29
29
  </script>
30
30
  JS
31
- s
31
+
32
+ respond_to?(:raw) ? raw(s) : s
32
33
  end
33
34
 
34
- def croppable_image_preview(object_name, attachment, style, options = {})
35
- object = eval("@#{object_name.to_s}") unless object_name.is_a? ActiveRecord::Base
36
- options[:preview_size] = {:width => options.delete(:width), :height => options.delete(:height)}
35
+ def croppable_image_preview(object_and_name, attachment, style, options = {})
36
+ object, name = split_object_and_name(object_and_name)
37
+ options[:preview_size] = {:width => options.delete(:width), :height => options.delete(:height)} if options[:width] and options[:height]
37
38
  return unless options = parse_options(object, options)
38
39
 
39
40
  s = <<-HTML
@@ -41,7 +42,7 @@ module JCropper
41
42
  #{image_tag(@js_cropper_c_i.attachment.url(:original), :class => "#{options[:css_prefix]}-preview")}
42
43
  </div>
43
44
  HTML
44
- s
45
+ respond_to?(:raw) ? raw(s) : s
45
46
  end
46
47
 
47
48
  private
@@ -50,19 +51,15 @@ module JCropper
50
51
  h.merge(pair[0].to_s.camelize(:lower) => pair[1])
51
52
  end
52
53
  end
53
-
54
- def find_bounding_scale(container, to_contain)
55
- to_contain_aspect = to_contain[0].to_f / to_contain[1]
56
- container_aspect = container[0].to_f / container[1]
57
-
58
- if to_contain_aspect > container_aspect
59
- return (container[0].to_f / to_contain[0]);
54
+
55
+ def split_object_and_name(object_and_name)
56
+ if object_and_name.is_a? Array
57
+ object_and_name
60
58
  else
61
- return (container[1].to_f / to_contain[1]);
59
+ [eval("@#{object_and_name.to_s}"), object_and_name]
62
60
  end
63
61
  end
64
62
 
65
-
66
63
  def parse_options(object, options = {})
67
64
  @js_cropper_c_i ||= object.cropped_image
68
65
 
@@ -76,7 +73,8 @@ module JCropper
76
73
  :aspect_ratio => @js_cropper_c_i.target_geometry.width / @js_cropper_c_i.target_geometry.height,
77
74
  :original_geometry => {:width => @js_cropper_c_i.original_geometry.width, :height => @js_cropper_c_i.original_geometry.height},
78
75
  :starting_crop => @js_cropper_c_i.starting_crop,
79
- :js_object => 'croppedImage'
76
+ :js_object => 'croppedImage' + SecureRandom.hex(10),
77
+ :jcrop_options => @js_cropper_c_i.options[:maintain_aspect_ratio] ? {:aspectRatio => @js_cropper_c_i.target_geometry.width / @js_cropper_c_i.target_geometry.height} : {}
80
78
  }.merge(@js_cropper_options.merge(options))
81
79
  end
82
80
  end
@@ -1,6 +1,17 @@
1
1
  module JCropper
2
+ def self.find_bounding_scale(container, to_contain)
3
+ to_contain_aspect = to_contain[0].to_f / to_contain[1]
4
+ container_aspect = container[0].to_f / container[1]
5
+
6
+ if to_contain_aspect > container_aspect
7
+ return (container[0].to_f / to_contain[0]);
8
+ else
9
+ return (container[1].to_f / to_contain[1]);
10
+ end
11
+ end
12
+
2
13
  module ClassMethods
3
- def jcrop(attachment, style)
14
+ def jcrop(attachment, style, options = {})
4
15
  raise "jcropper requires attachment to be of type Paperclip::Attachment" if self.attachment_definitions[attachment.to_sym].nil?
5
16
 
6
17
  attr_reader :cropped_image
@@ -11,12 +22,23 @@ module JCropper
11
22
  after_save :jcropper_reprocess
12
23
 
13
24
  x, y, w, h = [:x, :y, :w, :h].map{|coord| JCropper.jattr(attachment, style, coord) }
14
- to_eval = <<-TO_EVAL
15
- ###CRZ - alias chain this
16
- def after_initialize
17
- @cropped_image = CroppedImage.new(self)
18
- end
19
-
25
+ if defined?(Rails) and Rails.version.split('.').first.to_i > 2
26
+ to_eval = <<-TO_EVAL
27
+ after_initialize :jcropper_initialize
28
+ def jcropper_initialize
29
+ @cropped_image = CroppedImage.new(self, #{options.to_hash})
30
+ end
31
+ TO_EVAL
32
+ else
33
+ to_eval = <<-TO_EVAL
34
+ ###CRZ - alias chain this
35
+ def after_initialize
36
+ @cropped_image = CroppedImage.new(self, #{options.to_hash})
37
+ end
38
+ TO_EVAL
39
+ end
40
+
41
+ to_eval += <<-TO_EVAL
20
42
  def jcropper_reprocess
21
43
  cropped_image.attachment.reprocess! if @jcropper_should_reprocess
22
44
  end
data/lib/railtie.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'jcropper'
2
+ require 'rails'
3
+
4
+ module Jcropper
5
+ class Railtie < Rails::Railtie
6
+ rake_tasks do
7
+ load "tasks/jcropper.rake"
8
+ end
9
+ end
10
+ end
@@ -1,7 +1,7 @@
1
1
  namespace :jcropper do
2
2
  desc "Install .CSS and .js files required for view helpers to function. Will not overwrite"
3
3
  task :install => :environment do
4
- root = File.join(File.dirname(__FILE__), '..')
4
+ root = File.join(File.dirname(__FILE__), '../../')
5
5
  install_file(File.join(root, 'public/images/Jcrop.gif'), File.join(Rails.root, 'public/images/Jcrop.gif'))
6
6
  install_file(File.join(root, 'public/javascripts/jquery.Jcrop.min.js'), File.join(Rails.root, 'public/javascripts/jquery.Jcrop.min.js'))
7
7
  install_file(File.join(root, 'public/javascripts/jcropper.js'), File.join(Rails.root, 'public/javascripts/jcropper.js'))
@@ -30,7 +30,7 @@ CroppedImage = function(image, properties) {
30
30
  t.log = function() {
31
31
  try {
32
32
  if(typeof console == 'object' && typeof console.log == 'function') {
33
- console.log.apply(this, arguments);
33
+ console.log.apply(console, arguments);
34
34
  }
35
35
  } catch(e) {;}
36
36
  }
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jcropper
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
4
+ hash: 15
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 3
9
- - 3
10
- version: 0.3.3
8
+ - 4
9
+ - 0
10
+ version: 0.4.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Ryan Ziegler
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-06-11 00:00:00 -04:00
18
+ date: 2011-05-09 00:00:00 -04:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
@@ -40,18 +40,20 @@ files:
40
40
  - generators/jcropper/templates/jcropper_migration.rb.erb
41
41
  - install.rb
42
42
  - jcropper.gemspec
43
+ - lib/generators/jcropper/jcropper_generator.rb
43
44
  - lib/jcropper.rb
44
45
  - lib/jcropper/cropped_image.rb
45
46
  - lib/jcropper/helpers.rb
46
47
  - lib/jcropper/jcropper.rb
47
48
  - lib/jcropper/tasks.rb
48
49
  - lib/paperclip_processors/jcropper.rb
50
+ - lib/railtie.rb
51
+ - lib/tasks/jcropper.rake
49
52
  - public/images/Jcrop.gif
50
53
  - public/javascripts/jcropper.js
51
54
  - public/javascripts/jquery.Jcrop.min.js
52
55
  - public/stylesheets/jquery.Jcrop.css
53
56
  - rails/init.rb
54
- - tasks/jcropper.rake
55
57
  - test/jcropper_test.rb
56
58
  - test/test_helper.rb
57
59
  - uninstall.rb
@@ -88,7 +90,7 @@ rubyforge_project:
88
90
  rubygems_version: 1.3.7
89
91
  signing_key:
90
92
  specification_version: 3
91
- summary: gem plugin wrapping jquery
93
+ summary: gem plugin wrapping jquery.jcrop.js
92
94
  test_files:
93
95
  - test/jcropper_test.rb
94
96
  - test/test_helper.rb