Class: Murlsh::UrlBody

Inherits:
Builder::XmlMarkup
  • Object
show all
Includes:
Markup
Defined in:
lib/murlsh/url_body.rb

Overview

Url list page builder.

Instance Method Summary collapse

Methods included from Markup

#atom, #css, #form_input, #javascript, #metas, #murlsh_img

Constructor Details

#initialize(config, req, content_type = 'text/html') ⇒ UrlBody

Returns a new instance of UrlBody.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/murlsh/url_body.rb', line 9

def initialize(config, req, content_type='text/html')
  @config, @req, @q, @content_type =
    config, req, req.params['q'], content_type
  @page = [req.params['p'].to_i, 1].max

  @first_href, @prev_href, @next_href = page_href(1), nil, nil
 
  @total_entries, @total_pages = 0, 0

  @per_page = @req.params['pp'] ? @req.params['pp'].to_i :
    config.fetch('num_posts_page', 25)

  super(:indent => @config['html_indent'] || 0)
end

Instance Method Details

#add_formObject

Url add form builder.



168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/murlsh/url_body.rb', line 168

def add_form
  form(:action => '', :method => 'post') {
    fieldset(:id => 'add') {
      self.p { form_input :id => 'url', :label => 'Add URL', :size => 32 }
      self.p { form_input :id => 'via', :label => 'Via', :size => 32 }
      self.p {
        form_input :type => 'password', :id => 'auth', :label => 'Password',
          :size => 16
        form_input :id => 'submit', :type => 'button', :value => 'Add'
      }
    }
  }
end

#clearObject

Clear div builder.



183
# File 'lib/murlsh/url_body.rb', line 183

def clear; div(:style => 'clear : both') { }; end

#each {|html(:lang => 'en') { headd body { ul(:id => 'urls') { li { feed_icon ; search_form } last = nil mus.each do |mu| li { unless mu.same_author?(last) avatar_url = Murlsh::Plugin.hooks('avatar').inject( nil) do |url_so_far,plugin| plugin.run(url_so_far, mu, @config) end div(:class => 'icon') { murlsh_img :src => avatar_url, :text => mu.name } if avatar_url div(mu.name, :class => 'name') if @config.fetch('show_names', false) and mu.name end if mu.thumbnail_url murlsh_img :src => mu.thumbnail_url, :text => mu.title_stripped, :class => 'thumb' end a mu.title_stripped, :href => mu.url, :class => 'm' Murlsh::Plugin.hooks('url_display_add') do |p| p.run self, mu, @config end last = mu } end li { paging_nav } li { add_form } } clear powered_by js div '', :id => 'bottom' } }| ... } ⇒ Object

Url list page body builder.

Yields:

  • (html(:lang => 'en') { headd body { ul(:id => 'urls') { li { feed_icon ; search_form } last = nil mus.each do |mu| li { unless mu.same_author?(last) avatar_url = Murlsh::Plugin.hooks('avatar').inject( nil) do |url_so_far,plugin| plugin.run(url_so_far, mu, @config) end div(:class => 'icon') { murlsh_img :src => avatar_url, :text => mu.name } if avatar_url div(mu.name, :class => 'name') if @config.fetch('show_names', false) and mu.name end if mu.thumbnail_url murlsh_img :src => mu.thumbnail_url, :text => mu.title_stripped, :class => 'thumb' end a mu.title_stripped, :href => mu.url, :class => 'm' Murlsh::Plugin.hooks('url_display_add') do |p| p.run self, mu, @config end last = mu } end li { paging_nav } li { add_form } } clear powered_by js div '', :id => 'bottom' } })


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/murlsh/url_body.rb', line 64

