app/helpers/crud_helper.rb in templus_models-2.0.1 vs app/helpers/crud_helper.rb in templus_models-2.0.2
- old
+ new
@@ -1,13 +1,13 @@
module CrudHelper
def is_active_controller(controller_name)
- params[:controller] == controller_name ? "active" : nil
+ params[:controller] == controller_name ? "active" : nil
end
def is_active_action(action_name)
- params[:action] == action_name ? "active" : nil
+ params[:action] == action_name ? "active" : nil
end
def is_raro_crud(classe)
#if(Rails.env == "production")
# @@cruds = Dir[Rails.root.join("app","raro_crud","*.rb")].map{|f| f.split(/\//).last.gsub(/_crud\.rb/,'')} unless @@cruds
@@ -172,13 +172,13 @@
def documento?(file)
!(video?(file) || imagem?(file))
end
def render_field_file(field)
- if imagem?(field) && field.url(:thumb)
+ if imagem?(field) && field.respond_to?(:thumb)
if is_active_action("printing")
- wicked_pdf_image_tag(field.url(:thumb))
+ pdf_image_tag(field)
else
image_tag(field.url(:thumb))
end
elsif video?(field)
link_to field, field.url, target: "_blank"
@@ -194,47 +194,67 @@
def render_default_actions_crud
render "default_actions_crud"
end
#Permissions
- def should_view?(crud_helper,record)
+ def should_view?(crud_helper, record)
return false unless can?(:read, record)
return true if crud_helper.condition_view_action.nil?
crud_helper.condition_view_action.call(record)
end
- def should_edit?(crud_helper,record)
+ def should_edit?(crud_helper, record)
return false unless can?(:update, record)
return true if crud_helper.condition_edit_action.nil?
crud_helper.condition_edit_action.call(record)
end
- def should_destroy?(crud_helper,record)
+ def should_destroy?(crud_helper, record)
return false unless can?(:destroy, record)
return true if crud_helper.condition_destroy_action.nil?
crud_helper.condition_destroy_action.call(record)
end
- def should_listing?(crud_helper,model)
+ def should_listing?(crud_helper, model)
return false unless can?(:read, model)
return true if crud_helper.condition_listing_action.nil?
crud_helper.condition_listing_action.call(model)
end
- def should_listing_excel?(crud_helper,model)
+ def should_listing_excel?(crud_helper, model)
return false unless can?(:read, model)
return true if crud_helper.condition_listing_excel.nil?
crud_helper.condition_listing_excel.call(model)
end
- def should_listing_pdf?(crud_helper,model)
- return false unless can?(:read, model)
- return true if crud_helper.condition_listing_pdf.nil?
- crud_helper.condition_listing_pdf.call(model)
- end
-
- def should_printing?(crud_helper,record)
+ def can_print_pdf?(crud_helper, record)
return false unless can?(:read, record)
return true if crud_helper.condition_printing_action.nil?
crud_helper.condition_printing_action.call(record)
+ end
+
+
+ private
+
+ def pdf_image_tag(field, options = {})
+ if Rails.env.development? || Rails.env.test?
+ # Unless running a web server that can process 2 requests at the same
+ # time, trying to insert an image in a PDF creates a deadlock: the server
+ # can't finish processing the PDF request until it gets the image, but it
+ # can't start processing the image request until it has finished
+ # processing the PDF request.
+ # This will not be a problem in production, but in dev, a workaround is
+ # to base64 the image and insert its contents into the HTML
+ if field.respond_to?(:thumb)
+ image_data = File.read(field.thumb.path)
+ else
+ image_data = Rails.application.assets[field].to_s
+ end
+ image_tag("data:image/png;base64,#{::Base64.encode64(image_data)}", options)
+ else
+ if field.respond_to?(:thumb)
+ field = field.thumb.url
+ end
+ wicked_pdf_image_tag(field, options)
+ end
end
end