PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/app/helpers/skyline/presenters/presenter.rb

http://github.com/DigitPaint/skyline
Ruby | 122 lines | 92 code | 19 blank | 11 comment | 16 complexity | 91593ae6fa0bdb103056f72bc6d31ce9 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-3.0
  1. # Use options[:collection] = collection_name to specify this list is sub-colleciton of
  2. # a parent object.
  3. class Skyline::Presenters::Presenter
  4. class << self
  5. def create(presenter,records,fieldset,template,options={})
  6. Skyline::Presenters::Table.new(records,fieldset,template,options)
  7. end
  8. end
  9. attr_accessor :collection,:options, :fieldset
  10. def initialize(collection, fieldset,template, options={})
  11. @collection = collection
  12. @fieldset = fieldset
  13. @template = template
  14. options.reverse_merge!(:navigation => true)
  15. @options = options
  16. # Make sure we initialize the stack
  17. self.stack
  18. end
  19. # Delegate all missing methods (helpers) to the template
  20. def method_missing(method,*params)
  21. @template.send(method,*params)
  22. end
  23. # Speedup
  24. def content_tag(*params); @template.content_tag(*params); end # :nodoc:
  25. def output
  26. @template.render :partial => "skyline/content/presenters/table", :locals => {:presenter => self}
  27. end
  28. def edit_button(record)
  29. 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"
  30. end
  31. def delete_button(record)
  32. link_to(
  33. button_text(:delete),
  34. {:action => "delete",:types => stack.url_types(:down => [record.id]),:return_to => url_for({:types => params[:types]})},
  35. :remote => true,
  36. :confirm => t(:confirm_deletion, :scope => [:content,:list], :class => self.fieldset.singular_name),
  37. :class => "button small red")
  38. end
  39. # We have to create our own stack, since it may be possible that we're
  40. # inline of some other screen. We have to add the collection we're listing to this stack
  41. # this may mean we don't have to do anything at all because the main stack is aleady the right one
  42. # Navigation within inlin lists is prohibited, because we do not support sub-list navigation on an inline list yet.
  43. def stack
  44. return @stack if @stack
  45. @stack = @template.stack.dup
  46. if self.options[:collection] && !(@stack.collection_name == self.options[:collection])
  47. @stack.push(self.options[:collection])
  48. self.options[:navigation] = false
  49. end
  50. @stack
  51. end
  52. def heading_collection(fieldset=self.fieldset,cache=true)
  53. return @heading_collection if @heading_collection && cache
  54. @heading_collection = []
  55. fieldset.each_field do |field|
  56. next if field.hidden_in :list
  57. case field
  58. when Skyline::Content::MetaData::Field
  59. @heading_collection << field
  60. when Skyline::Content::MetaData::FieldGroup
  61. @heading_collection += heading_collection(field,false)
  62. end
  63. end
  64. # Publishable
  65. @heading_collection << Skyline::Content::MetaData::Field.new(:name => :published, :owner => fieldset, :editor => :publish) if fieldset.kind_of?(Class) && fieldset.publishable?
  66. @heading_collection
  67. end
  68. def value(record,field)
  69. if field.association?
  70. case field.reflection.macro
  71. when :has_many
  72. link_to "#{record.send(field.reflection.name).count.to_s} items (view)", :action => "list", :types => stack.url_types(:down => [record.id,field.name])
  73. when :belongs_to
  74. sub_record = record.send(field.reflection.name)
  75. sub_record && sub_record.human_id || ""
  76. end
  77. else
  78. begin
  79. content = field.value(record)
  80. end
  81. case field.editor
  82. # :publish editor is temporary editor created by the presenter itself.
  83. when :publish
  84. [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"}]
  85. else normalize_content(content,field)
  86. end
  87. end
  88. end
  89. def normalize_content(content,field=nil)
  90. case content
  91. when /<.+?>/ then
  92. if field.filter_html == false
  93. content.to_s
  94. else
  95. truncate(simple_format(strip_tags(content.gsub("<br />", "<br />\n").gsub("</p>", "</p>\n"))), :length => 150)
  96. end
  97. when String then truncate(content,:length => 150)
  98. 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"}]
  99. when Date,Time then l(content, :format => :long)
  100. else content.to_s
  101. end
  102. end
  103. end