Code Monkey home page Code Monkey logo

setsy's Introduction

Setsy

Settings for your classes. Depends on ActiveModel 3.0+.

Obligatory blog post

Here.

Note

This is not very tidy as it is. But it's battle-tested.

You want this if...

You want this if you don't want to have a zillion columns per model to manage your settings.

Installation

Add this line to your application's Gemfile:

gem 'setsy'

And then execute:

$ bundle

Or install it yourself as:

$ gem install setsy

Usage

First, create a migration for a JSONB column. Call it settings_data, or something. Just don't call it settings if you want to be able to call settings on an instance. Default of {}

In app/models/user.rb:

class User < ApplicationRecord
  
  include ::Setsy::DSL
  
  # each setting can be a hash, like posts_limit, or just
  DEFAULT_SETTINGS = {
    posts_limit: { value: 10 },
    favorite_color: 'blue'
  }.freeze

  # do some stuff.
  # setsy <attribute_name>, <options>, <block of readers>
  setsy :settings, column: :settings_data, defaults: DEFAULT_SETTINGS do |conf|
    conf.reader :posts_limit_and_color do
      "posts limit is #{posts_limit} and color is #{favorite_color}"
    end
  end
end

These attributes will become available on User#settings, and will be backed by the settings_data column which you created as a JSONB column.

Then in your controller or view or something,

<% @user = User.first %>
<% if @user.settings.posts_limit.default? %>
Posts limit is default. 
<% else %>
Posts limit is <%= @user.settings.posts_limit %> 
<% end %>
<%= @user.settings.posts_limit_and_color %> 

Testing

See spec

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/joshmn/setsy.

setsy's People

Contributors

joshmn avatar

Stargazers

 avatar

Watchers

 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.