Module: ImageHelper
- Included in:
- Kuppayam::BaseController
- Defined in:
- app/helpers/image_helper.rb
Instance Method Summary collapse
-
#display_image(object, method_name, **options) ⇒ Object
This method will render the image with required width and height.
-
#edit_image(object, method_name, edit_url, delete_url, **options) ⇒ Object
Displays the image with a edit button below it.
-
#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 the system default image url if profile_picture is not set else it will return you the carrier wave url the convention is that the there is a folder named defaults in /assets which has user-small.png file user-small => user is the class name and small is the size name for e.g: if the class name is JackFruit, then: the filename which it expect would be /assets/defaults/jackfruit-small.png.
-
#namify(name) ⇒ Object
namify returns the first letters of first name and last name.
-
#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.
- #remove_image_link(object, assoc_name = :photo, scope = :admin, **options) ⇒ Object
-
#upload_image_link(object, assoc_name = :photo, scope = :admin, **options) ⇒ Object
Returns new photo url or edit existing photo url based on object is associated with photo or not.
- #upload_multiple_image_link(object, assoc_name = :photo, scope = :admin, **options) ⇒ Object
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
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'app/helpers/image_helper.rb', line 79 def display_image(object, method_name, **) .reverse_merge!(size: "large") .reverse_merge!( width: "100%", height: "auto", place_holder: {}, class: "", alt: "" ) [:class] = [:class] + (object.persisted? ? " #{object.id}-#{[:size]}-image" : "") img_url = image_url(object, method_name, **) return image_tag(img_url, class: [:class], width: [:width], height: [:height], alt: [:alt]) end |
#edit_image(object, method_name, edit_url, delete_url, **options) ⇒ Object
Displays the image with a edit button below it
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 141 |
# File 'app/helpers/image_helper.rb', line 99 def edit_image(object, method_name, edit_url, delete_url, **) .reverse_merge!( image_options: {}, edit_options: {}, delete_options: {} ) # options[:image_options].reverse_merge!() [:edit_options].reverse_merge!( remote: true, text: "Change", icon: "photo", class: "btn btn-primary btn-block btn-only-hover btn-xs", ) [:delete_options].reverse_merge!( remote: true, text: "Remove", icon: "trash", class: "btn btn-danger btn-block btn-only-hover btn-xs" ) # Image HTML image_tag = display_image(object, method_name, **[:image_options]) # Edit Button edit_btn_display = raw(theme_fa_icon([:edit_options][:icon]) + ([:edit_options][:text])) edit_btn = link_to(edit_btn_display, edit_url, :class=>[:edit_options][:class], :remote=>[:edit_options][:remote]) # Remove Button (only if the object is persisted) assoc_name = [:image_options][:assoc_name] image_object = nil image_object = object.send(assoc_name) if object.respond_to?(assoc_name) if image_object && image_object.persisted? remove_btn_display = raw(theme_fa_icon([:delete_options][:icon]) + ([:delete_options][:text])) remove_btn = link_to(remove_btn_display, delete_url, :class=>[:delete_options][:class], :remote=>[:delete_options][:remote], method: :delete) end link_to(image_tag, edit_url, :remote => [:edit_options][:remote]) + content_tag(:div, class: "btn-group btn-group-justified mt-10") do edit_btn + (object.persisted? ? remove_btn : "") end 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 the system default image url if profile_picture is not set else it will return you the carrier wave url the convention is that the there is a folder named defaults in /assets which has user-small.png file user-small => user is the class name and small is the size name for e.g: if the class name is JackFruit, then:
the filename which it expect would be /assets/defaults/jackfruit-small.png
51 52 53 54 55 56 57 58 59 60 |
# File 'app/helpers/image_helper.rb', line 51 def image_url(object, method_name, **) begin url = object.send :eval, method_name raise if url.blank? rescue .reverse_merge!(size: "small") url = object.respond_to?(:default_image_url) ? object.default_image_url([:size]) : "kuppayam/defaults/default-#{[:size]}.png" end return url end |
#namify(name) ⇒ Object
namify returns the first letters of first name and last name
8 9 10 |
# File 'app/helpers/image_helper.rb', line 8 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
24 25 26 27 |
# File 'app/helpers/image_helper.rb', line 24 def placeholdit(**) .reverse_merge!( width: 60, height: 60, text: "<No Image>" ) "http://placehold.it/#{[:width]}x#{[:height]}&text=#{[:text]}" end |
#remove_image_link(object, assoc_name = :photo, scope = :admin, **options) ⇒ Object
175 176 177 178 179 180 181 182 183 |
# File 'app/helpers/image_helper.rb', line 175 def remove_image_link(object, assoc_name=:photo, scope=:admin, **) image_object = nil image_object = object.send(assoc_name) if object.respond_to?(assoc_name) if image_object.persisted? main_app.url_for([scope, :image, id: image_object.id, imageable_id: object.id, imageable_type: object.class.to_s, image_type: image_object.class.name]) else "#" end end |
#upload_image_link(object, assoc_name = :photo, scope = :admin, **options) ⇒ Object
Returns new photo url or edit existing photo url based on object is associated with photo or not
159 160 161 162 163 164 165 166 167 168 |
# File 'app/helpers/image_helper.rb', line 159 def upload_image_link(object, assoc_name=:photo, scope=:admin, **) image_object = nil image_object = object.send(assoc_name) if object.respond_to?(assoc_name) if image_object.present? && image_object.persisted? main_app.url_for([:edit, scope, :image, id: image_object.id, imageable_id: object.id, imageable_type: object.class.to_s, image_type: image_object.class.name]) else image_object = object.send("build_#{assoc_name}") main_app.url_for([:new, scope, :image, imageable_id: object.id, imageable_type: object.class.to_s, image_type: image_object.class.name]) end end |
#upload_multiple_image_link(object, assoc_name = :photo, scope = :admin, **options) ⇒ Object
170 171 172 173 |
# File 'app/helpers/image_helper.rb', line 170 def upload_multiple_image_link(object, assoc_name=:photo, scope=:admin, **) image_object = object.class.new.send("#{assoc_name}").build main_app.url_for([:new, scope, :image, imageable_id: object.id, imageable_type: object.class.to_s, image_type: image_object.class.name, multiple: true]) end |