Giter Club home page Giter Club logo

serial_preference's Introduction

SerialPreference

If you have a large number of settings/preferences on your model (like a company or a businesss) and you store each preference in a separate model or in separate columns on the model itself, it gets hairy, quickly.

Additionally you require those settings/preferences to be read in from a form and then you need an easy way to validate them too.

Personally, I found that putting settings in the database relationally was hellish.

SerialPreference stores preferences serialized in a hash in your model. All in one place with a DSL to define your settings along with validations and other niceties.

Scratching my own itch.

Installation

Add this line to your application's Gemfile:

gem 'serial_preference'

And then execute:

$ bundle

Or install it yourself as:

$ gem install serial_preference

Usage

    class Company < ActiveRecord::Base

      include SerialPreference::HasSerialPreferences

      preferences do

        preference :taxable data_type: :boolean, required: true
        preference :vat_no required: false
        preference :max_invoice_items data_type: :integer

        float :rate_of_interest

        # default data type is :string
        # if the preference is required, then a validation is added to the model
        # if the data type is numerical, then a numericality validation is added
        # preferences can be grouped in preference groups

        preference_group "Preferred Ledgers" do
          income_ledger_id data_type: :integer, default: 1
        end

        password field_type: :password

      end

    end
  • Fetching Preference/Groups
    # something you can customize in your form perhaps?
    Company.preference_groups.each do |pg| # => returns an array of preference groups
      # pg.name => Name of Preference Group as specified in map e.g. Preferred Ledgers
      # pg.preferences => Array of Preference Definitions
      pg.preferences.each do |preference|
        # preference => PreferenceDefinition
        f.input preference.name, required: preference.required?, placeholder: preference.default, as: preference.field_type
      end
    end
    # List of Preferences
    Company.preference_names # => [:income_ledger_id]

## Contributing

1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request

serial_preference's People

Contributors

asanghi avatar ronakjangir47 avatar amitsuroliya avatar

Watchers

James Cloos avatar  avatar

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.