From 0330e109437fdcb8fbfdd112492699607b507d4c Mon Sep 17 00:00:00 2001 From: Matt Amos Date: Wed, 4 Apr 2012 16:56:24 +0100 Subject: [PATCH] Set defaults properly on redaction model, and remove unnecessary guards around update. --- app/controllers/redactions_controller.rb | 14 +++----------- app/models/redaction.rb | 12 ++++++++++++ 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/app/controllers/redactions_controller.rb b/app/controllers/redactions_controller.rb index 03324ad8f..f8713608d 100644 --- a/app/controllers/redactions_controller.rb +++ b/app/controllers/redactions_controller.rb @@ -22,9 +22,7 @@ class RedactionsController < ApplicationController @redaction.user = @user @redaction.title = params[:redaction][:title] @redaction.description = params[:redaction][:description] - # didn't see this come in from the form - maybe i'm doing something - # wrong, or markdown is the only thing supported at the moment? - @redaction.description_format = 'markdown' + # note that the description format will default to 'markdown' if @redaction.save flash[:notice] = t('redaction.create.flash') @@ -42,14 +40,8 @@ class RedactionsController < ApplicationController def update # note - don't update the user ID - - if params[:redaction][:title] and params[:redaction][:title] != @redaction.title - @redaction.title = params[:redaction][:title] - end - - if params[:redaction][:description] and params[:redaction][:description] != @redaction.description - @redaction.description = params[:redaction][:description] - end + @redaction.title = params[:redaction][:title] + @redaction.description = params[:redaction][:description] if @redaction.save flash[:notice] = t('redaction.update.flash') diff --git a/app/models/redaction.rb b/app/models/redaction.rb index ca0ea7232..43f5b3c7b 100644 --- a/app/models/redaction.rb +++ b/app/models/redaction.rb @@ -14,7 +14,19 @@ class Redaction < ActiveRecord::Base has_many :old_ways has_many :old_relations + after_initialize :set_defaults + + # this method overrides the AR default to provide the rich + # text object for the description field. def description RichText.new(read_attribute(:description_format), read_attribute(:description)) end + + private + + # set the default format to be markdown, in the absence of + # any other setting. + def set_defaults + self.description_format = "markdown" unless self.attribute_present?(:description_format) + end end -- 2.43.2