Project

setsy

0.05
No commit activity in last 3 years
No release in over 3 years
Settings for your classes
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.15
~> 0.11.3
>= 3.0
~> 10.0
~> 3.0

Runtime

 Project Readme

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.