From 27067b4d8e5e854f16e3bdb08c2f63f4250632a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstro=CC=88m?= Date: Wed, 3 Mar 2021 14:36:03 +0100 Subject: Add 'url' field to config table --- interfaces/ruby/models/config.rb | 14 ++++++++++++-- interfaces/ruby/models/post.rb | 4 ++++ 2 files changed, 16 insertions(+), 2 deletions(-) (limited to 'interfaces/ruby') diff --git a/interfaces/ruby/models/config.rb b/interfaces/ruby/models/config.rb index 55b4106..007dbb3 100644 --- a/interfaces/ruby/models/config.rb +++ b/interfaces/ruby/models/config.rb @@ -1,17 +1,27 @@ class Config < ActiveRecord::Base self.table_name = 'config' + before_validation :normalize_url + validates_presence_of :url validates_presence_of :title validates_presence_of :timezone - validates_presence_of :postsperpage + validates_presence_of :posts_per_page + +private + + def normalize_url + url.strip! + url.sub! /\/$/, '' + end end class CreateConfigTable < ActiveRecord::Migration[6.0] def change create_table :config do |t| + t.string :url, null: false t.string :title, null: false t.string :subtitle t.string :timezone, null: false - t.integer :postsperpage, null: false + t.integer :posts_per_page, null: false, default: 15 end end end diff --git a/interfaces/ruby/models/post.rb b/interfaces/ruby/models/post.rb index 3a29a1c..28cc82c 100644 --- a/interfaces/ruby/models/post.rb +++ b/interfaces/ruby/models/post.rb @@ -6,6 +6,10 @@ class Post < ActiveRecord::Base validates_presence_of :slug validates_presence_of :title validates_presence_of :body + + def url + "#{$config.url}/#{created_at.strftime("%Y/%m/%d")}/#{slug}" + end end class CreatePostTable < ActiveRecord::Migration[6.0] -- cgit v1.2.3