ruby-on-rails – How do I use the rails-settings-cached gem?

Question:

Hello everyone. Has anyone used the rails-settings-cached gem ( https://github.com/huacnlee/rails-se…er/README.md )?

I have the following questions and problems in this regard. (all questions are based on the gem guide, link above)

  1. I have Rails v.4.2.1. which version of the gem do I need to use "rails-settings-cached", "0.4.1" for Rails 4.1.x or "rails-settings-cached", "0.3.1" for Rails 4+ ??

  2. After installing the gem, generate settings:

    $ rails g settings SettingsCompany

this will only create the settings_company.rb model and migration to create the db table. Those. to make views for editing settings by the application administrator, do you need to manually create a controller?

  1. Omitting Question 2, let's say I create a controller for Settings. Will it matter what I call it?

so

rails g controller Settings

we have (based on the guide):

class SettingsController < ApplicationController
 
  def index
    # to get all items for render list
    @settings = Setting.all
  end
 
  def edit
    @setting = Setting.unscoped.find(params[:id])
    end
  end

and when I try to navigate to settings / index.erb I get an error

NameError in SettingsController # index uninitialized constant

SettingsController :: Setting

in line

@settings = Setting.unscoped

and what is and why is "unscoped"?

  1. And I don't quite understand how gem works

to enter data into the database, no manipulations with migrations are needed? and is it enough to just write something like this in the code ?:

Setting.admin_password = 'supersecret'

and this will create a record in the table with values

var = admin_password value = supersecret

??

Thank you

Answer:

1 "0.4.1" for Rails 4.1.x

2 Yes, the controller and views are on their own.

3 It should be like this:

class SettingsController < ApplicationController

  def index
    # to get all items for render list
    @settings = SettingsCompany.get_all 
  end
end

unscoped cancels all scopes. Scopes are predefined filters for a class:

class Shirt < ActiveRecord::Base
  scope :red, -> { where(color: 'red') }
end

Thus, one can call Shirt.red and get a collection of red shirts.
In the guide, unscoped is used to cancel some internal scopes that are imposed in the gem itself.

4 Yes, enough

Setting.admin_password = 'supersecret'

this magic works through method_missing

Scroll to Top