/app/helpers/skyline/presenters/presenter.rb
Ruby | 122 lines | 92 code | 19 blank | 11 comment | 16 complexity | 91593ae6fa0bdb103056f72bc6d31ce9 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-3.0
- # Use options[:collection] = collection_name to specify this list is sub-colleciton of
- # a parent object.
- class Skyline::Presenters::Presenter
- class << self
- def create(presenter,records,fieldset,template,options={})
- Skyline::Presenters::Table.new(records,fieldset,template,options)
- end
- end
-
- attr_accessor :collection,:options, :fieldset
- def initialize(collection, fieldset,template, options={})
- @collection = collection
- @fieldset = fieldset
- @template = template
- options.reverse_merge!(:navigation => true)
- @options = options
- # Make sure we initialize the stack
- self.stack
- end
-
- # Delegate all missing methods (helpers) to the template
- def method_missing(method,*params)
- @template.send(method,*params)
- end
- # Speedup
- def content_tag(*params); @template.content_tag(*params); end # :nodoc:
-
- def output
- @template.render :partial => "skyline/content/presenters/table", :locals => {:presenter => self}
- end
-
-
- def edit_button(record)
- link_to button_text(:edit),{:action => "edit", :types => stack.url_types(:down => [record.id]), :return_to => url_for({:types => params[:types], :filter => params[:filter]})}, :class => "button small"
- end
-
- def delete_button(record)
- link_to(
- button_text(:delete),
- {:action => "delete",:types => stack.url_types(:down => [record.id]),:return_to => url_for({:types => params[:types]})},
- :remote => true,
- :confirm => t(:confirm_deletion, :scope => [:content,:list], :class => self.fieldset.singular_name),
- :class => "button small red")
- end
-
- # We have to create our own stack, since it may be possible that we're
- # inline of some other screen. We have to add the collection we're listing to this stack
- # this may mean we don't have to do anything at all because the main stack is aleady the right one
- # Navigation within inlin lists is prohibited, because we do not support sub-list navigation on an inline list yet.
- def stack
- return @stack if @stack
- @stack = @template.stack.dup
-
- if self.options[:collection] && !(@stack.collection_name == self.options[:collection])
- @stack.push(self.options[:collection])
- self.options[:navigation] = false
- end
- @stack
- end
- def heading_collection(fieldset=self.fieldset,cache=true)
- return @heading_collection if @heading_collection && cache
-
- @heading_collection = []
- fieldset.each_field do |field|
- next if field.hidden_in :list
- case field
- when Skyline::Content::MetaData::Field
- @heading_collection << field
- when Skyline::Content::MetaData::FieldGroup
- @heading_collection += heading_collection(field,false)
- end
- end
- # Publishable
- @heading_collection << Skyline::Content::MetaData::Field.new(:name => :published, :owner => fieldset, :editor => :publish) if fieldset.kind_of?(Class) && fieldset.publishable?
- @heading_collection
- end
- def value(record,field)
- if field.association?
- case field.reflection.macro
- when :has_many
- link_to "#{record.send(field.reflection.name).count.to_s} items (view)", :action => "list", :types => stack.url_types(:down => [record.id,field.name])
- when :belongs_to
- sub_record = record.send(field.reflection.name)
- sub_record && sub_record.human_id || ""
- end
- else
- begin
- content = field.value(record)
- end
-
- case field.editor
- # :publish editor is temporary editor created by the presenter itself.
- when :publish
- [content ? image_tag("#{Skyline::Configuration.url_prefix}/images/icons/true.gif", :alt => t(:true, :scope => [:icons])) : image_tag("#{Skyline::Configuration.url_prefix}/images/icons/false.gif", :alt => t(:true, :scope => [:icons])),{:class => "center"}]
- else normalize_content(content,field)
- end
- end
- end
-
- def normalize_content(content,field=nil)
- case content
- when /<.+?>/ then
- if field.filter_html == false
- content.to_s
- else
- truncate(simple_format(strip_tags(content.gsub("<br />", "<br />\n").gsub("</p>", "</p>\n"))), :length => 150)
- end
- when String then truncate(content,:length => 150)
- when TrueClass,FalseClass then [(content ? image_tag("#{Skyline::Configuration.url_prefix}/images/icons/true.gif") : image_tag("#{Skyline::Configuration.url_prefix}/images/icons/false.gif")),{:class => "center"}]
- when Date,Time then l(content, :format => :long)
- else content.to_s
- end
- end
-
- end