From 2450088c03719db0c17e71dfc44f48a7862b74b3 Mon Sep 17 00:00:00 2001 From: Benjamin Kampmann Date: Fri, 25 Apr 2014 15:14:05 +0200 Subject: [PATCH] Add CustomFields to Post, Category and Group --- app/models/category.rb | 3 ++ app/models/category_custom_field.rb | 19 +++++++++++++ app/models/group.rb | 4 +++ app/models/group_custom_field.rb | 19 +++++++++++++ app/models/post.rb | 2 ++ app/models/post_custom_field.rb | 19 +++++++++++++ .../20140425125742_add_custom_fields.rb | 28 +++++++++++++++++++ spec/models/category_spec.rb | 12 ++++++++ spec/models/group_spec.rb | 13 +++++++++ spec/models/post_spec.rb | 12 ++++++++ 10 files changed, 131 insertions(+) create mode 100644 app/models/category_custom_field.rb create mode 100644 app/models/group_custom_field.rb create mode 100644 app/models/post_custom_field.rb create mode 100644 db/migrate/20140425125742_add_custom_fields.rb diff --git a/app/models/category.rb b/app/models/category.rb index 1b512899987..4bd71f5be4b 100644 --- a/app/models/category.rb +++ b/app/models/category.rb @@ -1,6 +1,9 @@ +require_dependency "concern/has_custom_fields" + class Category < ActiveRecord::Base include Positionable + include Concern::HasCustomFields belongs_to :topic, dependent: :destroy belongs_to :topic_only_relative_url, diff --git a/app/models/category_custom_field.rb b/app/models/category_custom_field.rb new file mode 100644 index 00000000000..ccc3344ff8b --- /dev/null +++ b/app/models/category_custom_field.rb @@ -0,0 +1,19 @@ +class CategoryCustomField < ActiveRecord::Base + belongs_to :category +end + +# == Schema Information +# +# Table name: category_custom_fields +# +# id :integer not null, primary key +# category_id :integer not null +# name :string(256) not null +# value :text +# created_at :datetime +# updated_at :datetime +# +# Indexes +# +# index_category_custom_fields_on_category_id_and_name (category_id,name) +# diff --git a/app/models/group.rb b/app/models/group.rb index 2741b30dfcb..35933da1012 100644 --- a/app/models/group.rb +++ b/app/models/group.rb @@ -1,4 +1,8 @@ +require_dependency "concern/has_custom_fields" + class Group < ActiveRecord::Base + include Concern::HasCustomFields + has_many :category_groups has_many :group_users, dependent: :destroy diff --git a/app/models/group_custom_field.rb b/app/models/group_custom_field.rb new file mode 100644 index 00000000000..78b087b97c8 --- /dev/null +++ b/app/models/group_custom_field.rb @@ -0,0 +1,19 @@ +class GroupCustomField < ActiveRecord::Base + belongs_to :group +end + +# == Schema Information +# +# Table name: group_custom_fields +# +# id :integer not null, primary key +# group_id :integer not null +# name :string(256) not null +# value :text +# created_at :datetime +# updated_at :datetime +# +# Indexes +# +# index_group_custom_fields_on_group_id_and_name (group_id,name) +# diff --git a/app/models/post.rb b/app/models/post.rb index 3d2c766ce97..fc38fc40798 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -6,6 +6,7 @@ require_dependency 'enum' require_dependency 'post_analyzer' require_dependency 'validators/post_validator' require_dependency 'plugin/filter' +require_dependency "concern/has_custom_fields" require 'archetype' require 'digest/sha1' @@ -13,6 +14,7 @@ require 'digest/sha1' class Post < ActiveRecord::Base include RateLimiter::OnCreateRecord include Trashable + include Concern::HasCustomFields rate_limit rate_limit :limit_posts_per_day diff --git a/app/models/post_custom_field.rb b/app/models/post_custom_field.rb new file mode 100644 index 00000000000..e7b00105660 --- /dev/null +++ b/app/models/post_custom_field.rb @@ -0,0 +1,19 @@ +class PostCustomField < ActiveRecord::Base + belongs_to :post +end + +# == Schema Information +# +# Table name: post_custom_fields +# +# id :integer not null, primary key +# post_id :integer not null +# name :string(256) not null +# value :text +# created_at :datetime +# updated_at :datetime +# +# Indexes +# +# index_post_custom_fields_on_post_id_and_name (post_id,name) +# diff --git a/db/migrate/20140425125742_add_custom_fields.rb b/db/migrate/20140425125742_add_custom_fields.rb new file mode 100644 index 00000000000..3b6c542fde4 --- /dev/null +++ b/db/migrate/20140425125742_add_custom_fields.rb @@ -0,0 +1,28 @@ +class AddCustomFields < ActiveRecord::Migration + def change + create_table :category_custom_fields do |t| + t.integer :category_id, null: false + t.string :name, limit: 256, null: false + t.text :value + t.timestamps + end + + create_table :group_custom_fields do |t| + t.integer :group_id, null: false + t.string :name, limit: 256, null: false + t.text :value + t.timestamps + end + + create_table :post_custom_fields do |t| + t.integer :post_id, null: false + t.string :name, limit: 256, null: false + t.text :value + t.timestamps + end + + add_index :category_custom_fields, [:category_id, :name] + add_index :group_custom_fields, [:group_id, :name] + add_index :post_custom_fields, [:post_id, :name] + end +end diff --git a/spec/models/category_spec.rb b/spec/models/category_spec.rb index 5c10f2f2128..cb69dc6554d 100644 --- a/spec/models/category_spec.rb +++ b/spec/models/category_spec.rb @@ -133,6 +133,18 @@ describe Category do Fabricate(:category, name: " blanks ").name.should == "blanks" end + it "has custom fields" do + category = Fabricate(:category, name: " music") + category.custom_fields["a"].should == nil + + category.custom_fields["bob"] = "marley" + category.custom_fields["jack"] = "black" + category.save + + category = Category.find(category.id) + category.custom_fields.should == {"bob" => "marley", "jack" => "black"} + end + describe "short name" do let!(:category) { Fabricate(:category, name: 'xx') } diff --git a/spec/models/group_spec.rb b/spec/models/group_spec.rb index 67675e71496..ca067f1c1d7 100644 --- a/spec/models/group_spec.rb +++ b/spec/models/group_spec.rb @@ -148,6 +148,19 @@ describe Group do GroupUser.count.should == original_count end + + it "has custom fields" do + group = Fabricate(:group) + group.custom_fields["a"].should == nil + + group.custom_fields["hugh"] = "jackman" + group.custom_fields["jack"] = "black" + group.save + + group = Group.find(group.id) + group.custom_fields.should == {"hugh" => "jackman", "jack" => "black"} + end + it "allows you to lookup a new group by name" do group = Fabricate(:group) group.id.should == Group[group.name].id diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb index 36d2984f8d4..fda07e82329 100644 --- a/spec/models/post_spec.rb +++ b/spec/models/post_spec.rb @@ -814,4 +814,16 @@ describe Post do end end + it "has custom fields" do + post = Fabricate(:post) + post.custom_fields["a"].should == nil + + post.custom_fields["Tommy"] = "Hanks" + post.custom_fields["Vincent"] = "Vega" + post.save + + post = Post.find(post.id) + post.custom_fields.should == {"Tommy" => "Hanks", "Vincent" => "Vega"} + end + end