Class: Sections::Controller::Sections
- Inherits:
-
Zen::Controller::AdminController
- Object
- Ramaze::Controller
- Zen::Controller::BaseController
- Zen::Controller::AdminController
- Sections::Controller::Sections
- Defined in:
- lib/zen/package/sections/lib/sections/controller/sections.rb
Overview
Sections are data containers with a specific purpose. For example, you might have a "Blog" or "Pages" section each with it's own entries, categories, custom fields and so on. A section and it's entries glue all the other data types (such as those mentioned earlier) together to form the content displayed on your website.
Sections can be managed by going to /admin. This page will show an
overview of all existing sections as well as a few buttons and links that
allow you to edit, create or delete sections as well as managing the
entries for each existing section.
Creating/Editing Sections
Creating a new section can be done by clicking the button "Add section" while editing a section can be done by clicking the name of a section. In both cases you'll end up with a form that looks like the one in the images below.
In this form you can specify the following fields:
- Name (required): the name of the section.
- Slug: a URL friendly version of the section name. If no slug is specified one will be generated automatically.
- Description: a description of the section to help clarify it's purpose.
- Allow comments (required): whether or not users can submit comments for entries assigned to the section.
- Comments require an account (required): when set to "Yes" a user has to be logged in in order to post a comment.
- Moderate comments (required): when enabled a comment first has to be approved before it's displayed. This option is disabled by default.
- Comment format (required): the format comments are posted in such as Markdown or plain text.
- Custom field groups: all the custom field groups to assign to the section. These groups can then be used by all the entries in the section.
- Category groups: all the category groups that should be available to the section entries of this section.
Note that the name and the slug of a section can not be longer than 255 characters.
Used Permissions
- show_section
- new_section
- edit_section
- delete_section
Events
All events in this controller receive an instance of
Model::Section. The event after_delete_section receives an
instance that has already been removed, thus you can't make any changes to
it and save those in the database.
Example of creating a dummy section entry:
Zen::Event.listen(:new_section) do |section|
section.add_section_entry(:title => 'My Entry', :user_id => user.id)
end
Instance Method Summary (collapse)
-
- (Object) delete
Deletes a number of sections and all the related data.
-
- (Object) edit(id)
Show a form that lets the user edit an existing section.
-
- (Object) index
Show an overview of all existing sections.
-
- (Object) new
Show a form that lets the user create a new section.
-
- (Object) save
Saves any changes made to an existing section or creates a new one.
Instance Method Details
- (Object) delete
Deletes a number of sections and all the related data. These sections should be specified in the POST array "section_ids[]".
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 |
# File 'lib/zen/package/sections/lib/sections/controller/sections.rb', line 267 def delete (:delete_section) if !request.params['section_ids'] \ or request.params['section_ids'].empty? (:error, lang('sections.errors.no_delete')) redirect_referrer end request.params['section_ids'].each do |id| section = ::Sections::Model::Section[id] next if section.nil? Zen::Event.call(:before_delete_section, section) begin section.destroy rescue => e Ramaze::Log.error(e.inspect) (:error, lang('sections.errors.delete') % id) redirect_referrer end Zen::Event.call(:after_delete_section, section) end (:success, lang('sections.success.delete')) redirect_referrer end |
- (Object) edit(id)
Show a form that lets the user edit an existing section.
151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/zen/package/sections/lib/sections/controller/sections.rb', line 151 def edit(id) (:edit_section) ( Sections.a(lang('sections.titles.index'), :index), @page_title ) @section = flash[:form_data] || validate_section(id) render_view(:form) end |
- (Object) index
Show an overview of all existing sections. Using this overview a user can manage an existing section, delete it or create a new one.
131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/zen/package/sections/lib/sections/controller/sections.rb', line 131 def index (:show_section) (lang('sections.titles.index')) @sections = search do |query| ::Sections::Model::Section.search(query).order(:id.asc) end @sections ||= ::Sections::Model::Section.order(:id.asc) @sections = paginate(@sections) end |
- (Object) new
Show a form that lets the user create a new section.
170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/zen/package/sections/lib/sections/controller/sections.rb', line 170 def new (:new_section) ( Sections.a(lang('sections.titles.index'), :index), @page_title ) @section = flash[:form_data] || ::Sections::Model::Section.new render_view(:form) end |
- (Object) save
Saves any changes made to an existing section or creates a new one.
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/zen/package/sections/lib/sections/controller/sections.rb', line 194 def save post = request.subset( :id, :name, :slug, :description, :comment_allow, :comment_require_account, :comment_moderate, :comment_format, :custom_field_group_pks, :category_group_pks ) if post['id'] and !post['id'].empty? (:edit_section) section = validate_section(post['id']) save_action = :save before_event = :before_edit_section after_event = :after_edit_section else (:new_section) section = ::Sections::Model::Section.new save_action = :new before_event = :before_new_section after_event = :after_new_section end success = lang("sections.success.#{save_action}") error = lang("sections.errors.#{save_action}") post['custom_field_group_pks'] ||= [] post['category_group_pks'] ||= [] post.delete('id') begin post.each { |k, v| section.send("#{k}=", v) } Zen::Event.call(before_event, section) section.save if save_action == :new section.custom_field_group_pks = post['custom_field_group_pks'] section.category_group_pks = post['category_group_pks'] end rescue => e Ramaze::Log.error(e.inspect) (:error, error) flash[:form_data] = section flash[:form_errors] = section.errors redirect_referrer end Zen::Event.call(after_event, section) (:success, success) redirect(Sections.r(:edit, section.id)) end |