From 81acdd9067adde2dd6a1ea2d6ec36f452150a37d Mon Sep 17 00:00:00 2001 From: uu59 Date: Thu, 19 Jun 2014 16:56:12 +0900 Subject: [PATCH] Refactoring --- .gitignore | 4 ++-- app/controllers/application_controller.rb | 2 +- app/controllers/fluentd_controller.rb | 9 ++------- app/controllers/tutorials_controller.rb | 3 +-- app/models/fluentd.rb | 17 +++++------------ app/models/user.rb | 2 +- app/views/fluentd/new.html.haml | 2 +- .../fluentd/{index.html.haml => show.html.haml} | 0 app/views/tutorials/index.html.haml | 2 +- config/locales/translation_en.yml | 2 +- config/locales/translation_ja.yml | 2 +- config/routes.rb | 2 +- 12 files changed, 17 insertions(+), 30 deletions(-) rename app/views/fluentd/{index.html.haml => show.html.haml} (100%) diff --git a/.gitignore b/.gitignore index 2191f3b..e01fb2a 100644 --- a/.gitignore +++ b/.gitignore @@ -26,5 +26,5 @@ /Gemfile.plugins -/db/user.txt -/db/fluentd.json +/db/*-user.txt +/db/*-fluentd.json diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 4e194a8..650d2aa 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -46,7 +46,7 @@ class ApplicationController < ActionController::Base private def find_fluentd - @fluentd = Fluentd.find(params[:fluentd_id]) + @fluentd = Fluentd.factory end def set_locale diff --git a/app/controllers/fluentd_controller.rb b/app/controllers/fluentd_controller.rb index 8f64b8c..c9a1f9c 100644 --- a/app/controllers/fluentd_controller.rb +++ b/app/controllers/fluentd_controller.rb @@ -1,13 +1,8 @@ class FluentdController < ApplicationController before_action :find_fluentd, only: [:edit, :update, :destroy] - def index - @fluentds = Fluentd.all - end - def show - @fluentds = Fluentd.all - render "index" + @fluentds = [Fluentd.factory].compact end def new @@ -44,7 +39,7 @@ class FluentdController < ApplicationController private def find_fluentd - @fluentd = Fluentd.find(params[:id]) + @fluentd = Fluentd.factory end def fluentd_params diff --git a/app/controllers/tutorials_controller.rb b/app/controllers/tutorials_controller.rb index 1b4b775..c80cdbe 100644 --- a/app/controllers/tutorials_controller.rb +++ b/app/controllers/tutorials_controller.rb @@ -27,8 +27,7 @@ class TutorialsController < ApplicationController end def find_fluentd - # NOTE: use first fluentd for tutorial - @fluentd = Fluentd.first + @fluentd = Fluentd.factory end def check_ready diff --git a/app/models/fluentd.rb b/app/models/fluentd.rb index fe1784d..ae027fc 100644 --- a/app/models/fluentd.rb +++ b/app/models/fluentd.rb @@ -116,17 +116,7 @@ class Fluentd end - def self.exists? - File.exists?(JSON_PATH) - end - - def self.all - [factory].compact - end - - def self.find(id) - factory - end + # ActiveRecord mimic def self.factory return unless exists? @@ -134,6 +124,10 @@ class Fluentd Fluentd.new(attr) end + def self.exists? + File.exists?(JSON_PATH) + end + def update_attributes(params) params.each_pair do |k, v| send("#{k}=", v) @@ -142,7 +136,6 @@ class Fluentd def save return false unless valid? - self.id = 1 json = COLUMNS.inject({}) do |result, col| result[col] = send(col) result diff --git a/app/models/user.rb b/app/models/user.rb index f91e9e7..648bf83 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -10,7 +10,7 @@ class User has_secure_password - ENCRYPTED_PASSWORD_FILE = Rails.env.test? ? "tmp/test.txt" : Rails.root + "db/user.txt" + ENCRYPTED_PASSWORD_FILE = Rails.root + "db/#{Rails.env}-user.txt" attr_accessor :name, :password, :password_confirmation, :current_password diff --git a/app/views/fluentd/new.html.haml b/app/views/fluentd/new.html.haml index 95c4d3a..466e3cb 100644 --- a/app/views/fluentd/new.html.haml +++ b/app/views/fluentd/new.html.haml @@ -1,3 +1,3 @@ - page_title t('.page_title') -= render partial: "form", locals: { btn: t(".create"), url: fluentd_index_path, method: :post } += render partial: "form", locals: { btn: t(".create"), url: fluentd_path, method: :post } diff --git a/app/views/fluentd/index.html.haml b/app/views/fluentd/show.html.haml similarity index 100% rename from app/views/fluentd/index.html.haml rename to app/views/fluentd/show.html.haml diff --git a/app/views/tutorials/index.html.haml b/app/views/tutorials/index.html.haml index 5e9840e..9cb0621 100644 --- a/app/views/tutorials/index.html.haml +++ b/app/views/tutorials/index.html.haml @@ -10,7 +10,7 @@ = t('.step1') - else = icon('fa-warning text text-danger') - = link_to t('.step1'), fluentd_index_path + = link_to t('.step1'), fluentd_path %li - if @fluentd && @fluentd.agent.running? = icon('fa-check text text-success') diff --git a/config/locales/translation_en.yml b/config/locales/translation_en.yml index 82b5b25..2b4c38d 100644 --- a/config/locales/translation_en.yml +++ b/config/locales/translation_en.yml @@ -89,7 +89,7 @@ en: finish: Update config form: <<: *fluentd_common - index: + show: <<: *fluentd_common page_title: fluentd new: diff --git a/config/locales/translation_ja.yml b/config/locales/translation_ja.yml index 07b2a79..d86eb7e 100644 --- a/config/locales/translation_ja.yml +++ b/config/locales/translation_ja.yml @@ -89,7 +89,7 @@ ja: finish: 設定する form: <<: *fluentd_common - index: + show: <<: *fluentd_common page_title: fluentd new: diff --git a/config/routes.rb b/config/routes.rb index 906b87f..4e70f26 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,5 +1,5 @@ Rails.application.routes.draw do - root "fluentd#index" # TODO: change to dashboard + root "fluentd#show" # TODO: change to dashboard resource :fluentd, controller: :fluentd do resource :agent, only: [:show], module: :fluentd do