2012-04-29 15:39:28 +00:00
|
|
|
module OrbitBackendHelper
|
|
|
|
|
2013-07-02 08:46:44 +00:00
|
|
|
#========================================================================================================================
|
|
|
|
# from bulletin_helper.rb
|
|
|
|
#========================================================================================================================
|
|
|
|
|
|
|
|
def show_reject_reason(object)
|
|
|
|
by_object = object.is_rejected
|
|
|
|
by_user = (((object.create_user_id == current_user.id) rescue nil) or is_manager? or is_admin?)
|
|
|
|
by_object && by_user
|
2012-04-29 15:39:28 +00:00
|
|
|
end
|
|
|
|
|
2013-07-02 08:46:44 +00:00
|
|
|
def show_form_status_field(object)
|
|
|
|
#by_object = (!object.is_expired? and object.is_pending?)
|
2013-07-25 09:36:55 +00:00
|
|
|
by_user = ((object.category.authed_users("approval_#{@module_app.key}").include?(current_user) rescue nil) or is_manager? or is_admin?)
|
2013-07-02 08:46:44 +00:00
|
|
|
by_user
|
2012-04-29 15:39:28 +00:00
|
|
|
end
|
|
|
|
|
2013-07-02 08:46:44 +00:00
|
|
|
def show_approval_link(object)
|
|
|
|
by_object = (!object.is_expired? and object.is_pending?)
|
2013-07-25 09:36:55 +00:00
|
|
|
by_user = ((object.category.authed_users("approval_#{@module_app.key}").include?(current_user) rescue nil) or is_manager? or is_admin?)
|
2013-07-02 08:46:44 +00:00
|
|
|
by_object and by_user
|
2012-04-29 15:39:28 +00:00
|
|
|
end
|
|
|
|
|
2013-07-02 08:46:44 +00:00
|
|
|
def show_delete_link(object)
|
|
|
|
if !current_user.nil?
|
|
|
|
by_object = (object.create_user_id == current_user.id )
|
|
|
|
by_user = (is_manager? or is_admin?)
|
|
|
|
by_object or by_user
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
# def show_bulletin_title_at_index (bulletin)
|
|
|
|
# if bulletin.is_checked?
|
|
|
|
# link_to bulletin.title, panel_announcement_front_end_bulletin_path(bulletin, :category_id => bulletin.bulletin_category.id) rescue ''
|
|
|
|
# else
|
|
|
|
# bulletin.title
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
|
|
|
|
#========================================================================================================================
|
|
|
|
#========================================================================================================================
|
|
|
|
#========================================================================================================================
|
|
|
|
#========================================================================================================================
|
|
|
|
#========================================================================================================================
|
|
|
|
#========================================================================================================================
|
|
|
|
#========================================================================================================================
|
|
|
|
|
2012-04-29 15:39:28 +00:00
|
|
|
def is_filter_active?(type, id)
|
|
|
|
' active' if (@filter[type].include?(id.to_s) rescue nil)
|
|
|
|
end
|
|
|
|
|
2013-07-02 08:46:44 +00:00
|
|
|
def is_sort_active?(field)
|
|
|
|
res = t(field[:translation])
|
|
|
|
if params[:sort].eql?(field[:sort])
|
|
|
|
res << " " + content_tag(:b, nil, class: (params[:direction].eql?('asc') ? "icons-arrow-up-3" : "icons-arrow-down-4 "))
|
|
|
|
end
|
|
|
|
res.html_safe
|
|
|
|
end
|
|
|
|
|
|
|
|
def show_toggle_archive_btn(object)
|
|
|
|
object.disable ? t(:disable) : t(:enable)
|
|
|
|
end
|
|
|
|
|
|
|
|
def sortable_options(column)
|
|
|
|
direction = (column == params[:sort] && params[:direction] == "asc") ? "desc" : "asc"
|
|
|
|
{:sort => column, :direction => direction}
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
# ===============================================================
|
|
|
|
# associated:
|
|
|
|
#
|
|
|
|
# TODO: link for other types
|
|
|
|
# ===============================================================
|
2013-07-30 16:01:48 +00:00
|
|
|
def get_value(object, field, authorization, approvable)
|
2013-07-02 08:46:44 +00:00
|
|
|
res = ''
|
|
|
|
case field[:type]
|
|
|
|
when 'associated'
|
2013-07-25 03:32:25 +00:00
|
|
|
sorted_objects = get_sorted_objects(object.send(field[:db_field]), field[:model_field]) rescue ''
|
|
|
|
when 'id'
|
|
|
|
sorted_objects = get_sorted_objects(field[:model].find(object.send(field[:db_field])), field[:model_field]) rescue ''
|
2013-07-02 08:46:44 +00:00
|
|
|
when 'status'
|
2013-07-25 03:32:25 +00:00
|
|
|
status = []
|
2013-07-02 08:46:44 +00:00
|
|
|
if object.is_top?
|
2013-07-25 03:32:25 +00:00
|
|
|
status << content_tag(:span, t(:top), class: "label label-success")
|
2013-07-02 08:46:44 +00:00
|
|
|
end
|
|
|
|
if object.is_hot?
|
2013-07-25 03:32:25 +00:00
|
|
|
status << content_tag(:span, t(:hot), class: "label label-important")
|
2013-07-02 08:46:44 +00:00
|
|
|
end
|
|
|
|
if object.is_hidden?
|
2013-07-25 03:32:25 +00:00
|
|
|
status << content_tag(:span, t(:hidden), class: "label")
|
2013-07-02 08:46:44 +00:00
|
|
|
end
|
2013-07-10 08:42:25 +00:00
|
|
|
if @approvable
|
|
|
|
if object.is_pending?
|
2013-07-25 03:32:25 +00:00
|
|
|
status << content_tag(:span, t(:pending), class: "label")
|
2013-07-10 08:42:25 +00:00
|
|
|
end
|
|
|
|
if object.is_checked?
|
2013-07-25 03:32:25 +00:00
|
|
|
status << content_tag(:span, t(:passed), class: "label")
|
2013-07-10 08:42:25 +00:00
|
|
|
end
|
|
|
|
if object.is_rejected?
|
2013-07-25 03:32:25 +00:00
|
|
|
status << content_tag(:span, t(:rejected), class: "label")
|
2013-07-10 08:42:25 +00:00
|
|
|
end
|
2013-07-02 08:46:44 +00:00
|
|
|
end
|
2013-07-25 03:32:25 +00:00
|
|
|
res << status.join(' ')
|
2013-07-02 08:46:44 +00:00
|
|
|
when 'tags'
|
2013-07-25 03:32:25 +00:00
|
|
|
sorted_objects = get_sorted_objects(object.tags, 'name') rescue ''
|
|
|
|
else
|
|
|
|
sorted_values = object.send(field[:db_field])
|
|
|
|
sorted_values = sorted_values.sort if sorted_values.is_a?(Array)
|
|
|
|
end
|
|
|
|
res << display_value_from_object(object, sorted_values, field[:display_option]) if sorted_values
|
|
|
|
res << display_value_from_objects(sorted_objects, field[:model_field], field[:display_option]) if sorted_objects
|
|
|
|
if field[:quick_edit]
|
|
|
|
res << (content_tag :div, class: "quick-edit" do
|
|
|
|
content_tag :ul, class: "nav nav-pills" do
|
|
|
|
@quick_edit.each do |quick|
|
|
|
|
concat get_quick_link(quick, object, authorization, approvable)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end)
|
2013-07-02 08:46:44 +00:00
|
|
|
end
|
|
|
|
res.html_safe
|
|
|
|
end
|
|
|
|
|
|
|
|
def index_table
|
|
|
|
content_tag :table, class: "table main-list" do
|
|
|
|
concat (content_tag :thead do
|
|
|
|
(content_tag :tr, class: "sort-header" do
|
|
|
|
@fields.each do |field|
|
|
|
|
concat (content_tag :th, "data-hide" => field[:hide], class: "#{field[:header_class]} #{'active' if params[:sort].eql?(field[:sort])}" do
|
2013-07-25 03:32:25 +00:00
|
|
|
if field[:sort]
|
2013-07-02 08:46:44 +00:00
|
|
|
link_to is_sort_active?(field), url_for({:filter => @filter}.merge(sortable_options(field[:sort]).merge(:sort_options => field[:db_field])))
|
|
|
|
else
|
|
|
|
t(field[:translation])
|
|
|
|
end
|
|
|
|
end)
|
2012-04-29 15:39:28 +00:00
|
|
|
end
|
2013-07-02 08:46:44 +00:00
|
|
|
end)
|
|
|
|
end)
|
|
|
|
concat (content_tag :tbody do
|
|
|
|
(@objects.each do |object|
|
2013-07-30 16:01:48 +00:00
|
|
|
authorization = !@authorization || (@authorization && is_authorized(object))
|
|
|
|
approvable = !@approvable || (@approvable && is_approvable(object))
|
2013-07-02 08:46:44 +00:00
|
|
|
concat (content_tag :tr do
|
|
|
|
(@fields.each do |field|
|
2013-07-30 16:01:48 +00:00
|
|
|
concat(content_tag :td, get_value(object, field, authorization, approvable))
|
2013-07-02 08:46:44 +00:00
|
|
|
end)
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_default_index(&block)
|
2013-07-10 08:42:25 +00:00
|
|
|
@approvable = @module_app.is_approvable
|
|
|
|
@authorization = @module_app.is_authorizable
|
2013-07-02 08:46:44 +00:00
|
|
|
@fields = []
|
|
|
|
@filter_fields =[]
|
|
|
|
@filterable = false
|
|
|
|
@index_footer = nil
|
|
|
|
@quick_edit = []
|
|
|
|
block.call
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
2013-07-25 03:32:25 +00:00
|
|
|
def display_value(value)
|
|
|
|
case value.class.name
|
|
|
|
when 'DateTime'
|
|
|
|
display_date_time(value)
|
|
|
|
when 'Date'
|
|
|
|
display_date(value)
|
|
|
|
when 'Time'
|
|
|
|
display_time(value)
|
|
|
|
when /Uploader/
|
|
|
|
image_tag(value.url)
|
|
|
|
else
|
|
|
|
value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def display_value_from_object(object, value, display_option)
|
|
|
|
if display_option
|
|
|
|
eval(display_option)
|
|
|
|
else
|
|
|
|
display_value(value)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def display_value_from_objects(objects, field, display_option)
|
|
|
|
if display_option
|
|
|
|
objects = Array(objects).map do |object|
|
|
|
|
eval(display_option)
|
|
|
|
end
|
|
|
|
objects.join(' ')
|
|
|
|
else
|
|
|
|
objects = Array(objects).map do |object|
|
|
|
|
display_value(object.send(field))
|
|
|
|
end
|
|
|
|
objects.join(', ')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-02 08:46:44 +00:00
|
|
|
# ===============================================================
|
|
|
|
# type:
|
|
|
|
# check "get_value"
|
|
|
|
# associated_value:
|
|
|
|
# check "get_value"
|
|
|
|
# db_field:
|
|
|
|
# name of the field(s) in the database
|
|
|
|
# translation:
|
|
|
|
# key in the yml translation files
|
|
|
|
# hide:
|
|
|
|
# - no value: always shown
|
|
|
|
# - all: alway hidden
|
|
|
|
# - phone: hidden for phone
|
|
|
|
# sort:
|
|
|
|
# unique value used for keeping the sorted column
|
|
|
|
# link:
|
|
|
|
# if provided, the values will be shown as link
|
|
|
|
# (only available for type 'field' for now)
|
|
|
|
# ===============================================================
|
|
|
|
def field(args={})
|
2013-07-25 03:32:25 +00:00
|
|
|
if !args.blank? && args[:translation] && args[:db_field]
|
2013-07-02 08:46:44 +00:00
|
|
|
case args[:type]
|
|
|
|
when 'associated'
|
2013-07-25 03:32:25 +00:00
|
|
|
@fields << args if args[:model_field] || args[:display_option]
|
2013-07-02 08:46:44 +00:00
|
|
|
when 'id'
|
2013-07-25 03:32:25 +00:00
|
|
|
@fields << args if args[:model] && (args[:model_field] || args[:display_option])
|
|
|
|
when 'image'
|
|
|
|
args[:header_class] ||= 'span2'
|
|
|
|
@fields << args
|
|
|
|
else
|
|
|
|
@fields << args
|
2012-04-29 15:39:28 +00:00
|
|
|
end
|
2013-07-25 03:32:25 +00:00
|
|
|
elsif !args.blank? && args[:type].eql?('tags')
|
|
|
|
args[:translation] ||= 'tags'
|
|
|
|
args[:display_option] ||= "content_tag(:span, object.name, class: 'label label-warning')"
|
|
|
|
@fields << args
|
2012-04-29 15:39:28 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-02 08:46:44 +00:00
|
|
|
def filter_field(args={})
|
|
|
|
if !args.blank? && args[:type] && args[:values] && args[:translation]
|
|
|
|
case args[:type]
|
|
|
|
when 'array'
|
|
|
|
@filter_fields << args
|
|
|
|
when 'objects'
|
|
|
|
@filter_fields << args if args[:object_field]
|
2012-09-13 18:38:56 +00:00
|
|
|
end
|
2013-07-25 03:32:25 +00:00
|
|
|
@filterable ||= true
|
2012-04-29 15:39:28 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-02 08:46:44 +00:00
|
|
|
# ===============================================================
|
|
|
|
# args:
|
|
|
|
# - paginate: default true, there'es no need to specify it
|
|
|
|
# - link: path to the new action
|
|
|
|
# TODO: links to other actions
|
|
|
|
# ===============================================================
|
|
|
|
def footer(args={})
|
|
|
|
paginate = args.has_key?(:paginate) ? args[:paginate] : true
|
|
|
|
link = (is_manager? || is_sub_manager? rescue nil) && args.has_key?(:link) ? true : false
|
|
|
|
if paginate || link
|
|
|
|
@index_footer = content_tag :div, class: "bottomnav clearfix" do
|
|
|
|
concat content_tag :div, link_to(content_tag(:i, nil, :class => 'icon-plus') + ' ' + t(:add), send(args[:link]), :class => 'btn btn-primary' ), class: "action pull-right" if link
|
|
|
|
concat content_tag :div, paginate(@objects, :params => {:direction => params[:direction], :sort => params[:sort], :filter => @filter, :new_filter => nil, :sort_options => params[:sort_options]}), class: "pagination pagination-centered" if paginate
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_quick_link(quick, object, authorization, approvable)
|
|
|
|
case quick[:type]
|
|
|
|
when 'approval'
|
|
|
|
if show_approval_link(object)
|
|
|
|
content_tag :li, link_to(t(quick[:translation] || :approval_), eval("#{quick[:link]}('#{object.id}')"), class: "preview_trigger #{quick[:class]}")
|
|
|
|
end
|
2013-07-25 09:36:55 +00:00
|
|
|
when 'authorization'
|
2013-07-30 16:01:48 +00:00
|
|
|
if is_admin? || is_manager?
|
2013-07-25 09:36:55 +00:00
|
|
|
content_tag :li, link_to(t(quick[:translation] || :authorization_), eval("#{quick[:link]}"), class: "preview_trigger #{quick[:class]}")
|
|
|
|
end
|
2013-07-02 08:46:44 +00:00
|
|
|
when 'edit'
|
2013-07-30 16:01:48 +00:00
|
|
|
if authorization && approvable
|
2013-07-02 08:46:44 +00:00
|
|
|
content_tag :li, link_to(t(quick[:translation] || :edit), eval("#{quick[:link]}('#{object.id}')"), class: quick[:class])
|
|
|
|
end
|
|
|
|
when 'delete'
|
|
|
|
if show_delete_link(object)
|
2013-07-29 06:29:37 +00:00
|
|
|
@delete_options = {title: quick[:title], warning: quick[:warning], cancel: quick[:cancel], submit: quick[:submit]}
|
|
|
|
content_tag :li, link_to(t(quick[:translation] || :delete_), '#', rel: eval("#{quick[:link]}('#{object.id}')"), class: "delete #{quick[:class] || 'text-error'}")
|
2013-07-02 08:46:44 +00:00
|
|
|
end
|
|
|
|
when 'detail'
|
|
|
|
content_tag :li, link_to(t(quick[:translation] || :detail), '#', class: (quick[:class] || "detail-row"))
|
|
|
|
when 'reject_reason'
|
|
|
|
if show_reject_reason(object)
|
|
|
|
content_tag :li, link_to(t(quick[:translation] || :rejected_reason) + ' ' + truncate(object.not_checked_reason, :length => 10), '#', rel: "tooltip", 'data-original-title' => (wrap_string_with(object.not_checked_reason, :line_width => 24)), class: "reject_info #{quick[:class]}")
|
|
|
|
end
|
|
|
|
else
|
|
|
|
content_tag :li, link_to(t(quick[:translation]), eval("#{quick[:link]}('#{object.id}')"), class: quick[:class])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-25 03:32:25 +00:00
|
|
|
def get_sorted_objects(objects, field)
|
|
|
|
if field
|
|
|
|
objects_array = Array(objects).inject([]){ |result, value|
|
|
|
|
result << [value.send(field), value]
|
|
|
|
}
|
|
|
|
objects_array.sort.map{|d| d[1]}
|
|
|
|
else
|
|
|
|
objects
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-02 08:46:44 +00:00
|
|
|
def is_approvable(object)
|
|
|
|
current_or_guest_user.admin? || (!object.is_rejected? && !object.is_checked?)
|
|
|
|
end
|
|
|
|
|
|
|
|
def is_authorized(object)
|
2013-07-30 16:01:48 +00:00
|
|
|
autorized = @module_app.authorizable_models.inject(false) do |autorized, klass|
|
|
|
|
if object.is_a?(klass.constantize)
|
2013-08-19 10:54:35 +00:00
|
|
|
autorized ||= object.user_can_sub_manage?(current_user)
|
2013-07-30 16:01:48 +00:00
|
|
|
else
|
2013-08-19 10:54:35 +00:00
|
|
|
autorized ||= object.category.user_can_sub_manage?(current_user)
|
2013-07-30 16:01:48 +00:00
|
|
|
end
|
|
|
|
autorized
|
|
|
|
end
|
|
|
|
at_least_module_manager || autorized
|
2013-07-02 08:46:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def quick_edit_link(args)
|
|
|
|
@quick_edit << args
|
|
|
|
end
|
|
|
|
|
|
|
|
def objects(objects)
|
|
|
|
@objects = objects
|
|
|
|
end
|
|
|
|
|
|
|
|
def with_link?(field, object, value)
|
|
|
|
if field[:link] && @approvable
|
|
|
|
if object.class.instance_methods.include?(:is_checked?) && object.is_checked?
|
|
|
|
link_to(value, eval("#{field[:link]}('#{object.id}')"))
|
|
|
|
# link_to bulletin.title, panel_announcement_front_end_bulletin_path(bulletin, :category_id => bulletin.bulletin_category.id) rescue ''
|
|
|
|
else
|
|
|
|
value
|
|
|
|
end
|
|
|
|
elsif field[:link]
|
|
|
|
link_to(value, eval("#{field[:link]}('#{object.id}')"))
|
|
|
|
else
|
|
|
|
value
|
|
|
|
end
|
2012-07-05 08:00:45 +00:00
|
|
|
end
|
|
|
|
|
2013-07-29 09:34:03 +00:00
|
|
|
# ===============================================================
|
|
|
|
# Breadcrumbs
|
|
|
|
# ===============================================================
|
|
|
|
|
|
|
|
def back_end_breadcrumb
|
|
|
|
res = ''
|
|
|
|
divider = "<span class='divider'>/</span>"
|
|
|
|
res << "<li><a href='#{admin_dashboards_path}'>#{t(:dashboard_)}</a>#{divider}</li>"
|
|
|
|
case controller.controller_name
|
|
|
|
when 'authorizations'
|
|
|
|
res << "<li><a href='/#{params[:controller]}/#{@module_app.key}'>#{@module_app.title}</a>#{divider}</li>"
|
|
|
|
case params[:type]
|
|
|
|
when 'approval'
|
|
|
|
res << "<li class='active'>#{t(:approval_)}</li>"
|
|
|
|
when 'category'
|
|
|
|
res << "<li class='active'>#{t(:category_auth)}</li>"
|
|
|
|
when nil
|
|
|
|
res << "<li class='active'>#{t(:module_authorization)}</li>"
|
|
|
|
else
|
|
|
|
res << "<li class='active'>#{params[:type].underscore.humanize.capitalize} #{t(:authorization_)}</li>"
|
|
|
|
end
|
|
|
|
when 'categories'
|
|
|
|
res << "<li>#{@module_app.title}#{divider}</li>"
|
|
|
|
res << "<li class='active'>#{t(:categories)}</li>"
|
|
|
|
when 'module_tags'
|
|
|
|
res << "<li>#{@module_app.title}#{divider}</li>"
|
|
|
|
res << "<li class='active'>#{t(:tags)}</li>"
|
|
|
|
else
|
|
|
|
if params[:action].eql?('index')
|
|
|
|
res << "<li>#{@module_app.title}</li>"
|
|
|
|
else
|
|
|
|
res << "<li><a href='/#{params[:controller]}'>#{@module_app.title}</a>#{divider}</li>"
|
|
|
|
res << "<li class='active'>#{t(params[:action], scope: 'restful_actions')}</li>"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
res.html_safe
|
|
|
|
end
|
|
|
|
|
2012-04-29 15:39:28 +00:00
|
|
|
end
|