Class: Sections::Controller::SectionEntries
- Inherits:
-
Zen::Controller::AdminController
- Object
- Ramaze::Controller
- Zen::Controller::BaseController
- Zen::Controller::AdminController
- Sections::Controller::SectionEntries
- Defined in:
- lib/zen/package/sections/lib/sections/controller/section_entries.rb
Overview
Section entries are collections of custom field values as well as some meta data related to a section. In a typical application a blog can be seen as a section and blog articles would be section entries.
Section entries can be managed by going to a section and clicking the link "Manage entries". This will bring you to an overview of all existing entries that looks like the one in the image below.
Editing an entry can be done by clicking on it's name, creating a new one can be done by clicking on the "Add section entry" button. In both cases you'll see a form that looks similar to the one displayed in the images below.
In the images above there are four tabs displayed. "Basic", "Categories", "General" and "Meta". The first two are always available, the last two tabs are tabs specific to the custom field groups assigned to a section the entry belongs to. This means that you might have other tabs depending on the names of your field groups.
Regardless of what field groups and categories you have assigned you can always specify the following fields:
- Title (required): the title of your entry.
- Slug: a URL friendly version of the title. If no slug is specified one will be generated manually.
- Created at: The date on which the entry was created. This field is filled in automatically when an entry is created.
- Author (required): the name of the person who wrote the entry.
- Status (required): the status of an entry. If an entry has a status other than "Published" it will not be displayed when using the sectio_entries plugin.
Depending on whether or not you have category and field groups assigned you can also use these fields. In the images above there's a "Body" field which is required and converts the text to HTML using Markdown.
Permissions
This controller uses the following permissions:
- show_section_entry
- new_section_entry
- edit_section_entry
- delete_section_entry
Events
All events in this controller receive an instance of
Model::SectionEntry. The event after_delete_section_entry
receives an instance of this model that has already been destroyed using
#destroy().
Instance Method Summary (collapse)
-
- (Object) delete
Delete a set of section entries based on the supplied POST field "section_entry_ids".
-
- (Object) edit(section_id, entry_id)
Show a form that lets the user edit an existing section entry.
-
- (Object) index(section_id)
Show an overview of all entries for the current section.
-
- (Object) new(section_id)
Show a form that lets the user create a new section entry.
-
- (Object) save
Saves any changes made to an existing entry and all the field values or creates a new entry.
Instance Method Details
- (Object) delete
Delete a set of section entries based on the supplied POST field "section_entry_ids".
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 |
# File 'lib/zen/package/sections/lib/sections/controller/section_entries.rb', line 326 def delete (:delete_section_entry) if !request.params['section_entry_ids'] \ or request.params['section_entry_ids'].empty? (:error, lang('section_entries.errors.no_delete')) redirect_referrer end request.params['section_entry_ids'].each do |id| entry = ::Sections::Model::SectionEntry[id] next if entry.nil? Zen::Event.call(:before_delete_section_entry, entry) begin entry.destroy rescue => e Ramaze::Log.error(e.inspect) (:error,lang('section_entries.errors.delete') % id) redirect_referrer end Zen::Event.call(:after_delete_section_entry, entry) end (:success, lang('section_entries.success.delete')) redirect_referrer end |
- (Object) edit(section_id, entry_id)
Show a form that lets the user edit an existing section entry.
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 |
# File 'lib/zen/package/sections/lib/sections/controller/section_entries.rb', line 157 def edit(section_id, entry_id) (:edit_section_entry) ( Sections.a( lang('sections.titles.index'), :index ), SectionEntries.a( lang('section_entries.titles.index'), :index, section_id ), lang('section_entries.titles.edit') ) validate_section(section_id) if flash[:form_data] @entry = flash[:form_data] else @entry = validate_section_entry(entry_id, section_id) end @section_id = section_id @possible_categories = @entry.possible_categories @custom_fields_hash = @entry.custom_fields_hash render_view(:form) end |
- (Object) index(section_id)
Show an overview of all entries for the current section.
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/zen/package/sections/lib/sections/controller/section_entries.rb', line 88 def index(section_id) (:show_section_entry) ( Sections.a(lang('sections.titles.index'), :index), lang('section_entries.titles.index') ) section = validate_section(section_id) @section_id = section_id @entries = search do |query| ::Sections::Model::SectionEntry.search(query) \ .filter(:section_id => section_id) \ .order(:id.asc) end @entries ||= ::Sections::Model::SectionEntry \ .filter(:section_id => section_id) \ .order(:id.asc) @entries = paginate(@entries) end |
- (Object) new(section_id)
Show a form that lets the user create a new section entry.
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 |
# File 'lib/zen/package/sections/lib/sections/controller/section_entries.rb', line 118 def new(section_id) (:new_section_entry) ( Sections.a( lang('sections.titles.index'), :index ), SectionEntries.a( lang('section_entries.titles.index'), :index, section_id ), lang('section_entries.titles.new') ) validate_section(section_id) @section_id = section_id if flash[:form_data] @entry = flash[:form_data] else @entry = ::Sections::Model::SectionEntry.new( :section_id => section_id ) end @possible_categories = @entry.possible_categories @custom_fields_hash = @entry.custom_fields_hash render_view(:form) end |
- (Object) save
Saves any changes made to an existing entry and all the field values or creates a new entry.
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 257 258 259 260 261 262 263 264 265 266 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 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 |
# File 'lib/zen/package/sections/lib/sections/controller/section_entries.rb', line 197 def save section_id = request.params['section_id'] validate_section(section_id) if request.params['id'] and !request.params['id'].empty? (:edit_section_entry) entry = ::Sections::Model::SectionEntry[request.params['id']] save_action = :save before_event = :before_edit_section_entry after_event = :after_edit_section_entry # Section entries aren't considered to be updated whenever a custom # field value is modified, this solves that problem request.params['updated_at'] = Time.new else (:new_section_entry) entry = ::Sections::Model::SectionEntry.new(:section_id => section_id) before_event = :before_new_section_entry after_event = :after_new_section_entry save_action = :new end request.params.delete('id') success = lang("section_entries.success.#{save_action}") error = lang("section_entries.errors.#{save_action}") custom_fields = entry.custom_fields field_errors = {} field_values = {} entry.custom_field_values.each do |value| field_values[value.custom_field_id] = value end begin Zen.database.transaction do # Update the entry itself post_data = request.subset( :title, :created_at, :updated_at, :section_id, :user_id, :slug, :section_entry_status_id, :category_pks ) # Transform the dates properly if post_data[:created_at] post_data[:created_at] = Time.strptime( post_data[:created_at], date_format ) end if post_data[:updated_at] post_data[:updated_at] = Time.strptime( post_data[:updated_at], date_format ) end post_data.each { |k, v| entry.send("#{k}=", v) } Zen::Event.call(before_event, entry) entry.save # Update/add all the custom field values custom_fields.each do |field| key = "custom_field_value_#{field.id}" # The custom field has been submitted, let's see if we have to # update it or add it. if request.params.key?(key) # Validate it if field.required and request.params[key].empty? field_errors[:custom_field_value_#{field.id}"] = \ lang('zen_models.presence') raise end # Update it if field_values.key?(field.id) field_values[field.id].update(:value => request.params[key]) # Add it else entry.add_custom_field_value( :custom_field_id => field.id, :value => request.params[key] ) end end end end # The rescue statement is called whenever the following happens: # # 1. The fields for the section entry (title, slug, etc) are invalid # 2. Any custom field marked as required didn't have a value # 3. Something else went wrong, god knows what. rescue => e Ramaze::Log.error(e.inspect) (:error, error) flash[:form_errors] = entry.errors.merge(field_errors) flash[:form_data] = entry redirect_referrer end Zen::Event.call(after_event, entry) (:success, success) redirect(SectionEntries.r(:edit, section_id, entry.id)) end |