block.rb
4.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
class Block < ActiveRecord::Base
# to be able to generate HTML
include ActionView::Helpers::UrlHelper
include ActionView::Helpers::TagHelper
# Block-specific stuff
include BlockHelper
delegate :environment, :to => :box, :allow_nil => true
acts_as_list :scope => :box
belongs_to :box
acts_as_having_settings
named_scope :enabled, :conditions => { :enabled => true }
# Determines whether a given block must be visible. Optionally a
# <tt>context</tt> must be specified. <tt>context</tt> must be a hash, and
# may contain the following keys:
#
# * <tt>:article</tt>: the article being viewed currently
# * <tt>:language</tt>: in which language the block will be displayed
def visible?(context = nil)
return false if display == 'never'
if context
return false if language != 'all' && language != context[:locale]
begin
return self.send("display_#{display}", context)
rescue NoMethodError => exception
raise "Display '#{display}' is not a valid value."
end
end
true
end
def display_always(context)
true
end
def display_home_page_only(context)
if context[:article]
return context[:article] == owner.home_page
else
return context[:request_path] == '/'
end
end
def display_except_home_page(context)
if context[:article]
return context[:article] != owner.home_page
else
return context[:request_path] != '/' + (owner.kind_of?(Profile) ? owner.identifier : '')
end
end
# The condition for displaying a block. It can assume the following values:
#
# * <tt>'always'</tt>: the block is always displayed
# * <tt>'never'</tt>: the block is hidden (it does not appear for visitors)
# * <tt>'home_page_only'</tt> the block is displayed only when viewing the
# homepage of its owner.
# * <tt>'except_home_page'</tt> the block is displayed only when viewing
# the homepage of its owner.
settings_items :display, :type => :string, :default => 'always'
# The block can be configured to be displayed in all languages or in just one language. It can assume any locale of the environment:
#
# * <tt>'all'</tt>: the block is always displayed
settings_items :language, :type => :string, :default => 'all'
# returns the description of the block, used when the user sees a list of
# blocks to choose one to include in the design.
#
# Must be redefined in subclasses to match the description of each block
# type.
def self.description
'(dummy)'
end
# Returns the content to be used for this block.
#
# This method can return several types of objects:
#
# * <tt>String</tt>: if the string starts with <tt>http://</tt> or <tt>https://</tt>, then it is assumed to be address of an IFRAME. Otherwise it's is used as regular HTML.
# * <tt>Hash</tt>: the hash is used to build an URL that is used as the address for a IFRAME.
# * <tt>Proc</tt>: the Proc is evaluated in the scope of BoxesHelper. The
# block can then use <tt>render</tt>, <tt>link_to</tt>, etc.
#
# The method can also return <tt>nil</tt>, which means "no content".
#
# See BoxesHelper#extract_block_content for implementation details.
def content(args={})
"This is block number %d" % self.id
end
# A footer to be appended to the end of the block. Returns <tt>nil</tt>.
#
# Override in your subclasses. You can return the same types supported by
# #content.
def footer
nil
end
# Is this block editable? (Default to <tt>false</tt>)
def editable?
true
end
# must always return false, except on MainBlock clas.
def main?
false
end
def owner
box ? box.owner : nil
end
def default_title
''
end
def title
if self[:title].blank?
self.default_title
else
self[:title]
end
end
def view_title
title
end
def cacheable?
true
end
alias :active_record_cache_key :cache_key
def cache_key(language='en', user=nil)
active_record_cache_key+'-'+language
end
def timeout
4.hours
end
def has_macro?
false
end
# Override in your subclasses.
# Define which events and context should cause the block cache to expire
# Possible events are: :article, :profile, :friendship, :category
# Possible contexts are: :profile, :environment
def self.expire_on
{
:profile => [],
:environment => []
}
end
DISPLAY_OPTIONS = {
'always' => __('In all pages'),
'home_page_only' => __('Only in the homepage'),
'except_home_page' => __('In all pages, except in the homepage'),
'never' => __('Don\'t display'),
}
def display_options
DISPLAY_OPTIONS.keys
end
def display_option_label(option)
DISPLAY_OPTIONS[option]
end
def duplicate
duplicated_block = self.clone
duplicated_block.display = 'never'
duplicated_block.created_at = nil
duplicated_block.updated_at = nil
duplicated_block.save!
duplicated_block.insert_at(self.position + 1)
duplicated_block
end
end