Class: Orange::NewsResource

Inherits:
ModelResource
  • Object
show all
Defined in:
lib/orange-more/news/resources/news_resource.rb

Instance Method Summary collapse

Instance Method Details

#archive(packet, opts = {}) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/orange-more/news/resources/news_resource.rb', line 56

def archive(packet, opts = {})
  opts.merge!(packet.extract_opts)
  opts.merge!( :archive_url => archive_url(packet))
  opts[:page] = opts[:page].blank? ? 1 : opts[:page].to_i
  page = opts[:page].blank? ? 0 : opts[:page] - 1 
  opts[:list] = model_class.all(:order => [:updated_at.desc], 
      :limit => 5, 
      :offset => (5*page)
  )
  opts[:pages] = (model_class.count / 5) + 1
  do_list_view(packet, :archive, opts)
end

#archive_url(packet) ⇒ Object



73
74
75
76
# File 'lib/orange-more/news/resources/news_resource.rb', line 73

def archive_url(packet)
  url = orange[:sitemap, true].url_for(packet, :resource => :news, :resource_action => :archive)
  url.gsub!(/\/$/, '')
end

#archive_view(packet, opts = {}) ⇒ Object



69
70
71
# File 'lib/orange-more/news/resources/news_resource.rb', line 69

def archive_view(packet, opts = {})
  archive(packet, opts.merge!({:page => 1}))
end

#beforeNew(packet, opts = {}) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/orange-more/news/resources/news_resource.rb', line 12

def beforeNew(packet, opts = {})
  unless OrangeRoute.first(:resource => 'news', :orange_site_id => packet['site'].id)
    orange[:sitemap, true].add_route_for(packet,
      :orange_site_id => packet['site'].id, 
      :resource => :news, 
      :resource_action => :archive,
      :slug => 'news-archive', 
      :link_text => 'Orange News Archive'
    )
  end
  true
end

#find_list(packet, mode, opts = {}) ⇒ Object



78
79
80
81
# File 'lib/orange-more/news/resources/news_resource.rb', line 78

def find_list(packet, mode, opts = {})
  opts[:order] = [:updated_at.desc]
  model_class.all(opts) || []
end

#latest(packet, opts = {}) ⇒ Object



49
50
51
52
53
54
# File 'lib/orange-more/news/resources/news_resource.rb', line 49

def latest(packet, opts = {})
  limit = packet['news.latest_news_limit', 3]
  do_list_view(packet, :latest, {
    :list => model_class.all(:order => :updated_at.desc, :limit => limit)
  })
end


25
26
27
28
29
30
31
32
# File 'lib/orange-more/news/resources/news_resource.rb', line 25

def link_for(packet, news)
  match = news.link.match(/\[(\d+)\]/)
  if match
    orange[:sitemap, true].to_href(packet, match[1].to_i)
  else
    news.link
  end
end

#news_view(packet, opts = {}) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/orange-more/news/resources/news_resource.rb', line 38

def news_view(packet, opts = {})
  resource_path = packet['route.resource_path']
  if resource_path.blank?
    archive_view(packet, opts)
  elsif resource_path =~ /^\/page/
    archive(packet, opts)
  else
    archive_view(packet, opts)
  end
end

#sitemap_row(packet, opts = {}) ⇒ Object



34
35
36
# File 'lib/orange-more/news/resources/news_resource.rb', line 34

def sitemap_row(packet, opts = {})
  do_view(packet, :sitemap_row, opts)
end

#stack_initObject



5
6
7
8
9
10
# File 'lib/orange-more/news/resources/news_resource.rb', line 5

def stack_init
  orange[:admin, true].add_link("Content", :resource => @my_orange_name, :text => 'News')
  orange[:radius, true].define_tag "latest_news" do |tag|
    orange[:news].latest(tag.locals.packet)
  end
end