0
votes

I'm using rails-settings by Squeegy from https://github.com/Squeegy/rails-settings as well as Activeadmin. What I'm trying to accomplish is making a form in ActiveAdmin that I can let the site admin change the settings for the site, which take a command line syntax of:

Setting.foo = "bar"
Setting.site_title = "My Awesome Site!"
Setting.max_users = 35

I really don't think I've got too far, but I'm already stuck. I'm up to the point of having a custom ActiveAdmin form made:

ActiveAdmin.register_page "Settings" do
  action_item do
    link_to "View Site", "/"
  end


  content do
    form do |f|
      #Inputs for Settings 
    end
  end
end

But I don't even know how to begin laying out the form to directly access the Settings model, or how to make a custom controller to handle the input. I suppose if I could get the input sent to a controller that I could make, I'd be just fine.

2

2 Answers

2
votes

This is very simple to do with ActiveAdmin. Lets say your settings class is Settings :

ActiveAdmin.register_page "Settings" do
  content do
    table :class => 'settings' do
      thead do
        th 'Setting'
        th 'Value'
        th ''
      end
      Settings.all.each do |key, val|
        tr do
          td strong key
          td val
          td do
            link_to "delete", admin_settings_delete_path( :key => key ), :method => :post
          end
        end
      end
      tr do
        form :action => admin_settings_create_path, :method => :post do
          td do
            input :name => 'key'
          end
          td do
            input :name => 'val'
          end
          td do
            input :type => 'submit', :value => 'Add'
          end
        end
      end
    end
  end

  page_action :create, :method => :post do
    Settings[params[:key]] = params[:val]
    redirect_to :back, :notice => "#{params[:key]} added"
  end

  page_action :delete, :method => :post do
    Settings.destroy params[:key]
    redirect_to :back, :notice => "#{params[:key]} deleted"
  end
end

Of course you'll need to add some CSS and maybe some validations but you have your settings page.

Edit: Note that I wrote this for rails-settings-cached, not rails-settings, but my quick search led here so I guess this could still help someone.

1
votes

I don't think you want your site's form to directly change the settings in ActiveAdmin, I would ...

  1. Create a new table, eg. adminsettings and add fields for each of the settings you want to store for instance site_title, alternatively you could use each row for a setting which means you can add new settings in the future without changing the database

  2. Put together a form in Activeadmin to maintain your settings

  3. Add some functions to your model to grab the settings so you can do something like ..

    Setting.site_title = Adminsetting.getsitetitle

You could be clever with your model method and use the method_missing facility so you need the least amount of code to get a setting ...

class << self 
  def method_missing(method, *args, &block)
    setting = Adminsetting.where(:code => method.to_s).first
    if setting
      return setting.content
    else
      return super(method, *args, &block)
    end
 end

Perhaps you could package this into a Gem as it could be a useful thing for others.