{"id":13463096,"url":"https://github.com/ledermann/rails-settings","last_synced_at":"2025-05-13T19:04:05.217Z","repository":{"id":570829,"uuid":"202444","full_name":"ledermann/rails-settings","owner":"ledermann","description":"Manage settings with Ruby on Rails","archived":false,"fork":false,"pushed_at":"2024-12-29T10:25:19.000Z","size":312,"stargazers_count":1018,"open_issues_count":23,"forks_count":138,"subscribers_count":24,"default_branch":"master","last_synced_at":"2025-04-26T11:37:58.560Z","etag":null,"topics":["configuration","rails","ruby","rubygems","settings"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ledermann.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"MIT-LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2009-05-16T07:00:46.000Z","updated_at":"2025-04-19T19:32:45.000Z","dependencies_parsed_at":"2023-12-26T07:27:02.697Z","dependency_job_id":"60e3b70a-f0c0-4cd6-9fdf-cb38f0d40a37","html_url":"https://github.com/ledermann/rails-settings","commit_stats":{"total_commits":289,"total_committers":25,"mean_commits":11.56,"dds":"0.23875432525951557","last_synced_commit":"dd24f15f909c7e00dd10b67ee0bd4a839d533d22"},"previous_names":[],"tags_count":21,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ledermann%2Frails-settings","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ledermann%2Frails-settings/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ledermann%2Frails-settings/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ledermann%2Frails-settings/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ledermann","download_url":"https://codeload.github.com/ledermann/rails-settings/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251086816,"owners_count":21534131,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["configuration","rails","ruby","rubygems","settings"],"created_at":"2024-07-31T13:00:45.923Z","updated_at":"2025-04-27T04:25:31.792Z","avatar_url":"https://github.com/ledermann.png","language":"Ruby","readme":"# Settings for Rails\n\n[![Build Status](https://github.com/ledermann/rails-settings/actions/workflows/main.yml/badge.svg)](https://github.com/ledermann/rails-settings/actions)\n[![Code Climate](https://codeclimate.com/github/ledermann/rails-settings.svg)](https://codeclimate.com/github/ledermann/rails-settings)\n[![Coverage Status](https://coveralls.io/repos/ledermann/rails-settings/badge.svg?branch=master)](https://coveralls.io/r/ledermann/rails-settings?branch=master)\n\nRuby gem to handle settings for ActiveRecord instances by storing them as serialized Hash in a separate database table. Namespaces and defaults included.\n\n## Requirements\n\n- Ruby 2.7 or newer\n- Rails 6.1 or newer (including Rails 7.0)\n\n## Installation\n\nInclude the gem in your Gemfile and run `bundle` to install it:\n\n```ruby\ngem 'ledermann-rails-settings'\n```\n\nGenerate and run the migration:\n\n```shell\nrails g rails_settings:migration\nrake db:migrate\n```\n\n## Usage\n\n### Define settings\n\n```ruby\nclass User \u003c ActiveRecord::Base\n  has_settings do |s|\n    s.key :dashboard, :defaults =\u003e { :theme =\u003e 'blue', :view =\u003e 'monthly', :filter =\u003e false }\n    s.key :calendar,  :defaults =\u003e { :scope =\u003e 'company'}\n  end\nend\n```\n\nIf no defaults are needed, a simplified syntax can be used:\n\n```ruby\nclass User \u003c ActiveRecord::Base\n  has_settings :dashboard, :calendar\nend\n```\n\nEvery setting is handled by the class `RailsSettings::SettingObject`. You can use your own class, e.g. for validations:\n\n```ruby\nclass Project \u003c ActiveRecord::Base\n  has_settings :info, :class_name =\u003e 'ProjectSettingObject'\nend\n\nclass ProjectSettingObject \u003c RailsSettings::SettingObject\n  validate do\n    unless self.owner_name.present? \u0026\u0026 self.owner_name.is_a?(String)\n      errors.add(:base, \"Owner name is missing\")\n    end\n  end\nend\n```\n\nIn case you need to define settings separatedly for the same models, you can use the persistent option\n\n```ruby\nmodule UserDashboardConcern\n  extend ActiveSupport::Concern\n\n  included do\n    has_settings persistent: true do |s|\n      s.key :dashboard\n    end\n  end\nend\n\nclass User \u003c ActiveRecord::Base\n  has_settings persistent: true do |s|\n    s.key :calendar\n  end\nend\n```\n\n### Set settings\n\n```ruby\nuser = User.find(1)\nuser.settings(:dashboard).theme = 'black'\nuser.settings(:calendar).scope = 'all'\nuser.settings(:calendar).display = 'daily'\nuser.save! # saves new or changed settings, too\n```\n\nor\n\n```ruby\nuser = User.find(1)\nuser.settings(:dashboard).update! :theme =\u003e 'black'\nuser.settings(:calendar).update! :scope =\u003e 'all', :display =\u003e 'daily'\n```\n\n### Get settings\n\n```ruby\nuser = User.find(1)\nuser.settings(:dashboard).theme\n# =\u003e 'black\n\nuser.settings(:dashboard).view\n# =\u003e 'monthly'  (it's the default)\n\nuser.settings(:calendar).scope\n# =\u003e 'all'\n```\n\n### Delete settings\n\n```ruby\nuser = User.find(1)\nuser.settings(:dashboard).update! :theme =\u003e nil\n\nuser.settings(:dashboard).view = nil\nuser.settings(:dashboard).save!\n```\n\n### Using scopes\n\n```ruby\nUser.with_settings\n# =\u003e all users having any setting\n\nUser.without_settings\n# =\u003e all users without having any setting\n\nUser.with_settings_for(:calendar)\n# =\u003e all users having a setting for 'calendar'\n\nUser.without_settings_for(:calendar)\n# =\u003e all users without having settings for 'calendar'\n```\n\n### Eager Loading\n\n```ruby\nUser.includes(:setting_objects)\n# =\u003e Eager load setting_objects when querying many users\n```\n\n## Compatibility\n\nVersion 2 is a complete rewrite and has a new DSL, so it's **not** compatible with Version 1. In addition, Rails 2.3 is not supported anymore. But the database schema is unchanged, so you can continue to use the data created by 1.x, no conversion is needed.\n\nIf you don't want to upgrade, you find the old version in the [1.x](https://github.com/ledermann/rails-settings/commits/1.x) branch. But don't expect any updates there.\n\n## Changelog\n\nSee https://github.com/ledermann/rails-settings/releases\n\n## License\n\nMIT License\n\nCopyright (c) 2012-2024 [Georg Ledermann](https://ledermann.dev)\n\nThis gem is a complete rewrite of [rails-settings](https://github.com/Squeegy/rails-settings) by [Alex Wayne](https://github.com/Squeegy)\n","funding_links":[],"categories":["Developer Tools","Ruby"],"sub_categories":["Configuration Management"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fledermann%2Frails-settings","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fledermann%2Frails-settings","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fledermann%2Frails-settings/lists"}