Module: ActionView::Helpers::FormHelper

Defined in:
lib/simple_crop/action_view_extensions/form_helper.rb

Instance Method Summary collapse

Instance Method Details

#sanitized_object_name(object_name) ⇒ Object



34
35
36
# File 'lib/simple_crop/action_view_extensions/form_helper.rb', line 34

def sanitized_object_name(object_name)
  object_name.gsub(/\]\[|[^-a-zA-Z0-9:.]/, "_").sub(/_$/, "")
end

#simple_crop(object_name, method, options = {}) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/simple_crop/action_view_extensions/form_helper.rb', line 4

def simple_crop(object_name, method, options = {})
  default_options = {"select" => :original, "crop" => :original, "script" => true}
  options = options.stringify_keys!
  options = default_options.merge(options)
  script = options["script"]
  
  # Build image and preview
  output = InstanceTag.new(object_name, method, self, options).to_simple_crop_tag(options)
  output << InstanceTag.new(object_name, method, self, options).to_simple_crop_preview_tag(options) unless (options["select"] == options["crop"])
  
  # Build hidden fields for accessors on model used by simple_crop
  output << InstanceTag.new(object_name, :simple_crop_x, self).to_input_field_tag("hidden")
  output << InstanceTag.new(object_name, :simple_crop_y, self).to_input_field_tag("hidden")
  output << InstanceTag.new(object_name, :simple_crop_w, self).to_input_field_tag("hidden")
  output << InstanceTag.new(object_name, :simple_crop_h, self).to_input_field_tag("hidden")
  output << InstanceTag.new(object_name, :simple_crop_style, self).to_input_field_tag("hidden", {:value => options["crop"]})
  
  # Initialize jQuery plugin
  # Either output inline or yield to provided template
  if script === true
    output << %Q(<script type="text/javascript" charset="utf-8">$(window).load(function() {$('img[data-crop-for="#{sanitized_object_name(object_name)}"]').SimpleCrop();});</script>).html_safe
  elsif not script === false
    content_for script do
      %Q($('img[data-crop-for="#{sanitized_object_name(object_name)}"]').SimpleCrop();).html_safe
    end
  end
  
  output
end