Skip to content

brings some document-oriented behavior into ActiveRecord

Notifications You must be signed in to change notification settings

nicck/serialized_attributes

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 

Repository files navigation

serialized_attributes

This is a very cool lib, allows to define dynamic serialized fields in your AR model, which acts as normal rails db columns. It give you full power of rails to your virtual columns: validations, rails forms, error messages, type casting (integer, date/time), type safety, :dirty? s, etc.. STI is supported! Basic associations is working too!

Example of usage

STI table def

create_table :documents do |t|
  t.string :type
  t.text :serialized_attributes     # <---  here all your dynamic fields will be saved
  t.integer :reference_id           # <---  you can also define any sql columns for your indexes
  t.timestamps
end

STI base model

class Document < ActiveRecord::Base
  include SerializedAttributes
end

Other models..

class Post < Document
  attribute :title, String
  attribute :body,  String
  attribute :is_published, Boolean, :default => false
  
  attribute :comment_ids, Array             # <--- serialized Array of ids of associated comments
  has_references_to :comments
  
  validates_presence_of :title, :body
end

class Comment < Document
  attribute :body, String, :requied => true # <--- validates_presence_of :body
  attribute :post_id, Integer
  belongs_to :post
end

IRB fun

post = Post.create(:title => "First Post", :body => "Lorem ...")
assert !post.new_record?
post.comments = [Comment.create(:body => "this is a comment")]
post.comments << Comment.create(:body => "this is second comment")
assert_equal Comment.all.map(&:id), post.comment_ids
post.save
assert post.reload.comments.length == 2

Limitations

  • has-references-to association dont update reverse association;
  • serialized-attributes column saved every time you call :save without depending on what is actually changed.

About

brings some document-oriented behavior into ActiveRecord

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages