From: Tom Hughes Date: Sat, 10 Jan 2015 15:34:03 +0000 (+0000) Subject: Set default formats in the database now that rails handles enums X-Git-Tag: live~4296^2~2 X-Git-Url: https://git.openstreetmap.org/rails.git/commitdiff_plain/8fa9763281c5bcd4e9a88cfa0597e4e9a86963e7 Set default formats in the database now that rails handles enums Because rails now reads the defaults from the database correctly it no longer works to set them conditionally in after_initialise as they have already been set. --- diff --git a/app/models/diary_comment.rb b/app/models/diary_comment.rb index 9d29f52b7..5ace3d158 100644 --- a/app/models/diary_comment.rb +++ b/app/models/diary_comment.rb @@ -5,7 +5,6 @@ class DiaryComment < ActiveRecord::Base validates_presence_of :body validates_associated :diary_entry - after_initialize :set_defaults after_save :spam_check def body @@ -23,10 +22,6 @@ class DiaryComment < ActiveRecord::Base private - def set_defaults - self.body_format = "markdown" unless self.attribute_present?(:body_format) - end - def spam_check user.spam_check end diff --git a/app/models/diary_entry.rb b/app/models/diary_entry.rb index 58f8710f9..c0fbac546 100644 --- a/app/models/diary_entry.rb +++ b/app/models/diary_entry.rb @@ -16,7 +16,6 @@ class DiaryEntry < ActiveRecord::Base :greater_than_or_equal_to => -180, :less_than_or_equal_to => 180 validates_associated :language - after_initialize :set_defaults after_save :spam_check def body @@ -25,10 +24,6 @@ class DiaryEntry < ActiveRecord::Base private - def set_defaults - self.body_format = "markdown" unless self.attribute_present?(:body_format) - end - def spam_check user.spam_check end diff --git a/app/models/message.rb b/app/models/message.rb index b51c59f43..b05d005b0 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -9,8 +9,6 @@ class Message < ActiveRecord::Base validates_inclusion_of :message_read, :in => [ true, false ] validates_as_utf8 :title - after_initialize :set_defaults - def self.from_mail(mail, from, to) if mail.multipart? if mail.text_part @@ -47,10 +45,4 @@ class Message < ActiveRecord::Base md5 << body md5.hexdigest end - -private - - def set_defaults - self.body_format = "markdown" unless self.attribute_present?(:body_format) - end end diff --git a/app/models/redaction.rb b/app/models/redaction.rb index 43f5b3c7b..a9bdf5580 100644 --- a/app/models/redaction.rb +++ b/app/models/redaction.rb @@ -14,19 +14,9 @@ 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 diff --git a/app/models/user.rb b/app/models/user.rb index c845d4d0a..7ace4bca4 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -243,7 +243,6 @@ private def set_defaults self.creation_time = Time.now.getutc unless self.attribute_present?(:creation_time) - self.description_format = "markdown" unless self.attribute_present?(:description_format) end def encrypt_password diff --git a/app/models/user_block.rb b/app/models/user_block.rb index cb1a97dca..d8fa95cb2 100644 --- a/app/models/user_block.rb +++ b/app/models/user_block.rb @@ -4,8 +4,6 @@ class UserBlock < ActiveRecord::Base belongs_to :user, :class_name => "User", :foreign_key => :user_id belongs_to :creator, :class_name => "User", :foreign_key => :creator_id belongs_to :revoker, :class_name => "User", :foreign_key => :revoker_id - - after_initialize :set_defaults PERIODS = USER_BLOCK_PERIODS @@ -41,12 +39,6 @@ class UserBlock < ActiveRecord::Base private - ## - # set default values for new records. - def set_defaults - self.reason_format = "markdown" unless self.attribute_present?(:reason_format) - end - ## # validate that only moderators are allowed to change the # block. this should be caught and dealt with in the controller, diff --git a/db/migrate/20150110152606_change_default_formats.rb b/db/migrate/20150110152606_change_default_formats.rb new file mode 100644 index 000000000..84bc2be40 --- /dev/null +++ b/db/migrate/20150110152606_change_default_formats.rb @@ -0,0 +1,17 @@ +class ChangeDefaultFormats < ActiveRecord::Migration + def up + change_column_default :diary_entries, :body_format, "markdown" + change_column_default :diary_comments, :body_format, "markdown" + change_column_default :messages, :body_format, "markdown" + change_column_default :users, :description_format, "markdown" + change_column_default :user_blocks, :reason_format, "markdown" + end + + def down + change_column_default :diary_entries, :body_format, "html" + change_column_default :diary_comments, :body_format, "html" + change_column_default :messages, :body_format, "html" + change_column_default :users, :description_format, "html" + change_column_default :user_blocks, :reason_format, "html" + end +end diff --git a/db/structure.sql b/db/structure.sql index cfbd368fd..e54354a03 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -485,7 +485,7 @@ CREATE TABLE diary_comments ( created_at timestamp without time zone NOT NULL, updated_at timestamp without time zone NOT NULL, visible boolean DEFAULT true NOT NULL, - body_format format_enum DEFAULT 'html'::format_enum NOT NULL + body_format format_enum DEFAULT 'markdown'::format_enum NOT NULL ); @@ -523,7 +523,7 @@ CREATE TABLE diary_entries ( longitude double precision, language_code character varying DEFAULT 'en'::character varying NOT NULL, visible boolean DEFAULT true NOT NULL, - body_format format_enum DEFAULT 'html'::format_enum NOT NULL + body_format format_enum DEFAULT 'markdown'::format_enum NOT NULL ); @@ -684,7 +684,7 @@ CREATE TABLE messages ( to_user_id bigint NOT NULL, to_user_visible boolean DEFAULT true NOT NULL, from_user_visible boolean DEFAULT true NOT NULL, - body_format format_enum DEFAULT 'html'::format_enum NOT NULL + body_format format_enum DEFAULT 'markdown'::format_enum NOT NULL ); @@ -983,7 +983,7 @@ CREATE TABLE user_blocks ( revoker_id bigint, created_at timestamp without time zone, updated_at timestamp without time zone, - reason_format format_enum DEFAULT 'html'::format_enum NOT NULL + reason_format format_enum DEFAULT 'markdown'::format_enum NOT NULL ); @@ -1110,7 +1110,7 @@ CREATE TABLE users ( openid_url character varying, preferred_editor character varying, terms_seen boolean DEFAULT false NOT NULL, - description_format format_enum DEFAULT 'html'::format_enum NOT NULL, + description_format format_enum DEFAULT 'markdown'::format_enum NOT NULL, image_fingerprint character varying, changesets_count integer DEFAULT 0 NOT NULL, traces_count integer DEFAULT 0 NOT NULL, @@ -2541,6 +2541,8 @@ INSERT INTO schema_migrations (version) VALUES ('20140507110937'); INSERT INTO schema_migrations (version) VALUES ('20140519141742'); +INSERT INTO schema_migrations (version) VALUES ('20150110152606'); + INSERT INTO schema_migrations (version) VALUES ('21'); INSERT INTO schema_migrations (version) VALUES ('22');