Coder Social home page Coder Social logo

flexi-model's Introduction

flexi-model

Code Climate

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: '[email protected]', 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: '[email protected]'

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

flexi-model's People

Contributors

we4tech avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

thehungrycoder

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.