Add CustomFields to Post, Category and Group

This commit is contained in:
Benjamin Kampmann 2014-04-25 15:14:05 +02:00
parent e6e03a1a96
commit 2450088c03
10 changed files with 131 additions and 0 deletions

View File

@ -1,6 +1,9 @@
require_dependency "concern/has_custom_fields"
class Category < ActiveRecord::Base class Category < ActiveRecord::Base
include Positionable include Positionable
include Concern::HasCustomFields
belongs_to :topic, dependent: :destroy belongs_to :topic, dependent: :destroy
belongs_to :topic_only_relative_url, belongs_to :topic_only_relative_url,

View File

@ -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)
#

View File

@ -1,4 +1,8 @@
require_dependency "concern/has_custom_fields"
class Group < ActiveRecord::Base class Group < ActiveRecord::Base
include Concern::HasCustomFields
has_many :category_groups has_many :category_groups
has_many :group_users, dependent: :destroy has_many :group_users, dependent: :destroy

View File

@ -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)
#

View File

@ -6,6 +6,7 @@ require_dependency 'enum'
require_dependency 'post_analyzer' require_dependency 'post_analyzer'
require_dependency 'validators/post_validator' require_dependency 'validators/post_validator'
require_dependency 'plugin/filter' require_dependency 'plugin/filter'
require_dependency "concern/has_custom_fields"
require 'archetype' require 'archetype'
require 'digest/sha1' require 'digest/sha1'
@ -13,6 +14,7 @@ require 'digest/sha1'
class Post < ActiveRecord::Base class Post < ActiveRecord::Base
include RateLimiter::OnCreateRecord include RateLimiter::OnCreateRecord
include Trashable include Trashable
include Concern::HasCustomFields
rate_limit rate_limit
rate_limit :limit_posts_per_day rate_limit :limit_posts_per_day

View File

@ -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)
#

View File

@ -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

View File

@ -133,6 +133,18 @@ describe Category do
Fabricate(:category, name: " blanks ").name.should == "blanks" Fabricate(:category, name: " blanks ").name.should == "blanks"
end 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 describe "short name" do
let!(:category) { Fabricate(:category, name: 'xx') } let!(:category) { Fabricate(:category, name: 'xx') }

View File

@ -148,6 +148,19 @@ describe Group do
GroupUser.count.should == original_count GroupUser.count.should == original_count
end 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 it "allows you to lookup a new group by name" do
group = Fabricate(:group) group = Fabricate(:group)
group.id.should == Group[group.name].id group.id.should == Group[group.name].id

View File

@ -814,4 +814,16 @@ describe Post do
end end
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 end