def each
  mus = urls

  declare! :DOCTYPE, :html

  yield html(:lang => 'en') {
    headd
    body {
      ul(:id => 'urls') {
        li { feed_icon ; search_form }

        last = nil

        mus.each do |mu|
          li {
            unless mu.same_author?(last)
              avatar_url = Murlsh::Plugin.hooks('avatar').inject(
                nil) do |url_so_far,plugin|
                plugin.run(url_so_far, mu, @config)
              end
              div(:class => 'icon') {
                murlsh_img :src => avatar_url, :text => mu.name
              }  if avatar_url
              div(mu.name, :class => 'name')  if
                @config.fetch('show_names', false) and mu.name
            end

            if mu.thumbnail_url
              murlsh_img :src => mu.thumbnail_url,
                :text => mu.title_stripped, :class => 'thumb'
            end

            a mu.title_stripped, :href => mu.url, :class => 'm'

            Murlsh::Plugin.hooks('url_display_add') do |p|
              p.run self, mu, @config
            end

            last = mu
          }
        end

        li { paging_nav }

        li { add_form }
      }

      clear
      powered_by
      js
      div '', :id => 'bottom'
    }
  }
end

#feed_iconObject

Feed icon builder.



140
141
142
143
144
# File 'lib/murlsh/url_body.rb', line 140

def feed_icon
  div(:class => 'icon') {
    a('feed', :href => @config.fetch('feed_file'), :class => 'feed')
  }
end

#headdObject

Head builder.



120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/murlsh/url_body.rb', line 120

def headd
  head {
    titlee
    meta :'http-equiv' => 'Content-Type', :content => @content_type
    metas(@config.find_all { |k,v| k =~ /^meta_tag_/ and v }.
      map { |k,v| [k.sub('meta_tag_', ''), v] })
    css(@config['css_compressed'] || @config['css_files'])
    atom @config.fetch('feed_file')
    link :rel => 'first', :href => @first_href
    link :rel => 'prev', :href => @prev_href  if @prev_href
    link :rel => 'next', :href => @next_href  if @next_href
  }
end

#jsObject

Required javascript builder.



194
# File 'lib/murlsh/url_body.rb', line 194

def js; javascript(@config['js_compressed'] || @config['js_files']); end

#page_href(page) ⇒ Object

Get the href of a page in the same result set as this page.



25
26
27
28
29
# File 'lib/murlsh/url_body.rb', line 25

def page_href(page)
  query = @req.params.dup
  query['p'] = page
  Murlsh.build_query(query)
end

#paging_navObject

Paging navigation.



157
158
159
160
161
162
163
164
165
# File 'lib/murlsh/url_body.rb', line 157

def paging_nav
  text! "Page #{@page}/#{@total_pages}"
  if @prev_href
    text! ' | '; a 'previous', :href => @prev_href
  end
  if @next_href
    text! ' | '; a 'next', :href => @next_href
  end
end

#powered_byObject

Powered by builder.



186
187
188
189
190
191
# File 'lib/murlsh/url_body.rb', line 186

def powered_by
  self.p {
    text! 'powered by '
    a 'murlsh', :href => 'http://github.com/mmb/murlsh/'
  }
end

#search_conditionsObject

Search conditions builder for ActiveRecord conditions.



53
54
55
56
57
58
59
60
61
# File 'lib/murlsh/url_body.rb', line 53

def search_conditions
  if @q
    search_cols = %w{name title url}
    [search_cols.map { |x| "MURLSHMATCH(#{x}, ?)" }.join(' OR ')].push(
      *[@q] * search_cols.size)
  else
    []
  end
end

#search_formObject

Search form builder.



147
148
149
150
151
152
153
154
# File 'lib/murlsh/url_body.rb', line 147

def search_form
  form(:action => '', :method => 'get') {
    fieldset {
      form_input :id => 'q', :size => 32, :value => @q
      form_input :type => 'submit', :value => 'Regex Search'
    }
  }
end

#titleeObject

Title builder.



135
136
137
# File 'lib/murlsh/url_body.rb', line 135

def titlee
  title(@config.fetch('page_title', '') + (@q ? " /#{@q}" : ''))
end

#urlsObject

Fetch urls based on query string parameters.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/murlsh/url_body.rb', line 32

def urls
  search = search_conditions

  @total_entries = Murlsh::Url.count(:conditions => search)
  @total_pages = (@total_entries / @per_page.to_f).ceil

  if @page > 1 and @page <= @total_pages
    @prev_href = page_href(@page - 1)
  end

  if @page < @total_pages
    @next_href = page_href(@page + 1)
  end

  offset = (@page - 1) * @per_page

  Murlsh::Url.all(:conditions => search_conditions, :order => 'time DESC',
    :limit => @per_page, :offset => offset)
end