--- /dev/null
+
+module Pettanr
+ class Manifest
+ class ModelConf
+ def initialize manifest
+ @conf = conf || {}
+ end
+
+ def type
+ @conf['type'] || 'default'
+ end
+
+ def name
+ @conf['name'] || 'edit_template'
+ end
+
+ end
+
+ def initialize manifest
+ @manifest = manifest || {}
+ end
+
+ def models
+ end
+
+ def profiles
+ end
+
+ end
+
+ manifest = Manifest.new JSON.parse(
+ open(Rails.root + 'public/manifest.json').read
+ )
+
+ module Cross
+ class ModelConf
+ def initialize manifest
+ @conf = conf || {}
+ end
+
+ def type
+ @conf['type'] || 'default'
+ end
+
+ def name
+ @conf['name'] || 'edit_template'
+ end
+
+ end
+
+ class Profiler
+ class Item
+ def initialize item, attribute_name, attribute_conf
+ @attribute_conf = attribute_conf || {}
+ end
+
+ def label
+ label = item.class.human_attribute_name(field_name)
+ end
+
+ def value
+ case field_conf['type']
+ when 'datetime'
+ if item.attributes[field_name]
+ l item.attributes[field_name]
+ else
+ item.attributes[field_name]
+ end
+ else
+ item.attributes[field_name]
+ end
+ end
+
+ def note
+ if field_conf['source']
+ case field_conf['source']['type']
+ when 'magic_number'
+ t_selected_item(field_conf['source']['key'], item.attributes[field_name])
+ when 'model'
+ else
+ end
+ end
+ end
+
+ end
+
+ def initialize item, roles, cross
+ item_name = item.class.to_s.underscore
+ profiler_conf = ProfilerConf.new cross['profiles'][item_name]
+ edit_conf = EditConf.new conf['edit']
+ end
+
+ def header
+ ender 'system/filer.html', :items => [item], :model => item.class, :roles => roles, :pager => nil
+ end
+
+ def each_column
+ profiler_conf['fields'].each do |field_name|
+ yield label, value, note
+ end
+ end
+
+ def each_belongs_to
+ profiler_conf['fields'].each do |field_name|
+ yield label, value, note
+ end
+ end
+
+ def each_has_many
+ profiler_conf['fields'].each do |field_name|
+ yield label, value, note
+ end
+ end
+
+ def each_has_one
+ profiler_conf['fields'].each do |field_name|
+ yield label, value, note
+ end
+ end
+
+ end
+
+ class Filer
+ class EditConf
+ def initialize conf
+ @conf = conf || {}
+ end
+
+ def type
+ @conf['type'] || 'default'
+ end
+
+ def name
+ @conf['name'] || 'edit_template'
+ end
+
+ end
+
+ class Symbol
+ def initialize symbol_conf
+ @symbol_conf = symbol_conf || {}
+ @link_conf = @symbol_conf['link'] || {'type' => 'action', 'name' => 'show'}
+ @type_conf = @symbol_conf['type'] || 'image'
+ @link_method = self.link_method
+ @tag_method = self.tag_method
+ end
+
+ def link_method
+ case @link_conf['type']
+ when 'action'
+ case @link_conf['name']
+ when 'show'
+ :link_action_show
+ else
+ :link_action_else
+ end
+ when 'none'
+ :link_none
+ when 'url_column'
+ :link_url_column
+ else
+ :link_else
+ end
+ end
+
+ def tag_method
+ tag = case @type_conf
+ when 'image'
+ :tag_image
+ when 'picture'
+ :tag_picture
+ when 'template'
+ :tag_template
+ else
+ :tag_else
+ end
+ end
+
+ def link_action_show view, item
+ view.polymorphic_path(item)
+ end
+
+ def link_action_else view, item
+ view.polymorphic_path(item, :action => @link_conf['name'])
+ end
+
+ def link_none view, item
+ ''
+ end
+
+ def link_url_column view, item
+ item.__send__(@link_conf['name']).to_s
+ end
+
+ def link_else view, item
+ ''
+ end
+
+ def tag_image view, item
+ view.tag(:img, :src => @symbol_conf['name'], :width => 64, :height => 64)
+ end
+
+ def tag_picture view, item
+ view.tag(:img, item.__send__(@symbol_conf['name']))
+ end
+
+ def tag_template view, item
+ view.render item.path_name + '/' + @symbol_conf['name'], :item => item
+ nil
+ end
+
+ def tag_else view, item
+ 'no.png'
+ end
+
+ def render view, item
+ link = self.__send__(@link_method, view, item)
+ tag = self.__send__(@tag_method, view, item)
+ unless tag.blank?
+ link_to_unless link.blank?, tag, link
+ end
+ end
+
+
+ end
+
+ class Caption
+ def initialize caption_conf
+ @caption_conf = caption_conf || {}
+ @link_conf = @symbol_conf['link'] || {'type' => 'action', 'name' => 'show'}
+ @type_conf = @symbol_conf['type'] || 'column'
+ @link_method = self.link_method
+ @tag_method = self.tag_method
+ end
+
+ def link_method
+ case @link_conf['type']
+ when 'action'
+ case @link_conf['name']
+ when 'show'
+ :link_action_show
+ else
+ :link_action_else
+ end
+ when 'none'
+ :link_none
+ when 'url_column'
+ :link_url_column
+ else
+ :link_else
+ end
+ end
+
+ def tag_method
+ tag = case @type_conf
+ when 'column'
+ :tag_column
+ when 'method'
+ :tag_method
+ else
+ :tag_else
+ end
+ end
+
+ def link_action_show view, item
+ view.polymorphic_path(item)
+ end
+
+ def link_action_else view, item
+ view.polymorphic_path(item, :action => @link_conf['name'])
+ end
+
+ def link_none view, item
+ ''
+ end
+
+ def link_url_column view, item
+ item.__send__(@link_conf['name']).to_s
+ end
+
+ def link_else view, item
+ ''
+ end
+
+ def tag_column view, item
+ item.attributes[caption['name']]
+ end
+
+ def tag_method view, item
+ item.__send__(caption['name']).to_s
+ end
+
+ def tag_else view, item
+ 'no caption'
+ end
+
+ def render view, item
+ unless caption_type == 'none'
+ link = self.__send__(@link_method, view, item)
+ tag = self.__send__(@tag_method, view, item)
+ tag = 'no caption' if tag.blank?
+ unless tag.blank?
+ link_to_unless link.blank?, tag, link
+ end
+ end
+ end
+
+ end
+
+ class Summary
+ def initialize summary_conf
+ @summary_conf = summary_conf || {}
+ @type_conf = @summary_conf['type'] || 'none'
+ @type_method = self.type_method
+ end
+
+ def type_method
+ case @type_conf
+ when 'none'
+ :type_none
+ when 'template'
+ :type_template
+ else
+ :type_none
+ end
+ end
+
+ def type_none view, item
+ ''
+ end
+
+ def type_template view, item
+ view.render item.path_name + '/' + @summary_conf['name'], :item => item
+ end
+
+ def render view, item
+ self.__send__(@type_method, view, item)
+ end
+
+ end
+
+ class Icon
+ def initialize icon_conf
+ @icon_conf = icon_conf || {}
+ end
+
+ def render view, item
+ view.link_to view.icon_tag(item.class.to_s, :object => item, :size => 64),
+ view.polymorphic_path(item, :format => :prof)
+ end
+
+ end
+
+ class Date
+ def initialize date_conf
+ @date_conf = date_conf || {}
+
+ def render view, item
+ view.distance_of_time_in_words_to_now item.updated_at
+ end
+
+ end
+
+ class Edit
+ def initialize edit_conf
+ @edit_conf = edit_conf || {}
+ @type_conf = @edit_conf['type'] || 'default'
+ @type_method = self.type_method
+ end
+
+ def type_method
+ case @type_conf
+ when 'default'
+ :type_default
+ when 'account'
+ :type_account
+ when 'template'
+ :type_template
+ when 'none'
+ :type_none
+ else
+ :type_none
+ end
+ end
+
+ def type_default view, item
+ if item.own? roles
+ view.link_to tag(:img, :src => view.asset_path('edit.png'), :width => 32, :height => 32),
+ view.polymorphic_path(item, :action => :edit)
+ view.link_to tag(:img, :src => view.asset_path('remove.png'), :width => 32, :height => 32),
+ item, confirm: 'Are you sure?', :method => :delete
+ end
+ end
+
+ def type_account view, item
+ if item.own? roles
+ view.link_to tag(:img, :src => view.asset_path('edit.png'), :width => 32, :height => 32),
+ '/home/configure'
+ end
+ end
+
+ def type_template view, item
+ view.render item.path_name + '/' + @edit_conf['name'], :item => item
+ end
+
+ def type_none view, item
+ ''
+ end
+
+ def render view, item
+ self.__send__(@type_method, view, item)
+ end
+
+ end
+
+ class FileItem
+ def initialize item, symbol, caption, summary, icon, date, edit
+ edit_conf = EditConf.new conf['edit']
+ @item = item
+ @symbol = symbol
+ @caption = caption
+ @summary = summary
+ @icon = icon
+ @date = date
+ @edit = edit
+ end
+
+ def symbol view
+ @symbol.render view, item
+ end
+
+ def caption view
+ @caption.render view, item
+ end
+
+ def summary view
+ @summary.render view, item
+ end
+
+ def icon view
+ @icon.render view, item
+ end
+
+ def date view
+ @date.render view, item
+ end
+
+ def edit view
+ @edit.render view, item
+ end
+
+ end
+
+ def initialize model, cross
+ @model = model
+ @filer_conf = cross['filers'][@model.item_name] || {}
+ @symbol = Symbol.new @filer_conf['symbol']
+ @caption = Caption.new @filer_conf['caption']
+ @summary = Summary.new @filer_conf['summary']
+ @icon = Icon.new @filer_conf['icon']
+ @date = Date.new @filer_conf['date']
+ @edit = Edit.new @filer_conf['edit']
+ end
+
+ def list page, page_size
+ @page = @model.page_number params[:page]
+ @page_size = @model.page_size params[:page_size]
+ list_method = 'list' || configurations['models'][@model.to_s.underscore]['select']['method']
+ @items = @model.__send__(list_method, @page, @page_size).map {|item|
+ FileItem.new item, @symbol, @caption, @summary, @icon, @date, @edit
+ }
+ end
+
+ def symbol_action_show item, view
+ view.polymorphic_path(item)
+ end
+
+ def symbol_action_name item, view
+ view.polymorphic_path(item, :action => link['name'])
+ end
+
+ end
+ end
+end
+