flexi-model
Build flexible database model with dynamic fields (right now based on ActiveRecord soon it will work with mongoid too)
How to do ?
Define your first model.
class User
include FlexiModel
_string :name, :email
_text :bio
validates_presence_of :name, :email
end
Create your new record.
User.create name: 'hasan', email: 'hasan@welltreat.us', bio: 'Ruby developer'
#=> #<User:...>
Find record by id.
User.find(1)
Find records by name
User.where(name: 'hasan')
#=> #<...::Criteria...> # Instance of criteria object
Define belongs to and has many relationship
class Blog
include FlexiModel
_string :title
_text :content
belongs_to :user
end
class User
include FlexiModel
_string :name
has_many :blogs
end
# Create records
user = User.create(name: 'nafi')
Blog.create(title: 'Hello world', content: 'Hello content', user: user)
# Find all related blogs
user.blogs
# Find parent record
user.blogs.first.user
Define has and belongs to many relationships
class User
inlude FlexiModel
_string :name
has_and_belongs_to_many :roles
end
class Role
include FlexiModel
_string :name
has_and_belongs_to_many :users
end
# Create user with roles
User.create(name: 'khan', roles: [Role.create(name: 'admin'), Role.create(name: 'moderator')])
# Find user roles
user = User.where(name: 'khan').first
user.roles
Update attribute
user = User.where(...)
user.update_attribute :name, 'raju'
Update attributes
user = User.where(...)
user.update_attributes name: 'raju', email: 'hola@hola.com'
Destroy record
user.destroy
User.where(...conditions...).destroy_all
Observers
- Before, After and Around Create
- Before, After and Around Update
- Before, After and Around Destroy
TODOS
- Write documentation