# coding: UTF-8
module Edgarj
module ListDrawer
# Base for popup-list and normal-list column drawer
#
# Sub class of Drawer to draw list
class Base
include ERB::Util
# * options
# * namespace - namespace for path (ex: :admin)
#
# TODO: enum_cache は止め、グローバルに1つのキャッシュを作る。
def initialize(drawer, options = {})
@drawer = drawer
@options = options.dup
@vc = drawer.vc
@enum_cache = {}
@bitset_cache = {}
@parent_rec = nil
@belongs_to_link = false # doesn't make link on belongs_to
end
def draw_column(rec, col)
@parent_rec = rec.belongs_to_AR(col)
@vc.content_tag(:td, td_options(rec, col)) do
# edgarj_address column is prior to draw_belongs_to() because of
# avoiding link_to process on the 'belongs_to' column.
=begin
if rec.class.edgarj_address?(col)
col_name = rec.class.get_column_name(col)
adrs = Edgarj::Address.find_by_id(rec.send(col_name))
adrs ? draw_trimmed_str(adrs.name) : ''
=end
if @parent_rec then
if @belongs_to_link
@vc.link_to(@parent_rec.name, @drawer.popup_path(col), remote: true)
else
h(@parent_rec.name)
end
else
draw_normal_column(rec, col)
end
end
end
private
#
options
def td_options(rec, col)
if @enum_cache[col.name]
{}
elsif @bitset_cache[col.name]
{}
elsif @parent_rec
{}
else
if @vc.get_enum(rec.class, col)
{}
# elsif @vc.get_bitset(rec.class, col)
# {}
else
case col.type
when :datetime
{}
when :date
{}
when :integer
{align: 'right'}
when :boolean
{align: 'center'}
else
{}
end
end
end
end
# trim string when too long
def draw_trimmed_str(str)
s = str.split(//)
if s.size > Edgarj::LIST_TEXT_MAX_LEN
s = s[0..Edgarj::LIST_TEXT_MAX_LEN] << '...'
end
s.join('')
end
# draw rec.col other than 'belongs_to'
# 1. DateTime format is YYYY-MM-DD hh:mm:ss.
# 1. if col.name == 'flags', it is assumed bitset.
# 1. if col is edgarj_file, it is assumed file.
#
# === INPUTS
# rec:: AR instance
# col:: ActiveRecord::ConnectionAdapters::Column type which rec.class.columns returns
def draw_normal_column(rec, col)
=begin
if rec.class.edgarj_file?(col)
file_info = FileInfo.safe_find(rec.send(col.name))
file_info ? file_info.filename : ''
elsif (enum = @enum_cache[col.name])
@vc.draw_column_enum(rec, col, enum)
elsif (bitset = @bitset_cache[col.name])
@vc.draw_column_bitset(rec, col, bitset)
else
=end
if (enum = @vc.get_enum(rec.class, col))
@enum_cache[col.name] = enum
@vc.draw_column_enum(rec, col, enum)
=begin
elsif (bitset = @vc.get_bitset(rec.class, col))
@bitset_cache[col.name] = bitset
@vc.draw_column_bitset(rec, col, bitset)
=end
else
case col.type
when :datetime
@vc.datetime_fmt(rec.send(col.name))
when :date
@vc.date_fmt(rec.send(col.name))
when :integer
h(rec.send(col.name))
when :boolean
rec.send(col.name) ? '√' : ''
else
# NOTE: rec.send(col.name) is not used since sssn.send(:data)
# becomes hash rather than actual string-data so that following
# split() fails for Hash.
if str = rec.attributes[col.name]
draw_trimmed_str(str)
else
''
end
end
end
#end
end
end
# Drawer for record list in Edgarj CRUD view.
#
class Normal < Base
def initialize(edgarj_drawer, options = {})
super(edgarj_drawer, options)
#@belongs_to_link = true # make link on belongs_to
end
# prepare path for the rec, which will be drawn later.
def set_path(rec)
@path = @vc.polymorphic_path([@options[:namespace], rec], format: :js)
end
# | options
#
# add Edgarj.click_listCB() with base result.
# When the column is parent, do nothing.
def td_options(rec, col)
result = super.merge(
style: 'cursor:pointer;',
class: '_edgarj_list_column')
#if !@parent_rec
result[:'data-url'] = @path
#end
result
end
end
end
end
|