GithubHelp home page GithubHelp logo

pocake / rails-settings-cached Goto Github PK

View Code? Open in Web Editor NEW

This project forked from huacnlee/rails-settings-cached

0.0 9.0 0.0 221 KB

This is improved from rails-settings, added caching for all settings

Home Page: http://huacnlee.com/rails-settings-cached/

License: Other

Ruby 100.00%

rails-settings-cached's Introduction

Rails Settings Cached

This is improved from rails-settings, added caching for all settings. Settings is a plugin that makes managing a table of global key, value pairs easy. Think of it like a global Hash stored in your database, that uses simple ActiveRecord like methods for manipulation. Keep track of any global setting that you dont want to hard code into your rails app. You can store any kind of object. Strings, numbers, arrays, or any object.

Status

Gem Version CI Status Code Climate codecov.io

Setup

Edit your Gemfile:

gem "rails-settings-cached"

Generate your settings:

$ rails g settings:install

If you want custom model name:

$ rails g settings:install SiteConfig

Now just put that migration in the database with:

rake db:migrate

Usage

The syntax is easy. First, lets create some settings to keep track of:

Setting.admin_password = 'supersecret'
Setting.date_format    = '%m %d, %Y'
Setting.cocktails      = ['Martini', 'Screwdriver', 'White Russian']
Setting.foo            = 123
Setting.credentials    = { :username => 'tom', :password => 'secret' }

Now lets read them back:

Setting.foo            # returns 123

Changing an existing setting is the same as creating a new setting:

Setting.foo = 'super duper bar'

Decide you dont want to track a particular setting anymore?

Setting.destroy :foo
Setting.foo            # returns nil

Want a list of all the settings?

Setting.get_all

You need name spaces and want a list of settings for a give name space? Just choose your prefered named space delimiter and use Setting.get_all (Settings.all for # Rails 3.x and 4.0.x) like this:

Setting['preferences.color'] = :blue
Setting['preferences.size'] = :large
Setting['license.key'] = 'ABC-DEF'
# Rails 4.1.x
Setting.get_all('preferences.')
# Rails 3.x and 4.0.x
Setting.all('preferences.')
# returns { 'preferences.color' => :blue, 'preferences.size' => :large }

Extend a model

Settings may be bound to any existing ActiveRecord object. Define this association like this: Notice! is not do caching in this version.

class User < ActiveRecord::Base
  include RailsSettings::Extend
end

Then you can set/get a setting for a given user instance just by doing this:

user = User.find(123)
user.settings.color = :red
user.settings.color # returns :red
user.settings.get_all
# { "color" => :red }

If you want to find users having or not having some settings, there are named scopes for this:

User.with_settings
# => returns a scope of users having any setting

User.with_settings_for('color')
# => returns a scope of users having a 'color' setting

User.without_settings
# returns a scope of users having no setting at all (means user.settings.get_all == {})

User.without_settings('color')
# returns a scope of users having no 'color' setting (means user.settings.color == nil)

Default settings

Sometimes you may want define default settings.

RailsSettings has generate a config YAML file in:

# config/app.yml
defaults: &defaults
  github_token: "123456"
  twitter_token: "<%= ENV["TWITTER_TOKEN"] %>"
  foo:
    bar: "Foo bar"

development:
  <<: *defaults

test:
  <<: *defaults

production:
  <<: *defaults

And you can use by Setting model:

Setting.github_token
=> "123456"
Setting.github_token = "654321"
# Save into database.
Setting.github_token
# Read from databae / caching.
=> "654321"
Setting['foo.bar']
=> 'Foo bar'

NOTE: YAML setting it also under the cache scope, when you restart Rails application, cache will expire, so when you want change default config, you need restart Rails application server.

Caching flow:

Setting.foo -> Check Cache -> Exist - Write Cache -> Return
                   |
                Check DB -> Exist -> Write Cache -> Return
                   |
               Check Default -> Exist -> Write Cache -> Return
                   |
               Return nil

Change cache key

class Setting < RailsSettings::Base
  cache_prefix { 'you-prefix' }
  ...
end

How to create a list, form to manage Settings?

If you want create an admin interface to editing the Settings, you can try methods in follow:

config/routes.rb

namespace :admin do
  resources :settings
end

app/controllers/admin/settings_controller.rb

module Admin
  class SettingsController < ApplicationController
    before_action :get_setting, only: [:edit, :update]

    def index
      @settings = Setting.get_all
    end

    def edit
    end

    def update
      if @setting.value != params[:setting][:value]
        @setting.value = params[:setting][:value]
        @setting.save
        redirect_to admin_settings_path, notice: 'Setting has updated.'
      else
        redirect_to admin_settings_path
      end
    end

    def get_setting
      @setting = Setting.find_by(var: params[:id]) || Setting.new(var: params[:id])
    end
  end
end

app/views/admin/settings/index.html.erb

<table>
  <tr>
    <th>Key</th>
    <th></th>
  </tr>
  <% @settings.each_key do |key| %>
  <tr>
    <td><%= key %></td>
    <td><%= link_to 'edit', edit_admin_setting_path(key) %></td>
  </tr>
  <% end %>
</table>

app/views/admin/settings/edit.html.erb

<%= form_for(@setting, url: admin_setting_path(@setting.var), method: 'patch') do |f| %>
  <label><%= @setting.var %></label>
  <%= f.text_area :value, rows: 10 %>
  <%= f.submit %>
<% end %>

Also you may use rails-settings-ui gem for building ready to using interface with validations, or activeadmin_settings_cached gem if you use activeadmin.

Use case:

rails-settings-cached's People

Contributors

accessd avatar adambutler avatar alexanderadam avatar alexjwayne avatar artemave avatar artofhuman avatar dangerous avatar hovsater avatar huacnlee avatar huobazi avatar justin-natance avatar justin808 avatar kiela avatar kingwkb avatar ledermann avatar masolin avatar merqlove avatar mgrachev avatar miks avatar mlandauer avatar ncreuschling avatar paxa avatar phlipper avatar pmq20 avatar qichunren avatar stlv avatar tcmacdonald avatar tooooolong avatar tsipiniuk avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  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.