This repository has been archived on 2024-03-16. You can view files and clone it, but cannot push or open issues or pull requests.
orbit-4-1/app/helpers/orbit_backend_helper.rb

296 lines
11 KiB
Ruby
Raw Normal View History

2012-04-29 15:39:28 +00:00
module OrbitBackendHelper
#========================================================================================================================
# 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
def show_form_status_field(object)
#by_object = (!object.is_expired? and object.is_pending?)
by_user = ((object.category.authed_users("fact_check_#{@module_app.key}").include?(current_user) rescue nil) or is_manager? or is_admin?)
by_user
2012-04-29 15:39:28 +00:00
end
def show_approval_link(object)
by_object = (!object.is_expired? and object.is_pending?)
by_user = ((object.category.authed_users("fact_check_#{@module_app.key}").include?(current_user) rescue nil) or is_manager? or is_admin?)
by_object and by_user
2012-04-29 15:39:28 +00:00
end
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
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
# ===============================================================
def get_value(object, field)
authorization = !@authorization || (@authorization && is_authorized(object))
approvable = !@approvable || (@approvable && is_approvable(object))
res = ''
case field[:type]
when 'associated'
res << object.send(field[:db_field]).send(field[:associated_value]) rescue ''
when 'date'
res << (object.send(field[:db_field]) ? display_date_time(object.send(field[:db_field])) : t(:no_date))
when 'field'
res << with_link?(field, object, object.send(field[:db_field]))
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)
end
when 'status'
if object.is_top?
res << content_tag(:span, t(:top), class: "label label-success") + ' '
end
if object.is_hot?
res << content_tag(:span, t(:hot), class: "label label-important") + ' '
end
if object.is_hidden?
res << content_tag(:span, t(:hidden), class: "label") + ' '
end
2013-07-10 08:42:25 +00:00
if @approvable
if object.is_pending?
res << content_tag(:span, t(:pending), class: "label") + ' '
end
if object.is_checked?
res << content_tag(:span, t(:passed), class: "label") + ' '
end
if object.is_rejected?
res << content_tag(:span, t(:rejected), class: "label") + ' '
end
end
when 'tags'
object.sorted_tags.each do |tag|
res << content_tag(:span, tag.name, class: "label label-warning")
end if object.tags
res.html_safe
when 'id'
res << field[:id_model].constantize.find(object.send(field[:db_field])).name rescue ''
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
if @sortable && field[:sort]
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
end)
end)
concat (content_tag :tbody do
(@objects.each do |object|
concat (content_tag :tr do
(@fields.each do |field|
concat(content_tag :td, get_value(object, field))
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
@fields = []
@filter_fields =[]
@filterable = false
@index_footer = nil
@quick_edit = []
@sortable = false
block.call
end
protected
# ===============================================================
# 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={})
if !args.blank? && args[:type] && args[:translation] && args[:db_field]
case args[:type]
when 'status', 'field', 'date', 'tags'
@fields << args
when 'associated'
@fields << args if args[:associated_value]
when 'id'
@fields << args if args[:id_model]
2012-04-29 15:39:28 +00:00
end
end
end
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
2012-04-29 15:39:28 +00:00
end
end
def filterable
@filterable = true
end
# ===============================================================
# 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
when 'edit'
if authorization || approvable
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)
content_tag :li, link_to(t(quick[:translation] || :delete_), '#', rel: eval("#{quick[:link]}('#{object.id}')"), class: "delete_link #{quick[:class] || 'text-error'}")
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
def is_approvable(object)
current_or_guest_user.admin? || (!object.is_rejected? && !object.is_checked?)
end
def is_authorized(object)
at_least_module_manager || object.category.cur_user_is_sub_manager_of("submit_#{@module_app.key}")
end
def quick_edit_link(args)
@quick_edit << args
end
def objects(objects)
@objects = objects
end
def sortable
@sortable = true
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
2012-04-29 15:39:28 +00:00
end