Module: Poodle::ImageHelper

Defined in:
app/helpers/poodle/image_helper.rb

Instance Method Summary collapse

Instance Method Details

#display_image(object, method_name, **options) ⇒ Object

This method will render the image with required width and height. The image url will be set to the placeholder url if the object doesn’t respond to the image method

Examples:

Basic Usage without Image


>>> display_image(user, "profile_picture.image.thumb.url")
"<img class=\"\" width=\"100%\" height=\"auto\" src=\"http://placehold.it/100x60&amp;text=&lt;No Image&gt;\" alt=\"100x60&amp;text=&lt;no image&gt;\" />"

Basic Usage with image


>>> display_image(user, "profile_picture.image.thumb.url", width: "100%", height:"100%")
"<img class=\"\" width=\"100%\" height=\"100%\" src=\"http://placehold.it/100x60&amp;text=&lt;No Image&gt;\" alt=\"100x60&amp;text=&lt;no image&gt;\" />"

Advanced Usage with width & height


>>> display_image(user_with_image, 'profile_picture.image.large.url', width: "30px", height: "50px", place_holder: {width: 100, height: 50, text: "SOME TEXT"})
"<img class=\"\" width=\"30px\" height=\"50px\" src=\"/spec/dummy/uploads/image/profile_picture/1/large_test.jpg\" alt=\"Large test\" />"


75
76
77
78
79
80
81
82
83
84
85
# File 'app/helpers/poodle/image_helper.rb', line 75

def display_image(object, method_name, **options)
  options.reverse_merge!(
    width: "100%",
    height: "auto",
    place_holder: {},
    class: object.persisted? ? "#{object.id}-poodle-thumb-image" : ""
  )

  img_url = image_url(object, method_name, **options[:place_holder])
  return image_tag(img_url, class: options[:class], width: options[:width], height: options[:height])
end

#display_user_image(user, method_name, **options) ⇒ Object

Examples:

Basic Usage


>>> display_user_image(@user, 'profile_picture.image.thumb.url')
"<div><div class="rounded" style="width:60px;height:60px;"><img alt="Thumb krishnan" class="" src="/uploads/image/profile_picture/39/thumb_krishnan.jpg" style="width:100%;height:auto;cursor:default;"></div></div>"

Advanced Usage with


>>> display_user_image(@user, 'profile_picture.image.thumb.url', width: 100px, height: 100px)
"<div><div class="rounded" style="width:100px;height:60px;"><img alt="Thumb krishnan" class="" src="/uploads/image/profile_picture/39/thumb_krishnan.jpg" style="width:100%;height:auto;cursor:default;"></div></div>"


96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'app/helpers/poodle/image_helper.rb', line 96

def display_user_image(user, method_name, **options)

  url_domain = defined?(QAuthRubyClient) ? QAuthRubyClient.configuration.q_auth_url : ""

  options.reverse_merge!(
    width: "60px",
    height: "auto",
    url_domain: url_domain,
    place_holder: {},
    html_options: {}
  )

  options[:html_options].reverse_merge!(
    style: "width:100%;height:auto;cursor:#{options.has_key?(:popover) ? "pointer" : "default"};",
    class: user.persisted? ? "#{user.id}-poodle-thumb-image" : ""
  )

  if user.respond_to?(:name)
    options[:place_holder].reverse_merge!(text: namify(user.name))
  end

  options[:html_options].reverse_merge!(
    "data-toggle" => "popover",
    "data-placement" => "bottom",
    "title" => user.name,
    "data-content" => options[:popover] === true ? "" : options[:popover].to_s
  ) if options[:popover]

  begin
    image_path = user.send(:eval, method_name)
    if image_path.starts_with?(:http)
      url = image_path
    else
      url = options[:url_domain] + image_path
    end
  rescue
    url = placeholdit(**options[:place_holder])
  end

  (:div) do
    (:div, class: "rounded", style: "width:#{options[:width]};height:#{options[:height]}") do
      image_tag(url, options[:html_options])
    end
  end
end

#edit_image(object, method_name, edit_url, **options) ⇒ Object

Displays the image with a edit button below it

Examples:

Basic Usage

>>> edit_image(@project, "logo.image.url", edit_url, width: "100px", height: "auto")
""


146
147
148
149
150
151
152
153
154
155
156
157
# File 'app/helpers/poodle/image_helper.rb', line 146

