From ee71a3972dc27ee574b42ca9c8baca05fa8d3dde Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Fri, 13 Mar 2015 02:48:05 -0500 Subject: [PATCH] Cache admin settings; work on admin functionality. --- app/controllers/admin_controller.rb | 5 +++++ app/helpers/admin_helper.rb | 6 ++++++ app/models/admin_setting.rb | 12 +++++++++++- app/views/admin/index.html.erb | 21 ++++++++++++++++++++- app/views/layouts/application.html.erb | 20 ++++++++++---------- app/views/shared/_footer.html.erb | 14 +++++++------- app/views/shared/_header.html.erb | 2 +- app/views/tools/index.html.erb | 12 ++++++------ config/routes.rb | 7 ++++--- 9 files changed, 70 insertions(+), 29 deletions(-) diff --git a/app/controllers/admin_controller.rb b/app/controllers/admin_controller.rb index c2a9ad9..73ac166 100644 --- a/app/controllers/admin_controller.rb +++ b/app/controllers/admin_controller.rb @@ -1,4 +1,9 @@ class AdminController < ApplicationController def index end + + def update + # respond to form data... + render 'index' + end end diff --git a/app/helpers/admin_helper.rb b/app/helpers/admin_helper.rb index d5c6d35..16e5a87 100644 --- a/app/helpers/admin_helper.rb +++ b/app/helpers/admin_helper.rb @@ -1,2 +1,8 @@ module AdminHelper + SETTINGS = [ + {:key => :site_name, :label => 'Site name'}, + {:key => :corporation, :label => 'Corporation'}, + {:key => :description, :label => 'Home page description', :textarea => true}, + {:key => :copyright, :label => 'Copyright'}, + ] end diff --git a/app/models/admin_setting.rb b/app/models/admin_setting.rb index 9e4ab4b..668989c 100644 --- a/app/models/admin_setting.rb +++ b/app/models/admin_setting.rb @@ -1,6 +1,16 @@ class AdminSetting < ActiveRecord::Base def self.get(key) - self.find_by(key: key).value + Rails.cache.fetch("admin_setting/#{key}") do + self.find_by(key: key).value + end + end + + def self.set(key, value) + setting = self.find_by(key: key) + setting.update(value: value) + Rails.cache.write("admin_setting/#{key}") do + value + end end end diff --git a/app/views/admin/index.html.erb b/app/views/admin/index.html.erb index 10dadec..34a5e1e 100644 --- a/app/views/admin/index.html.erb +++ b/app/views/admin/index.html.erb @@ -1,4 +1,23 @@ <% provide(:title, 'Admin') %>

Admin#index

-

Find me in app/views/admin/index.html.erb

+ +

<%= link_to 'Home', controller: 'tools', action: 'index' %>

+ +<%= form_tag do %> + + <% AdminHelper::SETTINGS.each do |s| %> + + + <% if s[:textarea] %> + + <% else %> + + <% end %> + + <% end %> + + + +
<%= label_tag(s[:key], s[:label]) %><%= text_area_tag(s[:key], AdminSetting.get(s[:key].to_s), size: "60x5") %><%= text_field_tag(s[:key], AdminSetting.get(s[:key].to_s), size: 40) %>
<%= submit_tag('Update') %>
+<% end %> diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index d1a8cd1..124cc94 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -1,17 +1,17 @@ - <%= get_title(yield(:title)) %> - <%= stylesheet_link_tag 'application', media: 'all' %> - <%= javascript_include_tag 'application' %> - <%= csrf_meta_tags %> + <%= get_title(yield(:title)) %> + <%= stylesheet_link_tag 'application', media: 'all' %> + <%= javascript_include_tag 'application' %> + <%= csrf_meta_tags %> - <%= render "shared/header" %> - -
- <%= yield %> -
- <%= render "shared/footer" %> + <%= render "shared/header" %> + +
+ <%= yield %> +
+ <%= render "shared/footer" %> diff --git a/app/views/shared/_footer.html.erb b/app/views/shared/_footer.html.erb index 6ab1827..4e81975 100644 --- a/app/views/shared/_footer.html.erb +++ b/app/views/shared/_footer.html.erb @@ -1,9 +1,9 @@ diff --git a/app/views/shared/_header.html.erb b/app/views/shared/_header.html.erb index 4034bac..3417ce4 100644 --- a/app/views/shared/_header.html.erb +++ b/app/views/shared/_header.html.erb @@ -1,3 +1,3 @@ diff --git a/app/views/tools/index.html.erb b/app/views/tools/index.html.erb index 31b7d6c..3588faf 100644 --- a/app/views/tools/index.html.erb +++ b/app/views/tools/index.html.erb @@ -1,14 +1,14 @@

Tools#index

- Users: - <%= link_to "Signup", controller: "users", action: "create" %> - • - <%= link_to "Login", controller: "users", action: "login" %> + Users: + <%= link_to "Signup", controller: "users", action: "create" %> + • + <%= link_to "Login", controller: "users", action: "login" %>

- Admin: - <%= link_to "Admin", controller: "admin", action: "index" %> + Admin: + <%= link_to "Admin", controller: "admin", action: "index" %>

Tools: ...

diff --git a/config/routes.rb b/config/routes.rb index 5b723ba..9931e04 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,10 +1,11 @@ Rails.application.routes.draw do root 'tools#index' - get '/signup' => 'users#create' - get '/login' => 'users#login' + get '/signup' => 'users#create' + get '/login' => 'users#login' - get '/admin' => 'admin#index' + get '/admin' => 'admin#index' + post '/admin' => 'admin#update' # Example of named route that can be invoked with purchase_url(id: product.id) # get 'products/:id/purchase' => 'catalog#purchase', as: :purchase