Giter Site home page Giter Site logo

liooo / dynabute Goto Github PK

View Code? Open in Web Editor NEW
18.0 4.0 1.0 62 KB

DYNAmic attriBUTEs for ActiveRecord

License: MIT License

Ruby 89.92% JavaScript 0.69% CSS 1.10% HTML 8.29%
rails activerecord dynamic-attributes dynamic-schema dynamic-model relational-databases

dynabute's Introduction

Dynabute

Dynamically add attributes on Relational Database backed ActiveRecord, without hash serialization and bullshits.

Try messing with a working demo.

Usage

First, enable dynabute

class User < ActiveRecord::Base
  has_dynabutes
end

then add some field definitions

User.dynabutes.create( name: 'age', value_type: 'integer' )
User.dynabutes.create( name: 'skinny', value_type: 'boolean' )
User.dynabutes.create( name: 'personalities', value_type: 'string', has_many: true )

now set value

user = User.create

user.build_dynabute_value( name: 'age', value: 35 ).save
# => <Dynabute::Values::IntegerValue:0x007faba5279540 id: 1, field_id: 1, dynabutable_id: 1, dynabutable_type: "User", value: 35>

check the value

user.dynabute_value( name: 'age' ).value
# => 35

values can also be referenced by dynabute_<field name>_value(s)

user.dynabute_age_value.value
# => 35

nested attributes of glory

personality_field_id = User.dynabutes.find_by( name: 'personalities' ).id
user.update(
  dynabute_values_attributes: [
    { name: 'age', value: 36 }, #=> tell us which field to update by `name:`
    { name: 'skinny', value: false },
    { field_id: personality_field_id, value: 'introverted' }, # or by `field_id:`
    { field_id: personality_field_id, value: 'stingy' }
  ]
)

select value_type is also available

User.dynabutes.create( name: 'gender', value_type: 'select', options_attributes: [ { label: 'male' }, { label: 'female' } ] )
User.dynabutes.create( name: 'hobbies', has_many: true, value_type: 'select',  options_attributes: [ { label: 'running' }, { label: 'swimming' }, { label: 'hiking' } ] )

list the available options for a field

User.dynabutes.find_by(name: gender).options
# => [#<Dynabute::Option:0x007ff53e2e1f90 id: 1, field_id: 4, label: "male">,
#     #<Dynabute::Option:0x007ff53e2e1568 id: 2, field_id: 4, label: "female">]

set value

male = User.dynabutes.find_by(name: 'gender').options.find_by(label: 'male')
hobbies = User.dynabutes.find_by(name: 'hobbies').options

user.update(dynabute_values_attributes: [
 { name: 'gender', value: male.id },
 { name: 'hobbies', value: hobbies[0].id },
 { name: 'hobbies', value: hobbies[1].id }
])

check out the selected options

user.dynabute_value(name: 'gender').option
# => <Dynabute::Option:0x007ff53e2e1f90 id: 1, field_id: 4, label: "male">,

user.dynabute_value(name: 'hobbies').map(&:option)
# => [#<Dynabute::Option:0x007fb26c4467d8 id: 5, field_id: 5, label: "running">,
#     #<Dynabute::Option:0x007fb26c446238 id: 6, field_id: 5, label: "swimming">]

Installation

Add this line to your application's Gemfile:

gem 'dynabute'

And then execute:

$ bundle install
$ rails generate dynabute:install
$ rake db:migrate

Contributing

yea?

License

The gem is available as open source under the terms of the MIT License.

dynabute's People

Contributors

crash1101 avatar liooo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

crash1101

dynabute's Issues

Integrate with active admin

Hey, I found this gem a great and pleasant piece of work that is precise as I want.
My rails application is built on active admin and I want to add dynamic attributes for some models in my active admin.

But I found no any way to integrate this gem with active admin as I'm a newbie on rails. Do you have any idea about this?

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.