def edit_image(object, method_name, edit_url, **options)
  options.reverse_merge!(
    remote: true,
    text: "Change Image",
    icon: "photo",
    classes: "btn btn-default btn-xs mt-10"
  )
  img_tag = display_image(object, method_name, **options)
  btn_display = raw(theme_fa_icon(options[:icon]) + theme_button_text(options[:text]))
  link_to(img_tag, edit_url, :remote => options[:remote]) +
  link_to(btn_display, edit_url, :class=>options[:classes], :remote=>options[:remote])
end

#edit_user_image(object, method_name, edit_url, **options) ⇒ Object

Displays the user image in a rounded frame with a edit button below it

Examples:

Basic Usage

>>> edit_user_image(@project, "logo.image.url", edit_url, width: "100px", height: "auto")
""


163
164
165
166
167
168
169
170
171
172
173
174
# File 'app/helpers/poodle/image_helper.rb', line 163

def edit_user_image(object, method_name, edit_url, **options)
  options.reverse_merge!(
    remote: true,
    text: "Change Image",
    icon: "photo",
    classes: "btn btn-default btn-xs mt-10"
  )
  img_tag = display_user_image(object, method_name, **options)
  btn_display = raw(theme_fa_icon(options[:icon]) + theme_button_text(options[:text]))
  link_to(img_tag, edit_url, :remote => options[:remote]) +
  link_to(btn_display, edit_url, :class=>options[:classes], :remote=>options[:remote])
end

#image_url(object, method_name, **options) ⇒ Object

image_url is a helper method which can be used along with carrier_wave gem Suppose, you have an object ‘user’ which has a profile_picture image_url will return you placehold.it image if profile_picture is not set else it will return you the carrier wave url

Examples:

Basic Usage without Image


>>> image_url(user, "profile_picture.image.thumb.url")
"http://placehold.it/60x60&text=<No Image>"

Basic Usage with Image


>>> image_url(user_with_image, "profile_picture.image.thumb.url")
"uploads/profile_picture/image/1/thumb_test.jpg"

Advance Usage with Placehold.it Arguments


>>> image_url(user, "profile_picture.image.large.url", {width: 40, height: 10, text: "Pic"})
"http://placehold.it/40x10&text=Pic"


48
49
50
51
52
53
54
55
56
# File 'app/helpers/poodle/image_helper.rb', line 48

def image_url(object, method_name, **options)
  begin
    url = object.send :eval, method_name
    raise if url.blank?
  rescue
    url = placeholdit(**options)
  end
  return url
end

#namify(name) ⇒ Object

namify returns the first letters of first name and last name



9
10
11
# File 'app/helpers/poodle/image_helper.rb', line 9

def namify(name)
  name.split(" ").map{|x| x.first.capitalize}[0..1].join("")
end

#placeholdit(**options) ⇒ Object

placeholdit is a helper method used to return placechold.it urls with custom width, height and text It is quite useful for POC Applications to get started with place holder images while developing views

Examples:

Without Any Arguments


>>> placeholdit()
"http://placehold.it/60x60&text=<No Image>"

With width, height and custom text


>>> placeholdit(width: 120, height: 80, text: "User")
"http://placehold.it/120x80&text=User"


25
26
27
28
# File 'app/helpers/poodle/image_helper.rb', line 25

def placeholdit(**options)
  options.reverse_merge!( width: 60, height:  60, text: "<No Image>" )
  "http://placehold.it/#{options[:width]}x#{options[:height]}&text=#{options[:text]}"
end

Returns new photo url or edit existing photo url based on object is associated with photo or not

Examples:

Basic Usage - User without Image

>>> upload_image_link(@user, :profile_picture, :admin)
"/admin/images/new"

Basic Usage - User with Iamge


>>> upload_image_link(@user_with_image, :profile_picture, :admin)
"/admin/images/1/edit"

Basic Usage - Custom Scope


>>> upload_image_link(@project, :profile_picture, :client)
"/client/images/new"
>>> upload_image_link(@project_with_image, :profile_picture, :client)
"/client/images/1/edit"


192
193
194
195
196
197
198
199
200
201
# File 'app/helpers/poodle/image_helper.rb', line 192

def upload_image_link(object, assoc_name=:photo, scope=:admin)
  photo_object = nil
  photo_object =  object.send(assoc_name) if object.respond_to?(assoc_name)
  if photo_object.present? && photo_object.persisted?
    url_for([:edit, scope, :image, id: photo_object.id, imageable_id: object.id, imageable_type: object.class.to_s, image_type: photo_object.class.name])
  else
    photo_object = object.send("build_#{assoc_name}")
    url_for([:new, scope, :image, imageable_id: object.id, imageable_type: object.class.to_s, image_type: photo_object.class.name])
  end
end