From 508956f3b4a959a199a0d87399391508fd3c8349 Mon Sep 17 00:00:00 2001 From: uu59 Date: Tue, 13 May 2014 15:31:55 +0900 Subject: [PATCH 01/12] Add sign in process --- app/controllers/application_controller.rb | 9 +++++++ app/controllers/sessions_controller.rb | 28 ++++++++++++++++++++ app/controllers/users_controller.rb | 2 ++ app/helpers/sessions_helper.rb | 2 ++ app/helpers/users_helper.rb | 2 ++ app/models/user.rb | 10 +++++++ app/views/sessions/new.html.haml | 7 +++++ app/views/shared/_error.html.haml | 3 +++ config/routes.rb | 7 +++++ db/migrate/20140513053102_create_users.rb | 11 ++++++++ db/schema.rb | 24 +++++++++++++++++ db/seeds.rb | 2 ++ spec/controllers/sessions_controller_spec.rb | 5 ++++ spec/controllers/users_controller_spec.rb | 5 ++++ spec/helpers/sessions_helper_spec.rb | 15 +++++++++++ spec/helpers/users_helper_spec.rb | 15 +++++++++++ spec/models/user_spec.rb | 5 ++++ 17 files changed, 152 insertions(+) create mode 100644 app/controllers/sessions_controller.rb create mode 100644 app/controllers/users_controller.rb create mode 100644 app/helpers/sessions_helper.rb create mode 100644 app/helpers/users_helper.rb create mode 100644 app/models/user.rb create mode 100644 app/views/sessions/new.html.haml create mode 100644 app/views/shared/_error.html.haml create mode 100644 db/migrate/20140513053102_create_users.rb create mode 100644 db/schema.rb create mode 100644 spec/controllers/sessions_controller_spec.rb create mode 100644 spec/controllers/users_controller_spec.rb create mode 100644 spec/helpers/sessions_helper_spec.rb create mode 100644 spec/helpers/users_helper_spec.rb create mode 100644 spec/models/user_spec.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index d83690e..2bd0274 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -2,4 +2,13 @@ class ApplicationController < ActionController::Base # Prevent CSRF attacks by raising an exception. # For APIs, you may want to use :null_session instead. protect_from_forgery with: :exception + + def current_user + return unless session[:remember_token] + @current_user ||= User.find_by(remember_token: session[:remember_token]) + end + + def login_require + !!current_user + end end diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb new file mode 100644 index 0000000..04ac3d0 --- /dev/null +++ b/app/controllers/sessions_controller.rb @@ -0,0 +1,28 @@ +class SessionsController < ApplicationController + def create + user = User.find_by(name: session_params[:name]).try(:authenticate, session_params[:password]) + unless user + # TODO: i18n + flash.now[:notice] = I18n.t(:login_failed) + return render :new + end + sign_in user + redirect_to root_path + end + + def destroy + end + + private + + def session_params + params.require(:session).permit(:name, :password) + end + + def sign_in(user) + token = user.generate_remember_token + session[:remember_token] = token + user.update_attribute(:remember_token, token) + user + end +end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb new file mode 100644 index 0000000..3e74dea --- /dev/null +++ b/app/controllers/users_controller.rb @@ -0,0 +1,2 @@ +class UsersController < ApplicationController +end diff --git a/app/helpers/sessions_helper.rb b/app/helpers/sessions_helper.rb new file mode 100644 index 0000000..309f8b2 --- /dev/null +++ b/app/helpers/sessions_helper.rb @@ -0,0 +1,2 @@ +module SessionsHelper +end diff --git a/app/helpers/users_helper.rb b/app/helpers/users_helper.rb new file mode 100644 index 0000000..2310a24 --- /dev/null +++ b/app/helpers/users_helper.rb @@ -0,0 +1,2 @@ +module UsersHelper +end diff --git a/app/models/user.rb b/app/models/user.rb new file mode 100644 index 0000000..f573c0d --- /dev/null +++ b/app/models/user.rb @@ -0,0 +1,10 @@ +class User < ActiveRecord::Base + has_secure_password + + def generate_remember_token + begin + token = SecureRandom.base64(32) + end while User.where(remember_token: token).exists? + token + end +end diff --git a/app/views/sessions/new.html.haml b/app/views/sessions/new.html.haml new file mode 100644 index 0000000..5e1f88c --- /dev/null +++ b/app/views/sessions/new.html.haml @@ -0,0 +1,7 @@ += render partial: "shared/error" += form_for(:session, url: sessions_path) do |f| + = f.label :name + = f.text_field :name + = f.label :password + = f.text_field :password + = f.submit diff --git a/app/views/shared/_error.html.haml b/app/views/shared/_error.html.haml new file mode 100644 index 0000000..f42aa21 --- /dev/null +++ b/app/views/shared/_error.html.haml @@ -0,0 +1,3 @@ +%p.error + = flash[:notice] + diff --git a/config/routes.rb b/config/routes.rb index da62254..0a4d55a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,6 @@ Rails.application.routes.draw do + resources :users + resources :fluentd, only: [:index] do resource :daemon, only: [:show], module: :fluentd do put "start" @@ -10,6 +12,11 @@ Rails.application.routes.draw do end end + resource :sessions + + resources :users do + end + resources :misc, only: [] do end # The priority is based upon order of creation: first created -> highest priority. diff --git a/db/migrate/20140513053102_create_users.rb b/db/migrate/20140513053102_create_users.rb new file mode 100644 index 0000000..bf37774 --- /dev/null +++ b/db/migrate/20140513053102_create_users.rb @@ -0,0 +1,11 @@ +class CreateUsers < ActiveRecord::Migration + def change + create_table :users do |t| + t.string :name, null: false + t.string :password_digest, null: false + t.string :remember_token + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 0000000..b6c6a56 --- /dev/null +++ b/db/schema.rb @@ -0,0 +1,24 @@ +# encoding: UTF-8 +# This file is auto-generated from the current state of the database. Instead +# of editing this file, please use the migrations feature of Active Record to +# incrementally modify your database, and then regenerate this schema definition. +# +# Note that this schema.rb definition is the authoritative source for your +# database schema. If you need to create the application database on another +# system, you should be using db:schema:load, not running all the migrations +# from scratch. The latter is a flawed and unsustainable approach (the more migrations +# you'll amass, the slower it'll run and the greater likelihood for issues). +# +# It's strongly recommended that you check this file into your version control system. + +ActiveRecord::Schema.define(version: 20140513053102) do + + create_table "users", force: true do |t| + t.string "name", null: false + t.string "password_digest", null: false + t.string "remember_token" + t.datetime "created_at" + t.datetime "updated_at" + end + +end diff --git a/db/seeds.rb b/db/seeds.rb index 4edb1e8..e4c11f2 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -5,3 +5,5 @@ # # cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }]) # Mayor.create(name: 'Emanuel', city: cities.first) + +User.create(name: "admin", password: "changeme") diff --git a/spec/controllers/sessions_controller_spec.rb b/spec/controllers/sessions_controller_spec.rb new file mode 100644 index 0000000..702c559 --- /dev/null +++ b/spec/controllers/sessions_controller_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe SessionsController do + +end diff --git a/spec/controllers/users_controller_spec.rb b/spec/controllers/users_controller_spec.rb new file mode 100644 index 0000000..142455c --- /dev/null +++ b/spec/controllers/users_controller_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe UsersController do + +end diff --git a/spec/helpers/sessions_helper_spec.rb b/spec/helpers/sessions_helper_spec.rb new file mode 100644 index 0000000..cc4c89d --- /dev/null +++ b/spec/helpers/sessions_helper_spec.rb @@ -0,0 +1,15 @@ +require 'spec_helper' + +# Specs in this file have access to a helper object that includes +# the SessionsHelper. For example: +# +# describe SessionsHelper do +# describe "string concat" do +# it "concats two strings with spaces" do +# expect(helper.concat_strings("this","that")).to eq("this that") +# end +# end +# end +describe SessionsHelper do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/helpers/users_helper_spec.rb b/spec/helpers/users_helper_spec.rb new file mode 100644 index 0000000..5073928 --- /dev/null +++ b/spec/helpers/users_helper_spec.rb @@ -0,0 +1,15 @@ +require 'spec_helper' + +# Specs in this file have access to a helper object that includes +# the UsersHelper. For example: +# +# describe UsersHelper do +# describe "string concat" do +# it "concats two strings with spaces" do +# expect(helper.concat_strings("this","that")).to eq("this that") +# end +# end +# end +describe UsersHelper do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb new file mode 100644 index 0000000..44032b4 --- /dev/null +++ b/spec/models/user_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe User do + pending "add some examples to (or delete) #{__FILE__}" +end From 0f9909b68f1ac2ad4982bc74224b2be5ac7e6b9d Mon Sep 17 00:00:00 2001 From: uu59 Date: Tue, 13 May 2014 15:35:44 +0900 Subject: [PATCH 02/12] Add i18n files --- Gemfile.lock | 31 +++- config/application.rb | 2 +- config/locales/en.yml | 225 +++++++++++++++++++++++++++--- config/locales/ja.yml | 194 ++++++++++++++++++++++++++ config/locales/translation_en.yml | 10 ++ config/locales/translation_ja.yml | 10 ++ fluentd-ui.gemspec | 2 + 7 files changed, 450 insertions(+), 24 deletions(-) create mode 100644 config/locales/ja.yml create mode 100644 config/locales/translation_en.yml create mode 100644 config/locales/translation_ja.yml diff --git a/Gemfile.lock b/Gemfile.lock index d4e37f4..bf1bc1c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -2,10 +2,12 @@ PATH remote: . specs: fluentd-ui (0.0.1) + bcrypt (~> 3.1.5) bundler (~> 1.5) coffee-rails (~> 4.0.0) fluentd (= 0.10.46) haml-rails (~> 0.5.3) + i18n_generators (= 1.2.1) jbuilder (~> 2.0) jquery-rails (~> 3.1.0) rails (= 4.1.1) @@ -43,6 +45,7 @@ GEM thread_safe (~> 0.1) tzinfo (~> 1.1) arel (5.0.1.20140414130214) + bcrypt (3.1.7) builder (3.2.2) coderay (1.1.0) coffee-rails (4.0.1) @@ -52,8 +55,10 @@ GEM coffee-script-source execjs coffee-script-source (1.7.0) - cool.io (1.2.3) + cool.io (1.2.4) diff-lcs (1.2.5) + domain_name (0.5.18) + unf (>= 0.0.5, < 1.0.0) erubis (2.7.0) execjs (2.0.2) fluentd (0.10.46) @@ -71,8 +76,13 @@ GEM haml (>= 3.1, < 5.0) railties (>= 4.0.1) hike (1.2.3) + http-cookie (1.0.2) + domain_name (~> 0.5) http_parser.rb (0.6.0) i18n (0.6.9) + i18n_generators (1.2.1) + mechanize + rails (>= 3.0.0) jbuilder (2.0.7) activesupport (>= 3.0.0, < 5) multi_json (~> 1.2) @@ -83,11 +93,26 @@ GEM mail (2.5.4) mime-types (~> 1.16) treetop (~> 1.4.8) + mechanize (2.7.2) + domain_name (~> 0.5, >= 0.5.1) + http-cookie (~> 1.0.0) + mime-types (~> 1.17, >= 1.17.2) + net-http-digest_auth (~> 1.1, >= 1.1.1) + net-http-persistent (~> 2.5, >= 2.5.2) + nokogiri (~> 1.4) + ntlm-http (~> 0.1, >= 0.1.1) + webrobots (>= 0.0.9, < 0.2) method_source (0.8.2) mime-types (1.25.1) + mini_portile (0.5.3) minitest (5.3.3) msgpack (0.5.8) multi_json (1.10.0) + net-http-digest_auth (1.4) + net-http-persistent (2.9.4) + nokogiri (1.6.1) + mini_portile (~> 0.5.0) + ntlm-http (0.1.1) polyglot (0.3.4) pry (0.9.12.6) coderay (~> 1.0) @@ -153,6 +178,10 @@ GEM uglifier (2.5.0) execjs (>= 0.3.0) json (>= 1.8.0) + unf (0.1.4) + unf_ext + unf_ext (0.0.6) + webrobots (0.1.1) yajl-ruby (1.2.0) PLATFORMS diff --git a/config/application.rb b/config/application.rb index df49b14..c1fc524 100644 --- a/config/application.rb +++ b/config/application.rb @@ -28,6 +28,6 @@ module FluentdUi # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded. # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s] - # config.i18n.default_locale = :de + config.i18n.default_locale = 'ja' end end diff --git a/config/locales/en.yml b/config/locales/en.yml index 0653957..e1de9ab 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1,23 +1,204 @@ -# Files in the config/locales directory are used for internationalization -# and are automatically loaded by Rails. If you want to use locales other -# than English, add the necessary files in this directory. -# -# To use the locales, use `I18n.t`: -# -# I18n.t 'hello' -# -# In views, this is aliased to just `t`: -# -# <%= t('hello') %> -# -# To use a different locale, set it with `I18n.locale`: -# -# I18n.locale = :es -# -# This would use the information in config/locales/es.yml. -# -# To learn more, please read the Rails Internationalization guide -# available at http://guides.rubyonrails.org/i18n.html. - en: - hello: "Hello world" + date: + abbr_day_names: + - Sun + - Mon + - Tue + - Wed + - Thu + - Fri + - Sat + abbr_month_names: + - + - Jan + - Feb + - Mar + - Apr + - May + - Jun + - Jul + - Aug + - Sep + - Oct + - Nov + - Dec + day_names: + - Sunday + - Monday + - Tuesday + - Wednesday + - Thursday + - Friday + - Saturday + formats: + default: ! '%Y-%m-%d' + long: ! '%B %d, %Y' + short: ! '%b %d' + month_names: + - + - January + - February + - March + - April + - May + - June + - July + - August + - September + - October + - November + - December + order: + - :year + - :month + - :day + datetime: + distance_in_words: + about_x_hours: + one: about 1 hour + other: about %{count} hours + about_x_months: + one: about 1 month + other: about %{count} months + about_x_years: + one: about 1 year + other: about %{count} years + almost_x_years: + one: almost 1 year + other: almost %{count} years + half_a_minute: half a minute + less_than_x_minutes: + one: less than a minute + other: less than %{count} minutes + less_than_x_seconds: + one: less than 1 second + other: less than %{count} seconds + over_x_years: + one: over 1 year + other: over %{count} years + x_days: + one: 1 day + other: ! '%{count} days' + x_minutes: + one: 1 minute + other: ! '%{count} minutes' + x_months: + one: 1 month + other: ! '%{count} months' + x_seconds: + one: 1 second + other: ! '%{count} seconds' + prompts: + day: Day + hour: Hour + minute: Minute + month: Month + second: Seconds + year: Year + errors: + format: ! '%{attribute} %{message}' + messages: + accepted: must be accepted + blank: can't be blank + present: must be blank + confirmation: ! "doesn't match %{attribute}" + empty: can't be empty + equal_to: must be equal to %{count} + even: must be even + exclusion: is reserved + greater_than: must be greater than %{count} + greater_than_or_equal_to: must be greater than or equal to %{count} + inclusion: is not included in the list + invalid: is invalid + less_than: must be less than %{count} + less_than_or_equal_to: must be less than or equal to %{count} + not_a_number: is not a number + not_an_integer: must be an integer + odd: must be odd + record_invalid: ! 'Validation failed: %{errors}' + restrict_dependent_destroy: + one: "Cannot delete record because a dependent %{record} exists" + many: "Cannot delete record because dependent %{record} exist" + taken: has already been taken + too_long: + one: is too long (maximum is 1 character) + other: is too long (maximum is %{count} characters) + too_short: + one: is too short (minimum is 1 character) + other: is too short (minimum is %{count} characters) + wrong_length: + one: is the wrong length (should be 1 character) + other: is the wrong length (should be %{count} characters) + other_than: "must be other than %{count}" + template: + body: ! 'There were problems with the following fields:' + header: + one: 1 error prohibited this %{model} from being saved + other: ! '%{count} errors prohibited this %{model} from being saved' + helpers: + select: + prompt: Please select + submit: + create: Create %{model} + submit: Save %{model} + update: Update %{model} + number: + currency: + format: + delimiter: ! ',' + format: ! '%u%n' + precision: 2 + separator: . + significant: false + strip_insignificant_zeros: false + unit: $ + format: + delimiter: ! ',' + precision: 3 + separator: . + significant: false + strip_insignificant_zeros: false + human: + decimal_units: + format: ! '%n %u' + units: + billion: Billion + million: Million + quadrillion: Quadrillion + thousand: Thousand + trillion: Trillion + unit: '' + format: + delimiter: '' + precision: 3 + significant: true + strip_insignificant_zeros: true + storage_units: + format: ! '%n %u' + units: + byte: + one: Byte + other: Bytes + gb: GB + kb: KB + mb: MB + tb: TB + percentage: + format: + delimiter: '' + format: "%n%" + precision: + format: + delimiter: '' + support: + array: + last_word_connector: ! ', and ' + two_words_connector: ! ' and ' + words_connector: ! ', ' + time: + am: am + formats: + default: ! '%a, %d %b %Y %H:%M:%S %z' + long: ! '%B %d, %Y %H:%M' + short: ! '%d %b %H:%M' + pm: pm diff --git a/config/locales/ja.yml b/config/locales/ja.yml new file mode 100644 index 0000000..f86780d --- /dev/null +++ b/config/locales/ja.yml @@ -0,0 +1,194 @@ +ja: + date: + abbr_day_names: + - 日 + - 月 + - 火 + - 水 + - 木 + - 金 + - 土 + abbr_month_names: + - + - 1月 + - 2月 + - 3月 + - 4月 + - 5月 + - 6月 + - 7月 + - 8月 + - 9月 + - 10月 + - 11月 + - 12月 + day_names: + - 日曜日 + - 月曜日 + - 火曜日 + - 水曜日 + - 木曜日 + - 金曜日 + - 土曜日 + formats: + default: ! '%Y/%m/%d' + long: ! '%Y年%m月%d日(%a)' + short: ! '%m/%d' + month_names: + - + - 1月 + - 2月 + - 3月 + - 4月 + - 5月 + - 6月 + - 7月 + - 8月 + - 9月 + - 10月 + - 11月 + - 12月 + order: + - :year + - :month + - :day + datetime: + distance_in_words: + about_x_hours: + one: 約1時間 + other: 約%{count}時間 + about_x_months: + one: 約1ヶ月 + other: 約%{count}ヶ月 + about_x_years: + one: 約1年 + other: 約%{count}年 + almost_x_years: + one: 1年弱 + other: ! '%{count}年弱' + half_a_minute: 30秒前後 + less_than_x_minutes: + one: 1分以内 + other: ! '%{count}分未満' + less_than_x_seconds: + one: 1秒以内 + other: ! '%{count}秒未満' + over_x_years: + one: 1年以上 + other: ! '%{count}年以上' + x_days: + one: 1日 + other: ! '%{count}日' + x_minutes: + one: 1分 + other: ! '%{count}分' + x_months: + one: 1ヶ月 + other: ! '%{count}ヶ月' + x_seconds: + one: 1秒 + other: ! '%{count}秒' + prompts: + day: 日 + hour: 時 + minute: 分 + month: 月 + second: 秒 + year: 年 + errors: + format: ! '%{attribute}%{message}' + messages: + accepted: を受諾してください。 + blank: を入力してください。 + present: は入力しないでください。 + confirmation: と%{attribute}の入力が一致しません。 + empty: を入力してください。 + equal_to: は%{count}にしてください。 + even: は偶数にしてください。 + exclusion: は予約されています。 + greater_than: は%{count}より大きい値にしてください。 + greater_than_or_equal_to: は%{count}以上の値にしてください。 + inclusion: は一覧にありません。 + invalid: は不正な値です。 + less_than: は%{count}より小さい値にしてください。 + less_than_or_equal_to: は%{count}以下の値にしてください。 + not_a_number: は数値で入力してください。 + not_an_integer: は整数で入力してください。 + odd: は奇数にしてください。 + record_invalid: バリデーションに失敗しました。 %{errors} + restrict_dependent_destroy: ! '%{record}が存在しているので削除できません。' + taken: はすでに存在します。 + too_long: は%{count}文字以内で入力してください。 + too_short: は%{count}文字以上で入力してください。 + wrong_length: は%{count}文字で入力してください。 + other_than: "は%{count}以外の値にしてください。" + template: + body: 次の項目を確認してください。 + header: + one: ! '%{model}にエラーが発生しました。' + other: ! '%{model}に%{count}個のエラーが発生しました。' + helpers: + select: + prompt: 選択してください。 + submit: + create: 登録する + submit: 保存する + update: 更新する + number: + currency: + format: + delimiter: ! ',' + format: ! '%n%u' + precision: 0 + separator: . + significant: false + strip_insignificant_zeros: false + unit: 円 + format: + delimiter: ! ',' + precision: 3 + separator: . + significant: false + strip_insignificant_zeros: false + human: + decimal_units: + format: ! '%n %u' + units: + billion: 十億 + million: 百万 + quadrillion: 千兆 + thousand: 千 + trillion: 兆 + unit: '' + format: + delimiter: '' + precision: 3 + significant: true + strip_insignificant_zeros: true + storage_units: + format: ! '%n%u' + units: + byte: バイト + gb: ギガバイト + kb: キロバイト + mb: メガバイト + tb: テラバイト + percentage: + format: + delimiter: '' + format: "%n%" + precision: + format: + delimiter: '' + support: + array: + last_word_connector: と + two_words_connector: と + words_connector: と + time: + am: 午前 + formats: + default: ! '%Y/%m/%d %H:%M:%S' + long: ! '%Y年%m月%d日(%a) %H時%M分%S秒 %z' + short: ! '%y/%m/%d %H:%M' + pm: 午後 diff --git a/config/locales/translation_en.yml b/config/locales/translation_en.yml new file mode 100644 index 0000000..ca3c0a7 --- /dev/null +++ b/config/locales/translation_en.yml @@ -0,0 +1,10 @@ +en: + activerecord: + models: + user: user #g + + attributes: + user: + name: name #g + password_digest: password_digest #g + remember_token: remember_token #g diff --git a/config/locales/translation_ja.yml b/config/locales/translation_ja.yml new file mode 100644 index 0000000..c7f4618 --- /dev/null +++ b/config/locales/translation_ja.yml @@ -0,0 +1,10 @@ +ja: + activerecord: + models: + user: user #g + + attributes: + user: + name: name #g + password_digest: password_digest #g + remember_token: remember_token #g diff --git a/fluentd-ui.gemspec b/fluentd-ui.gemspec index e4a9cd7..62d8966 100644 --- a/fluentd-ui.gemspec +++ b/fluentd-ui.gemspec @@ -20,6 +20,8 @@ Gem::Specification.new do |spec| spec.add_dependency "fluentd", "0.10.46" spec.add_dependency 'rails', '4.1.1' + spec.add_dependency 'i18n_generators', '1.2.1' + spec.add_dependency 'bcrypt', '~> 3.1.5' spec.add_dependency 'sqlite3' spec.add_dependency 'sass-rails', '~> 4.0.3' spec.add_dependency 'uglifier', '>= 1.3.0' From 6af8a51b0216cf594681336e8d586fd7479893c4 Mon Sep 17 00:00:00 2001 From: uu59 Date: Tue, 13 May 2014 15:37:50 +0900 Subject: [PATCH 03/12] Add error message for login failed --- app/controllers/sessions_controller.rb | 3 +-- config/locales/translation_en.yml | 3 +++ config/locales/translation_ja.yml | 3 +++ 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 04ac3d0..a775bbf 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -2,8 +2,7 @@ class SessionsController < ApplicationController def create user = User.find_by(name: session_params[:name]).try(:authenticate, session_params[:password]) unless user - # TODO: i18n - flash.now[:notice] = I18n.t(:login_failed) + flash.now[:notice] = I18n.t("error.login_failed") return render :new end sign_in user diff --git a/config/locales/translation_en.yml b/config/locales/translation_en.yml index ca3c0a7..b6cd596 100644 --- a/config/locales/translation_en.yml +++ b/config/locales/translation_en.yml @@ -1,4 +1,7 @@ en: + error: + login_failed: Login failed. + activerecord: models: user: user #g diff --git a/config/locales/translation_ja.yml b/config/locales/translation_ja.yml index c7f4618..8863cee 100644 --- a/config/locales/translation_ja.yml +++ b/config/locales/translation_ja.yml @@ -1,4 +1,7 @@ ja: + error: + login_failed: ログインに失敗しました。 + activerecord: models: user: user #g From 1e568b279305d2a64c22fb1f89109958db491ec0 Mon Sep 17 00:00:00 2001 From: uu59 Date: Tue, 13 May 2014 15:43:26 +0900 Subject: [PATCH 04/12] Add root path as temporally --- config/routes.rb | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/config/routes.rb b/config/routes.rb index 0a4d55a..a1e7b36 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,5 +1,6 @@ Rails.application.routes.draw do - resources :users + root "fluentd#index" # TODO: change to dashboard + resources :fluentd, only: [:index] do resource :daemon, only: [:show], module: :fluentd do @@ -12,11 +13,9 @@ Rails.application.routes.draw do end end + resources :users resource :sessions - resources :users do - end - resources :misc, only: [] do end # The priority is based upon order of creation: first created -> highest priority. From 19b0e591a126286a9491f0af02731773886d9cc0 Mon Sep 17 00:00:00 2001 From: uu59 Date: Tue, 13 May 2014 15:46:24 +0900 Subject: [PATCH 05/12] Add login_required for exists controllers --- app/controllers/application_controller.rb | 5 +++-- app/controllers/fluentd/daemons_controller.rb | 3 ++- app/controllers/fluentd/settings_controller.rb | 2 ++ app/controllers/fluentd_controller.rb | 2 ++ app/controllers/users_controller.rb | 1 + 5 files changed, 10 insertions(+), 3 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 2bd0274..49e41f6 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -8,7 +8,8 @@ class ApplicationController < ActionController::Base @current_user ||= User.find_by(remember_token: session[:remember_token]) end - def login_require - !!current_user + def login_required + return true if current_user + redirect_to new_sessions_path end end diff --git a/app/controllers/fluentd/daemons_controller.rb b/app/controllers/fluentd/daemons_controller.rb index 8ae2e2d..1103cf5 100644 --- a/app/controllers/fluentd/daemons_controller.rb +++ b/app/controllers/fluentd/daemons_controller.rb @@ -1,5 +1,6 @@ class Fluentd::DaemonsController < ApplicationController - before_filter :fluentd + before_action :login_required + before_action :fluentd def show end diff --git a/app/controllers/fluentd/settings_controller.rb b/app/controllers/fluentd/settings_controller.rb index b24c060..5adabe8 100644 --- a/app/controllers/fluentd/settings_controller.rb +++ b/app/controllers/fluentd/settings_controller.rb @@ -1,4 +1,6 @@ class Fluentd::SettingsController < ApplicationController + before_action :login_required + def show render text: fluentd.config.to_s, content_type: "text/plain" end diff --git a/app/controllers/fluentd_controller.rb b/app/controllers/fluentd_controller.rb index 83cb51e..6531678 100644 --- a/app/controllers/fluentd_controller.rb +++ b/app/controllers/fluentd_controller.rb @@ -1,4 +1,6 @@ class FluentdController < ApplicationController + before_action :login_required + def index @daemons = [Fluentd.new(Rails.root + "tmp" + "fluentd")] # TODO end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 3e74dea..6ac7e49 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,2 +1,3 @@ class UsersController < ApplicationController + before_action :login_required end From e19ce055259ad16b982b1b17cb2398988beb6756 Mon Sep 17 00:00:00 2001 From: uu59 Date: Tue, 13 May 2014 16:03:08 +0900 Subject: [PATCH 06/12] Add sign out --- app/controllers/application_controller.rb | 1 + app/controllers/sessions_controller.rb | 3 +++ app/views/layouts/application.html.erb | 1 + config/locales/translation_en.yml | 4 ++++ config/locales/translation_ja.yml | 4 ++++ 5 files changed, 13 insertions(+) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 49e41f6..3d7d86c 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -2,6 +2,7 @@ class ApplicationController < ActionController::Base # Prevent CSRF attacks by raising an exception. # For APIs, you may want to use :null_session instead. protect_from_forgery with: :exception + helper_method :current_user def current_user return unless session[:remember_token] diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index a775bbf..cca3903 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -10,6 +10,9 @@ class SessionsController < ApplicationController end def destroy + current_user.update_attribute(:remember_token, nil) + session.delete :remember_token + redirect_to new_sessions_path end private diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index bd38456..59ed4d7 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -8,6 +8,7 @@ + <%= current_user ? link_to(t("terms.sign_out"), sessions_path, method: :delete) : link_to(t("terms.sign_in"), new_sessions_path) %> <%= yield %> diff --git a/config/locales/translation_en.yml b/config/locales/translation_en.yml index b6cd596..0663aae 100644 --- a/config/locales/translation_en.yml +++ b/config/locales/translation_en.yml @@ -1,4 +1,8 @@ en: + terms: + sign_in: Sign in + sign_out: Sign out + error: login_failed: Login failed. diff --git a/config/locales/translation_ja.yml b/config/locales/translation_ja.yml index 8863cee..81cc318 100644 --- a/config/locales/translation_ja.yml +++ b/config/locales/translation_ja.yml @@ -1,4 +1,8 @@ ja: + terms: + sign_in: ログイン + sign_out: ログアウト + error: login_failed: ログインに失敗しました。 From 1f8dd17c4353b14de23319b9de81d508b45462a0 Mon Sep 17 00:00:00 2001 From: uu59 Date: Tue, 13 May 2014 16:23:37 +0900 Subject: [PATCH 07/12] model spec --- Gemfile | 1 + Gemfile.lock | 6 +++++ app/models/user.rb | 3 +++ spec/factories/user.rb | 7 ++++++ spec/models/user_spec.rb | 53 +++++++++++++++++++++++++++++++++++++++- 5 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 spec/factories/user.rb diff --git a/Gemfile b/Gemfile index bc12412..1fd08c0 100644 --- a/Gemfile +++ b/Gemfile @@ -8,4 +8,5 @@ group :development, :test do gem "rake" gem "pry" gem "rspec-rails", "~> 2.0" + gem "factory_girl_rails" end diff --git a/Gemfile.lock b/Gemfile.lock index bf1bc1c..a56132c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -61,6 +61,11 @@ GEM unf (>= 0.0.5, < 1.0.0) erubis (2.7.0) execjs (2.0.2) + factory_girl (4.4.0) + activesupport (>= 3.0.0) + factory_girl_rails (4.4.1) + factory_girl (~> 4.4.0) + railties (>= 3.0.0) fluentd (0.10.46) cool.io (>= 1.1.1, < 2.0.0, != 1.2.0) http_parser.rb (>= 0.5.1, < 0.7.0) @@ -188,6 +193,7 @@ PLATFORMS ruby DEPENDENCIES + factory_girl_rails fluentd-ui! pry rake diff --git a/app/models/user.rb b/app/models/user.rb index f573c0d..70c745a 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,6 +1,9 @@ class User < ActiveRecord::Base has_secure_password + validates :name, uniqueness: true, presence: true + validates :remember_token, uniqueness: true, allow_nil: true + def generate_remember_token begin token = SecureRandom.base64(32) diff --git a/spec/factories/user.rb b/spec/factories/user.rb new file mode 100644 index 0000000..a61564b --- /dev/null +++ b/spec/factories/user.rb @@ -0,0 +1,7 @@ +FactoryGirl.define do + factory :user do + sequence(:name) {|n| "user#{n}" } + password "passw0rd" + password_confirmation "passw0rd" + end +end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 44032b4..b16884b 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -1,5 +1,56 @@ require 'spec_helper' describe User do - pending "add some examples to (or delete) #{__FILE__}" + let(:user) { FactoryGirl.build(:user) } + + describe "#generate_remember_token" do + subject { user.generate_remember_token } + it { User.find_by(remember_token: subject).should be_nil } + end + + describe "#valid?" do + it { user.should be_valid } + + describe "name" do + it "nil is invalid" do + user.name = nil + user.should_not be_valid + end + + it "taken name is invalid" do + another_user = FactoryGirl.create(:user) + user.name = another_user.name + user.should_not be_valid + end + end + + describe "password" do + it "password != password_confirmation is invalid" do + user.password = "a" + user.password_confirmation = "b" + user.should_not be_valid + end + end + + describe "remember_token" do + let(:token) { "xxx" } + + it "nil is valid" do + user.remember_token = nil + user.should be_valid + end + + it "nil and taken is valid " do + FactoryGirl.create(:user, remember_token: nil) + user.remember_token = nil + user.should be_valid + end + + it "taken token is invalid" do + FactoryGirl.create(:user, remember_token: token) + user.remember_token = token + user.should_not be_valid + end + end + end end From 3aca0a9f26402deab623630ce1b35690e8ea6f46 Mon Sep 17 00:00:00 2001 From: uu59 Date: Tue, 13 May 2014 16:23:59 +0900 Subject: [PATCH 08/12] Remove unused things --- app/helpers/sessions_helper.rb | 2 -- app/helpers/users_helper.rb | 2 -- spec/helpers/sessions_helper_spec.rb | 15 --------------- spec/helpers/users_helper_spec.rb | 15 --------------- 4 files changed, 34 deletions(-) delete mode 100644 app/helpers/sessions_helper.rb delete mode 100644 app/helpers/users_helper.rb delete mode 100644 spec/helpers/sessions_helper_spec.rb delete mode 100644 spec/helpers/users_helper_spec.rb diff --git a/app/helpers/sessions_helper.rb b/app/helpers/sessions_helper.rb deleted file mode 100644 index 309f8b2..0000000 --- a/app/helpers/sessions_helper.rb +++ /dev/null @@ -1,2 +0,0 @@ -module SessionsHelper -end diff --git a/app/helpers/users_helper.rb b/app/helpers/users_helper.rb deleted file mode 100644 index 2310a24..0000000 --- a/app/helpers/users_helper.rb +++ /dev/null @@ -1,2 +0,0 @@ -module UsersHelper -end diff --git a/spec/helpers/sessions_helper_spec.rb b/spec/helpers/sessions_helper_spec.rb deleted file mode 100644 index cc4c89d..0000000 --- a/spec/helpers/sessions_helper_spec.rb +++ /dev/null @@ -1,15 +0,0 @@ -require 'spec_helper' - -# Specs in this file have access to a helper object that includes -# the SessionsHelper. For example: -# -# describe SessionsHelper do -# describe "string concat" do -# it "concats two strings with spaces" do -# expect(helper.concat_strings("this","that")).to eq("this that") -# end -# end -# end -describe SessionsHelper do - pending "add some examples to (or delete) #{__FILE__}" -end diff --git a/spec/helpers/users_helper_spec.rb b/spec/helpers/users_helper_spec.rb deleted file mode 100644 index 5073928..0000000 --- a/spec/helpers/users_helper_spec.rb +++ /dev/null @@ -1,15 +0,0 @@ -require 'spec_helper' - -# Specs in this file have access to a helper object that includes -# the UsersHelper. For example: -# -# describe UsersHelper do -# describe "string concat" do -# it "concats two strings with spaces" do -# expect(helper.concat_strings("this","that")).to eq("this that") -# end -# end -# end -describe UsersHelper do - pending "add some examples to (or delete) #{__FILE__}" -end From 136d4da7b6f1eab3ce1b634e694114f48d5d25f1 Mon Sep 17 00:00:00 2001 From: uu59 Date: Tue, 13 May 2014 16:27:11 +0900 Subject: [PATCH 09/12] Add database_cleaner --- Gemfile | 1 + Gemfile.lock | 2 ++ spec/spec_helper.rb | 13 +++++++++++++ 3 files changed, 16 insertions(+) diff --git a/Gemfile b/Gemfile index 1fd08c0..1850ea6 100644 --- a/Gemfile +++ b/Gemfile @@ -9,4 +9,5 @@ group :development, :test do gem "pry" gem "rspec-rails", "~> 2.0" gem "factory_girl_rails" + gem "database_cleaner", "~> 1.2.0" end diff --git a/Gemfile.lock b/Gemfile.lock index a56132c..28ab76a 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -56,6 +56,7 @@ GEM execjs coffee-script-source (1.7.0) cool.io (1.2.4) + database_cleaner (1.2.0) diff-lcs (1.2.5) domain_name (0.5.18) unf (>= 0.0.5, < 1.0.0) @@ -193,6 +194,7 @@ PLATFORMS ruby DEPENDENCIES + database_cleaner (~> 1.2.0) factory_girl_rails fluentd-ui! pry diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 39de47e..3427a05 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -39,4 +39,17 @@ RSpec.configure do |config| # the seed, which is printed after each run. # --seed 1234 config.order = "random" + + config.before(:suite) do + DatabaseCleaner.strategy = :transaction + DatabaseCleaner.clean_with(:truncation) + end + + config.before(:each) do + DatabaseCleaner.start + end + + config.after(:each) do + DatabaseCleaner.clean + end end From fc044bd7199d63ee8c73fd34653a9cece85d11a1 Mon Sep 17 00:00:00 2001 From: uu59 Date: Tue, 13 May 2014 16:45:59 +0900 Subject: [PATCH 10/12] Add feature spec --- Gemfile | 1 + Gemfile.lock | 9 +++++++++ app/views/sessions/new.html.haml | 3 ++- spec/features/sign_in_spec.rb | 31 +++++++++++++++++++++++++++++++ 4 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 spec/features/sign_in_spec.rb diff --git a/Gemfile b/Gemfile index 1850ea6..0d547c2 100644 --- a/Gemfile +++ b/Gemfile @@ -10,4 +10,5 @@ group :development, :test do gem "rspec-rails", "~> 2.0" gem "factory_girl_rails" gem "database_cleaner", "~> 1.2.0" + gem "capybara", "~> 2.2.1" end diff --git a/Gemfile.lock b/Gemfile.lock index 28ab76a..8747ad6 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -47,6 +47,12 @@ GEM arel (5.0.1.20140414130214) bcrypt (3.1.7) builder (3.2.2) + capybara (2.2.1) + mime-types (>= 1.16) + nokogiri (>= 1.3.3) + rack (>= 1.0.0) + rack-test (>= 0.5.4) + xpath (~> 2.0) coderay (1.1.0) coffee-rails (4.0.1) coffee-script (>= 2.2.0) @@ -188,12 +194,15 @@ GEM unf_ext unf_ext (0.0.6) webrobots (0.1.1) + xpath (2.0.0) + nokogiri (~> 1.3) yajl-ruby (1.2.0) PLATFORMS ruby DEPENDENCIES + capybara (~> 2.2.1) database_cleaner (~> 1.2.0) factory_girl_rails fluentd-ui! diff --git a/app/views/sessions/new.html.haml b/app/views/sessions/new.html.haml index 5e1f88c..f1d3e69 100644 --- a/app/views/sessions/new.html.haml +++ b/app/views/sessions/new.html.haml @@ -4,4 +4,5 @@ = f.text_field :name = f.label :password = f.text_field :password - = f.submit + = submit_tag t("terms.sign_in") + diff --git a/spec/features/sign_in_spec.rb b/spec/features/sign_in_spec.rb new file mode 100644 index 0000000..31321b3 --- /dev/null +++ b/spec/features/sign_in_spec.rb @@ -0,0 +1,31 @@ +describe "the sign in process" do + let(:submit_label) { I18n.t("terms.sign_in") } + let(:exists_user) { FactoryGirl.create(:user) } + before do + visit '/sessions/new' + within("form") do + fill_in 'session_name', :with => user.name + fill_in 'session_password', :with => user.password + end + click_button submit_label + end + + context "sign in with exists user" do + let(:user) { exists_user } + it "login success, then redirect to root_path" do + current_path.should == root_path + end + end + + context "sign in with non-exists user" do + let(:user) { FactoryGirl.build(:user) } + + it "current location is not root_path" do + current_path.should_not == root_path + end + + it "display form for retry" do + page.body.should have_css('form') + end + end +end From 9945c62ede5ddb00eef53c943d8c308d6d830c36 Mon Sep 17 00:00:00 2001 From: uu59 Date: Tue, 13 May 2014 16:49:42 +0900 Subject: [PATCH 11/12] Add simplecov --- .gitignore | 5 +++++ Gemfile | 1 + Gemfile.lock | 5 +++++ spec/spec_helper.rb | 5 +++++ 4 files changed, 16 insertions(+) diff --git a/.gitignore b/.gitignore index 540fab8..ff0bb19 100644 --- a/.gitignore +++ b/.gitignore @@ -18,3 +18,8 @@ # built .gem file by `rake build` /pkg /Gemfile.production.lock + +# generated by simplecov +/coverage + +/.DS_Store diff --git a/Gemfile b/Gemfile index 0d547c2..eef1be7 100644 --- a/Gemfile +++ b/Gemfile @@ -11,4 +11,5 @@ group :development, :test do gem "factory_girl_rails" gem "database_cleaner", "~> 1.2.0" gem "capybara", "~> 2.2.1" + gem "simplecov", "~> 0.7.1", require: false end diff --git a/Gemfile.lock b/Gemfile.lock index 8747ad6..c8bb041 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -168,6 +168,10 @@ GEM sprockets (~> 2.8, <= 2.11.0) sprockets-rails (~> 2.0) sigdump (0.2.2) + simplecov (0.7.1) + multi_json (~> 1.0) + simplecov-html (~> 0.7.1) + simplecov-html (0.7.1) slop (3.5.0) sprockets (2.11.0) hike (~> 1.2) @@ -209,3 +213,4 @@ DEPENDENCIES pry rake rspec-rails (~> 2.0) + simplecov (~> 0.7.1) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 3427a05..a05908b 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,3 +1,8 @@ +if ENV['RAILS_ENV'] == 'test' + require 'simplecov' + SimpleCov.start 'rails' +end + # This file is copied to spec/ when you run 'rails generate rspec:install' ENV["RAILS_ENV"] ||= 'test' require File.expand_path("../../config/environment", __FILE__) From 0e9b54529e960fca60c399800ef3a87946fcf0d7 Mon Sep 17 00:00:00 2001 From: uu59 Date: Tue, 13 May 2014 16:55:36 +0900 Subject: [PATCH 12/12] Add sign out spec --- spec/features/sessions_spec.rb | 60 ++++++++++++++++++++++++++++++++++ spec/features/sign_in_spec.rb | 31 ------------------ 2 files changed, 60 insertions(+), 31 deletions(-) create mode 100644 spec/features/sessions_spec.rb delete mode 100644 spec/features/sign_in_spec.rb diff --git a/spec/features/sessions_spec.rb b/spec/features/sessions_spec.rb new file mode 100644 index 0000000..b8a00c8 --- /dev/null +++ b/spec/features/sessions_spec.rb @@ -0,0 +1,60 @@ +describe "sessions" do + let(:exists_user) { FactoryGirl.create(:user) } + + describe "the sign in process" do + let(:submit_label) { I18n.t("terms.sign_in") } + before do + visit '/sessions/new' + within("form") do + fill_in 'session_name', :with => user.name + fill_in 'session_password', :with => user.password + end + click_button submit_label + end + + context "sign in with exists user" do + let(:user) { exists_user } + it "login success, then redirect to root_path" do + current_path.should == root_path + end + end + + context "sign in with non-exists user" do + let(:user) { FactoryGirl.build(:user) } + + it "current location is not root_path" do + current_path.should_not == root_path + end + + it "display form for retry" do + page.body.should have_css('form') + end + end + end + + describe "sign out process" do + let(:submit_label) { I18n.t("terms.sign_in") } + before do + visit '/sessions/new' + within("form") do + fill_in 'session_name', :with => exists_user.name + fill_in 'session_password', :with => exists_user.password + end + click_button submit_label + end + + before do + visit root_path + click_link I18n.t("terms.sign_out") + end + + it "at sign in page after sign out" do + current_path.should == new_sessions_path + end + + it "remember_token was destroyed" do + exists_user.reload + exists_user.remember_token.should be_nil + end + end +end diff --git a/spec/features/sign_in_spec.rb b/spec/features/sign_in_spec.rb deleted file mode 100644 index 31321b3..0000000 --- a/spec/features/sign_in_spec.rb +++ /dev/null @@ -1,31 +0,0 @@ -describe "the sign in process" do - let(:submit_label) { I18n.t("terms.sign_in") } - let(:exists_user) { FactoryGirl.create(:user) } - before do - visit '/sessions/new' - within("form") do - fill_in 'session_name', :with => user.name - fill_in 'session_password', :with => user.password - end - click_button submit_label - end - - context "sign in with exists user" do - let(:user) { exists_user } - it "login success, then redirect to root_path" do - current_path.should == root_path - end - end - - context "sign in with non-exists user" do - let(:user) { FactoryGirl.build(:user) } - - it "current location is not root_path" do - current_path.should_not == root_path - end - - it "display form for retry" do - page.body.should have_css('form') - end - end -end