From 586635fc848d1afed9e6f4920a346a190e772b86 Mon Sep 17 00:00:00 2001 From: Gaddict Date: Wed, 11 Mar 2015 19:15:41 +0900 Subject: [PATCH 1/2] follow --- README.md | 6 ++ app/assets/javascripts/admin/bot.coffee | 3 + app/assets/stylesheets/admin/bot.scss | 3 + app/controllers/admin/bot_controller.rb | 56 +++++++++++++++++++ app/controllers/admin/manage_controller.rb | 10 +++- app/helpers/admin/bot_helper.rb | 2 + app/models/bot.rb | 18 ++++++ app/models/hash_tag.rb | 2 + app/views/admin/bot/creat.html.erb | 2 + app/views/admin/bot/destroy.html.erb | 2 + app/views/admin/bot/edit.html.erb | 2 + app/views/admin/bot/index.html.erb | 39 +++++++++++++ app/views/admin/bot/new.html.erb | 2 + app/views/admin/bot/show.html.erb | 2 + app/views/admin/bot/update.html.erb | 2 + app/views/admin/manage/bot.html.erb | 39 ++++++++++++- app/views/admin/manage/index.html.erb | 2 +- app/views/shared/_error_messages.html.erb | 9 +++ config/routes.rb | 11 ++-- db/migrate/20150310150232_create_bots.rb | 12 ++++ db/migrate/20150310150656_create_hash_tags.rb | 10 ++++ .../20150310150941_add_hash_tag_id_to_bots.rb | 6 ++ ...10151917_add_default_value_to_hash_tags.rb | 5 ++ db/schema.rb | 33 +++++++++++ test/controllers/admin/bot_controller_test.rb | 39 +++++++++++++ test/fixtures/bots.yml | 13 +++++ test/fixtures/hash_tags.yml | 9 +++ test/models/bot_test.rb | 7 +++ test/models/hash_tag_test.rb | 7 +++ 29 files changed, 346 insertions(+), 7 deletions(-) create mode 100644 app/assets/javascripts/admin/bot.coffee create mode 100644 app/assets/stylesheets/admin/bot.scss create mode 100644 app/controllers/admin/bot_controller.rb create mode 100644 app/helpers/admin/bot_helper.rb create mode 100644 app/models/bot.rb create mode 100644 app/models/hash_tag.rb create mode 100644 app/views/admin/bot/creat.html.erb create mode 100644 app/views/admin/bot/destroy.html.erb create mode 100644 app/views/admin/bot/edit.html.erb create mode 100644 app/views/admin/bot/index.html.erb create mode 100644 app/views/admin/bot/new.html.erb create mode 100644 app/views/admin/bot/show.html.erb create mode 100644 app/views/admin/bot/update.html.erb create mode 100644 app/views/shared/_error_messages.html.erb create mode 100644 db/migrate/20150310150232_create_bots.rb create mode 100644 db/migrate/20150310150656_create_hash_tags.rb create mode 100644 db/migrate/20150310150941_add_hash_tag_id_to_bots.rb create mode 100644 db/migrate/20150310151917_add_default_value_to_hash_tags.rb create mode 100644 db/schema.rb create mode 100644 test/controllers/admin/bot_controller_test.rb create mode 100644 test/fixtures/bots.yml create mode 100644 test/fixtures/hash_tags.yml create mode 100644 test/models/bot_test.rb create mode 100644 test/models/hash_tag_test.rb diff --git a/README.md b/README.md index 548e2aa..eac9ad0 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,12 @@ $ bundle exec rake twitter:tweet ``` +## modelの適用 + +``` +$ bundle exec rake db:migrate +``` + ## directory 以下は、このpjのadminとjobの分け方サンプル なぜadminとjobを一緒にしているのかはgoogledocの通り diff --git a/app/assets/javascripts/admin/bot.coffee b/app/assets/javascripts/admin/bot.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/app/assets/javascripts/admin/bot.coffee @@ -0,0 +1,3 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://coffeescript.org/ diff --git a/app/assets/stylesheets/admin/bot.scss b/app/assets/stylesheets/admin/bot.scss new file mode 100644 index 0000000..b6f4c9b --- /dev/null +++ b/app/assets/stylesheets/admin/bot.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the admin::bot controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/admin/bot_controller.rb b/app/controllers/admin/bot_controller.rb new file mode 100644 index 0000000..ec6d139 --- /dev/null +++ b/app/controllers/admin/bot_controller.rb @@ -0,0 +1,56 @@ +class Admin::BotController < ApplicationController + def index + list + @bot = Bot.new + end + + def create + list + @bot = Bot.new(bot_params) + if @bot.nil? + render({action: :index}, alert: "登録失敗!!") + end + + if @bot.save + redirect_to({action: :index}, notice: "登録完了!!") + else + render '/admin/manage/bot' + end + end + + def list + @bots = Bot.find_by + end + + def new + end + + def edit + end + + def show + end + + def update + end + + def destroy + bot = Bot.find_by_id(params[:id]) + bot.twitter_id = bot.twitter_id.blank? ? "ng" : bot.twitter_id + bot.twitter_name = bot.twitter_name.blank? ? "ng" : bot.twitter_name + bot.access_token = bot.access_token.blank? ? "ng" : bot.access_token + bot.hash_tags = bot.hash_tags.blank? ? "ng" : bot.hash_tags + bot.deleted = true + if bot.save + flash[:delete] = "物理削除完了!!" + redirect_to({action: :index}) + else + render '/admin/manage/bot' + end + end + + private + def bot_params + params.require(:bot).permit(:twitter_name, :twitter_id, :access_token, :hash_tags) + end +end diff --git a/app/controllers/admin/manage_controller.rb b/app/controllers/admin/manage_controller.rb index 142c1c5..9a2b776 100644 --- a/app/controllers/admin/manage_controller.rb +++ b/app/controllers/admin/manage_controller.rb @@ -1,8 +1,16 @@ class Admin::ManageController < ApplicationController def index end - def bot + + def delete + end + def twitter end + + private + def bot_params + params.require(:bot).permit(:twitter_name, :twitter_id, :access_token, :hash_tags) + end end diff --git a/app/helpers/admin/bot_helper.rb b/app/helpers/admin/bot_helper.rb new file mode 100644 index 0000000..8285e42 --- /dev/null +++ b/app/helpers/admin/bot_helper.rb @@ -0,0 +1,2 @@ +module Admin::BotHelper +end diff --git a/app/models/bot.rb b/app/models/bot.rb new file mode 100644 index 0000000..74fcf59 --- /dev/null +++ b/app/models/bot.rb @@ -0,0 +1,18 @@ +class Bot < ActiveRecord::Base + belongs_to :hash_tag + + validates :twitter_name, presence: true + validates :twitter_id, presence: true + validates :access_token, presence: true + validates :hash_tags, presence: true + + # 論理削除されていないレコードを全取得 + def self.find_by + Bot.where(:deleted => false) + end + + def self.find_by_id(id) + Bot.where(:id => id).first + end + +end diff --git a/app/models/hash_tag.rb b/app/models/hash_tag.rb new file mode 100644 index 0000000..8a07f4b --- /dev/null +++ b/app/models/hash_tag.rb @@ -0,0 +1,2 @@ +class HashTag < ActiveRecord::Base +end diff --git a/app/views/admin/bot/creat.html.erb b/app/views/admin/bot/creat.html.erb new file mode 100644 index 0000000..900f216 --- /dev/null +++ b/app/views/admin/bot/creat.html.erb @@ -0,0 +1,2 @@ +

Admin::Bot#creat

+

Find me in app/views/admin/bot/creat.html.erb

diff --git a/app/views/admin/bot/destroy.html.erb b/app/views/admin/bot/destroy.html.erb new file mode 100644 index 0000000..0be18fa --- /dev/null +++ b/app/views/admin/bot/destroy.html.erb @@ -0,0 +1,2 @@ +

Admin::Bot#destroy

+

Find me in app/views/admin/bot/destroy.html.erb

diff --git a/app/views/admin/bot/edit.html.erb b/app/views/admin/bot/edit.html.erb new file mode 100644 index 0000000..f00cfb2 --- /dev/null +++ b/app/views/admin/bot/edit.html.erb @@ -0,0 +1,2 @@ +

Admin::Bot#edit

+

Find me in app/views/admin/bot/edit.html.erb

diff --git a/app/views/admin/bot/index.html.erb b/app/views/admin/bot/index.html.erb new file mode 100644 index 0000000..df21b9c --- /dev/null +++ b/app/views/admin/bot/index.html.erb @@ -0,0 +1,39 @@ +

bot登録管理(仮)

+
+

❏ 登録

+
+<%= form_for(@bot, url: '') do |f| %> +<%= render 'shared/error_messages' %> +

<%= notice %>

+

<%= alert %>

+

twitter_name<%= f.text_field :twitter_name %>

+

twitter_id<%= f.text_field :twitter_id %>

+

access_token<%= f.text_field :access_token %>

+

hash_tags<%= f.number_field :hash_tags %>

+ +<% end %> +
+
+

❏ 一覧

+

<%= flash[:delete] %>

+
+ + + + + + + + +<% @bots.each_with_index do |b,cnt| %> + + + + + + + + +<% end %> +
No.twitter_nametwitter_idaccess_tokenhash_tags
<%= cnt+1 %><%= b.twitter_name %><%= b.twitter_id %><%= b.access_token %><%= b.hash_tags %><%= link_to('削除', "bot/#{b.id}", :method => :delete) %>
+
diff --git a/app/views/admin/bot/new.html.erb b/app/views/admin/bot/new.html.erb new file mode 100644 index 0000000..b42e541 --- /dev/null +++ b/app/views/admin/bot/new.html.erb @@ -0,0 +1,2 @@ +

Admin::Bot#new

+

Find me in app/views/admin/bot/new.html.erb

diff --git a/app/views/admin/bot/show.html.erb b/app/views/admin/bot/show.html.erb new file mode 100644 index 0000000..1318dc4 --- /dev/null +++ b/app/views/admin/bot/show.html.erb @@ -0,0 +1,2 @@ +

Admin::Bot#show

+

Find me in app/views/admin/bot/show.html.erb

diff --git a/app/views/admin/bot/update.html.erb b/app/views/admin/bot/update.html.erb new file mode 100644 index 0000000..7a54a92 --- /dev/null +++ b/app/views/admin/bot/update.html.erb @@ -0,0 +1,2 @@ +

Admin::Bot#update

+

Find me in app/views/admin/bot/update.html.erb

diff --git a/app/views/admin/manage/bot.html.erb b/app/views/admin/manage/bot.html.erb index 0be87c1..befa1b3 100644 --- a/app/views/admin/manage/bot.html.erb +++ b/app/views/admin/manage/bot.html.erb @@ -1 +1,38 @@ -bot +

bot登録管理(仮)

+
+

❏ 登録

+
+<%= form_for(@bot, url: '') do |f| %> +<%= render 'shared/error_messages' %> +

<%= notice %>

+

<%= alert %>

+

twitter_name<%= f.text_field :twitter_name %>

+

twitter_id<%= f.text_field :twitter_id %>

+

access_token<%= f.text_field :access_token %>

+

hash_tags<%= f.number_field :hash_tags %>

+ +<% end %> +
+
+

❏ 一覧

+
+ + + + + + + + +<% @bots.each_with_index do |b,cnt| %> + + + + + + + + +<% end %> +
No.twitter_nametwitter_idaccess_tokenhash_tags
<%= cnt+1 %><%= b.twitter_name %><%= b.twitter_id %><%= b.access_token %><%= b.hash_tags %><%= link_to('削除', delete_manage_path(b), :method => :delete) %>
+
diff --git a/app/views/admin/manage/index.html.erb b/app/views/admin/manage/index.html.erb index 2262de0..320437c 100644 --- a/app/views/admin/manage/index.html.erb +++ b/app/views/admin/manage/index.html.erb @@ -1 +1 @@ -hoge +<%= link_to 'Bot登録', admin_bot_index_path, :method => 'get' %> diff --git a/app/views/shared/_error_messages.html.erb b/app/views/shared/_error_messages.html.erb new file mode 100644 index 0000000..5fe73fc --- /dev/null +++ b/app/views/shared/_error_messages.html.erb @@ -0,0 +1,9 @@ +<% if @bot.errors.any? %> +
+ <%= @bot.errors.count %>つのエラーが発生: + <% @bot.errors.full_messages.each do |msg| %> +

<%= msg %>

+ <% end %> +
+
+<% end %> diff --git a/config/routes.rb b/config/routes.rb index 9c875f3..666f0a5 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,11 +1,14 @@ Rails.application.routes.draw do - namespace "admin" do + namespace :admin do # ADMIN TOP - get 'manage' => 'manage#index' + root :to => 'manage#index' + + resources :bot # BOT - get 'manage/bot' => 'manage#bot' - post 'manage/bot' => 'manage#bot' + # get 'manage/bot' => 'manage#bot' + # post 'manage/bot' => 'manage#create' + # delete 'manage/bot' => 'manage#delete' # TWITTER get 'manage/bot/twitter' => 'manage#twitter' diff --git a/db/migrate/20150310150232_create_bots.rb b/db/migrate/20150310150232_create_bots.rb new file mode 100644 index 0000000..afa264a --- /dev/null +++ b/db/migrate/20150310150232_create_bots.rb @@ -0,0 +1,12 @@ +class CreateBots < ActiveRecord::Migration + def change + create_table :bots do |t| + t.string :twitter_name + t.string :twitter_id + t.string :access_token + t.boolean :deleted + + t.timestamps null: false + end + end +end diff --git a/db/migrate/20150310150656_create_hash_tags.rb b/db/migrate/20150310150656_create_hash_tags.rb new file mode 100644 index 0000000..cb2921a --- /dev/null +++ b/db/migrate/20150310150656_create_hash_tags.rb @@ -0,0 +1,10 @@ +class CreateHashTags < ActiveRecord::Migration + def change + create_table :hash_tags do |t| + t.string :hash_tag + t.boolean :deleted + + t.timestamps null: false + end + end +end diff --git a/db/migrate/20150310150941_add_hash_tag_id_to_bots.rb b/db/migrate/20150310150941_add_hash_tag_id_to_bots.rb new file mode 100644 index 0000000..603f9f1 --- /dev/null +++ b/db/migrate/20150310150941_add_hash_tag_id_to_bots.rb @@ -0,0 +1,6 @@ +class AddHashTagIdToBots < ActiveRecord::Migration + def change + add_column :bots, :hash_tags, :integer + change_column :bots, :deleted, :boolean, :default => false + end +end diff --git a/db/migrate/20150310151917_add_default_value_to_hash_tags.rb b/db/migrate/20150310151917_add_default_value_to_hash_tags.rb new file mode 100644 index 0000000..12cb192 --- /dev/null +++ b/db/migrate/20150310151917_add_default_value_to_hash_tags.rb @@ -0,0 +1,5 @@ +class AddDefaultValueToHashTags < ActiveRecord::Migration + def change + change_column :hash_tags, :deleted, :boolean, :default => false + end +end diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 0000000..a9bd3ca --- /dev/null +++ b/db/schema.rb @@ -0,0 +1,33 @@ +# 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: 20150310151917) do + + create_table "bots", force: :cascade do |t| + t.string "twitter_name" + t.string "twitter_id" + t.string "access_token" + t.boolean "deleted", default: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.integer "hash_tags" + end + + create_table "hash_tags", force: :cascade do |t| + t.string "hash_tag" + t.boolean "deleted", default: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + +end diff --git a/test/controllers/admin/bot_controller_test.rb b/test/controllers/admin/bot_controller_test.rb new file mode 100644 index 0000000..07ce647 --- /dev/null +++ b/test/controllers/admin/bot_controller_test.rb @@ -0,0 +1,39 @@ +require 'test_helper' + +class Admin::BotControllerTest < ActionController::TestCase + test "should get index" do + get :index + assert_response :success + end + + test "should get creat" do + get :creat + assert_response :success + end + + test "should get new" do + get :new + assert_response :success + end + + test "should get edit" do + get :edit + assert_response :success + end + + test "should get show" do + get :show + assert_response :success + end + + test "should get update" do + get :update + assert_response :success + end + + test "should get destroy" do + get :destroy + assert_response :success + end + +end diff --git a/test/fixtures/bots.yml b/test/fixtures/bots.yml new file mode 100644 index 0000000..832f9e4 --- /dev/null +++ b/test/fixtures/bots.yml @@ -0,0 +1,13 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + twitter_name: MyString + twitter_id: MyString + access_token: MyString + deleted: false + +two: + twitter_name: MyString + twitter_id: MyString + access_token: MyString + deleted: false diff --git a/test/fixtures/hash_tags.yml b/test/fixtures/hash_tags.yml new file mode 100644 index 0000000..1dc1ecd --- /dev/null +++ b/test/fixtures/hash_tags.yml @@ -0,0 +1,9 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + hash_tag: MyString + deleted: false + +two: + hash_tag: MyString + deleted: false diff --git a/test/models/bot_test.rb b/test/models/bot_test.rb new file mode 100644 index 0000000..3909f2e --- /dev/null +++ b/test/models/bot_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class BotTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/models/hash_tag_test.rb b/test/models/hash_tag_test.rb new file mode 100644 index 0000000..9c79c58 --- /dev/null +++ b/test/models/hash_tag_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class HashTagTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end From 9915f1b7e44230957dea0cefc3d7de3e171a3878 Mon Sep 17 00:00:00 2001 From: Gaddict Date: Mon, 16 Mar 2015 23:25:50 +0900 Subject: [PATCH 2/2] teeeeet --- Gemfile | 4 ++ Gemfile.lock | 14 ++++++ app/controllers/admin/bot_controller.rb | 43 ++++++++++++++++++- app/models/bot.rb | 1 + app/views/admin/bot/index.html.erb | 23 ++++++++-- app/views/job/twitter/index.html.erb | 16 +++++++ config/initializers/0_settings.rb | 4 ++ config/initializers/omniauth.rb | 3 ++ config/routes.rb | 5 +++ config/settings.yml | 13 ++++++ ...50313230136_delete_column_pin_from_bots.rb | 5 +++ .../20150313230137_add_column_pin_to_bots.rb | 5 +++ .../20150313230138_add_column_acc_to_bots.rb | 6 +++ db/migrate/20150313_add_column_to_bots.rb | 5 +++ ...20150316125814_add_access_secret_to_bot.rb | 5 +++ .../20150316125910_remove_pin_from_bot.rb | 5 +++ db/schema.rb | 11 +++-- 17 files changed, 159 insertions(+), 9 deletions(-) create mode 100644 app/views/job/twitter/index.html.erb create mode 100644 config/initializers/0_settings.rb create mode 100644 config/initializers/omniauth.rb create mode 100644 config/settings.yml create mode 100644 db/migrate/20150313230136_delete_column_pin_from_bots.rb create mode 100644 db/migrate/20150313230137_add_column_pin_to_bots.rb create mode 100644 db/migrate/20150313230138_add_column_acc_to_bots.rb create mode 100644 db/migrate/20150313_add_column_to_bots.rb create mode 100644 db/migrate/20150316125814_add_access_secret_to_bot.rb create mode 100644 db/migrate/20150316125910_remove_pin_from_bot.rb diff --git a/Gemfile b/Gemfile index 5ba8dc1..f31907f 100644 --- a/Gemfile +++ b/Gemfile @@ -11,6 +11,10 @@ gem 'turbolinks' gem 'jbuilder', '~> 2.0' gem 'sdoc', '~> 0.4.0', group: :doc +gem 'omniauth' +gem 'omniauth-twitter' +gem 'settingslogic' + gem "oauth" gem 'twitter' gem "tweetstream" diff --git a/Gemfile.lock b/Gemfile.lock index bfbeb35..dd386f0 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -79,6 +79,7 @@ GEM multipart-post (>= 1.2, < 3) globalid (0.3.3) activesupport (>= 4.1.0) + hashie (3.4.0) hike (1.2.3) http (0.6.3) http_parser.rb (~> 0.6.0) @@ -107,6 +108,15 @@ GEM nokogiri (1.6.6.2) mini_portile (~> 0.6.0) oauth (0.4.7) + omniauth (1.2.2) + hashie (>= 1.2, < 4) + rack (~> 1.0) + omniauth-oauth (1.0.1) + oauth + omniauth (~> 1.0) + omniauth-twitter (1.1.0) + multi_json (~> 1.3) + omniauth-oauth (~> 1.0) rack (1.6.0) rack-test (0.6.3) rack (>= 1.0) @@ -147,6 +157,7 @@ GEM sdoc (0.4.1) json (~> 1.7, >= 1.7.7) rdoc (~> 4.0) + settingslogic (2.0.9) simple_oauth (0.3.1) slop (3.6.0) spring (1.3.3) @@ -202,9 +213,12 @@ DEPENDENCIES jbuilder (~> 2.0) jquery-rails oauth + omniauth + omniauth-twitter rails (= 4.2.0) sass-rails (~> 5.0) sdoc (~> 0.4.0) + settingslogic spring sqlite3 turbolinks diff --git a/app/controllers/admin/bot_controller.rb b/app/controllers/admin/bot_controller.rb index 1b0fd9a..4822a0f 100644 --- a/app/controllers/admin/bot_controller.rb +++ b/app/controllers/admin/bot_controller.rb @@ -1,3 +1,5 @@ +require 'oauth' + class Admin::BotController < ApplicationController NG = "ng".freeze @@ -7,9 +9,36 @@ def index @bot = Bot.new end + def update + parameters = params.require(:tweet).permit :bot_id, :content + bot = Bot.find_by_id(parameters[:bot_id]) + + puts "bot_id" + parameters[:bot_id] + + client = Twitter::REST::Client.new do |config| + config.consumer_key = Settings.twitter.consumer_key + config.consumer_secret = Settings.twitter.consumer_secret + config.access_token = bot.access_token + config.access_token_secret = bot.access_secret + end + + puts "content" + parameters[:content] + + + client.update(parameters[:content]) + + redirect_to "/admin/bot" + end + def create bots - @bot = Bot.new(bot_params) + + paramerters = bot_params + + paramerters[:access_token] = session[:access_token] + paramerters[:access_secret] = session[:access_secret] + + @bot = Bot.new(paramerters) render action: 'admin/bot/index', alert: "登録失敗!!" if @bot.nil? if @bot.save @@ -29,13 +58,23 @@ def destroy end end + def callback + auth = request.env["omniauth.auth"] + token = auth[:credentials] + + session[:access_token] = token[:token] + session[:access_secret] = token[:secret] + + redirect_to "/admin/bot" + end + private def bots @bots = Bot.find_by end def bot_params - params.require(:bot).permit :twitter_name, :twitter_id, :access_token, :hash_tags + params.require(:bot).permit :twitter_name, :twitter_id, :hash_tags end def find_destroy_bot diff --git a/app/models/bot.rb b/app/models/bot.rb index 66aac7c..3ba3d7f 100644 --- a/app/models/bot.rb +++ b/app/models/bot.rb @@ -4,6 +4,7 @@ class Bot < ActiveRecord::Base validates :twitter_name, presence: true validates :twitter_id, presence: true validates :access_token, presence: true + validates :access_secret, presence: true validates :hash_tags, presence: true # 論理削除されていないレコードを全取得 diff --git a/app/views/admin/bot/index.html.erb b/app/views/admin/bot/index.html.erb index d34adb3..ab54314 100644 --- a/app/views/admin/bot/index.html.erb +++ b/app/views/admin/bot/index.html.erb @@ -2,6 +2,8 @@

❏ 登録

+<%= link_to "Sign in with Twitter", "/auth/twitter" %> +
<%= form_for @bot, url: {action: 'create'} do |f| %> <%= render 'shared/error_messages' %> @@ -9,7 +11,6 @@

<%= alert %>

<%= f.label :twitter_name, "twitter_name" %><%= f.text_field :twitter_name %>

<%= f.label :twitter_id, "twitter_id" %><%= f.text_field :twitter_id %>

-

<%= f.label :access_token, "access_token" %><%= f.text_field :access_token %>

<%= f.label :hash_tags, "hash_tags" %><%= f.number_field :hash_tags %>

<%= button_tag('bot登録') %> <% end %> @@ -17,6 +18,18 @@
+

❏ TweetTest

+ +
+<%= form_tag "/bot/update" do %> +

Bot id<%= text_field :tweet, :bot_id %>

+

Tweet<%= text_field :tweet, :content %>

+<%= button_tag('Tweet') %> +<% end %> +
+ +
+

❏ 一覧

@@ -25,10 +38,12 @@ + - + + @@ -36,10 +51,12 @@ <%= form_tag do %> + - + + <% end %> diff --git a/app/views/job/twitter/index.html.erb b/app/views/job/twitter/index.html.erb new file mode 100644 index 0000000..2013063 --- /dev/null +++ b/app/views/job/twitter/index.html.erb @@ -0,0 +1,16 @@ +

TwitterTestView

+ +
+ +

❏ CreateTest

+ +
+<%= form_tag "/twitter/update" do %> +

Consumer Key<%= f.text_field :con_key %>

+

Consumer Secret<%= f.text_field :con_sec %>

+<%= button_tag('bot登録') %> +<% end %> +
+ + + diff --git a/config/initializers/0_settings.rb b/config/initializers/0_settings.rb new file mode 100644 index 0000000..7bd640f --- /dev/null +++ b/config/initializers/0_settings.rb @@ -0,0 +1,4 @@ +class Settings < Settingslogic + source "#{Rails.root}/config/settings.yml" + namespace Rails.env +end diff --git a/config/initializers/omniauth.rb b/config/initializers/omniauth.rb new file mode 100644 index 0000000..68eb01a --- /dev/null +++ b/config/initializers/omniauth.rb @@ -0,0 +1,3 @@ +Rails.application.config.middleware.use OmniAuth::Builder do + provider :twitter, Settings.twitter.consumer_key, Settings.twitter.consumer_secret +end diff --git a/config/routes.rb b/config/routes.rb index aee5e98..d45f667 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,9 @@ Rails.application.routes.draw do + + match '/auth/twitter/callback', to: 'admin/bot#callback', via: 'get' + + match '/bot/update', to: 'admin/bot#update', via: 'post' + # 管理機能 namespace :admin do # ADMIN TOP diff --git a/config/settings.yml b/config/settings.yml new file mode 100644 index 0000000..c20e09d --- /dev/null +++ b/config/settings.yml @@ -0,0 +1,13 @@ +defaults: &defaults + +development: + <<: *defaults + twitter: + consumer_key: CTGO18drqCEpTMbPhYucIV3Hc + consumer_secret: 90EU6sv8Bw3PRgM8H0yLrAnXoo8jXZavVx1FsibiRe1DzQAlYw + +test: + <<: *defaults + +production: + <<: *defaults diff --git a/db/migrate/20150313230136_delete_column_pin_from_bots.rb b/db/migrate/20150313230136_delete_column_pin_from_bots.rb new file mode 100644 index 0000000..43f34c1 --- /dev/null +++ b/db/migrate/20150313230136_delete_column_pin_from_bots.rb @@ -0,0 +1,5 @@ +class DeleteColumnPinFromBots < ActiveRecord::Migration + def change + remove_column :bots, :pin + end +end diff --git a/db/migrate/20150313230137_add_column_pin_to_bots.rb b/db/migrate/20150313230137_add_column_pin_to_bots.rb new file mode 100644 index 0000000..f43ad19 --- /dev/null +++ b/db/migrate/20150313230137_add_column_pin_to_bots.rb @@ -0,0 +1,5 @@ +class AddColumnPinToBots < ActiveRecord::Migration + def change + add_column :bots, :pin, :integer + end +end diff --git a/db/migrate/20150313230138_add_column_acc_to_bots.rb b/db/migrate/20150313230138_add_column_acc_to_bots.rb new file mode 100644 index 0000000..e7074b5 --- /dev/null +++ b/db/migrate/20150313230138_add_column_acc_to_bots.rb @@ -0,0 +1,6 @@ +class AddColumnAccToBots < ActiveRecord::Migration + def change + add_column :bots, :acc_key, :integer + add_column :bots, :acc_sec, :integer + end +end diff --git a/db/migrate/20150313_add_column_to_bots.rb b/db/migrate/20150313_add_column_to_bots.rb new file mode 100644 index 0000000..4d697db --- /dev/null +++ b/db/migrate/20150313_add_column_to_bots.rb @@ -0,0 +1,5 @@ +class AddColumnToBots < ActiveRecord::Migration + def change + add_column :bots, :pin, :integer + end +end diff --git a/db/migrate/20150316125814_add_access_secret_to_bot.rb b/db/migrate/20150316125814_add_access_secret_to_bot.rb new file mode 100644 index 0000000..c948c39 --- /dev/null +++ b/db/migrate/20150316125814_add_access_secret_to_bot.rb @@ -0,0 +1,5 @@ +class AddAccessSecretToBot < ActiveRecord::Migration + def change + add_column :bots, :access_secret, :string + end +end diff --git a/db/migrate/20150316125910_remove_pin_from_bot.rb b/db/migrate/20150316125910_remove_pin_from_bot.rb new file mode 100644 index 0000000..dbe3022 --- /dev/null +++ b/db/migrate/20150316125910_remove_pin_from_bot.rb @@ -0,0 +1,5 @@ +class RemovePinFromBot < ActiveRecord::Migration + def change + remove_column :bots, :pin, :integer + end +end diff --git a/db/schema.rb b/db/schema.rb index a9bd3ca..5ec3252 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,16 +11,19 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20150310151917) do +ActiveRecord::Schema.define(version: 20150316125910) do create_table "bots", force: :cascade do |t| t.string "twitter_name" t.string "twitter_id" t.string "access_token" - t.boolean "deleted", default: false - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + t.boolean "deleted", default: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false t.integer "hash_tags" + t.integer "acc_key" + t.integer "acc_sec" + t.string "access_secret" end create_table "hash_tags", force: :cascade do |t|
No.id twitter_name twitter_idaccess_token hash_tagsaccess_tokenaccess_secret 複製 削除
<%= cnt+1 %><%= b.id %> <%= text_field :bot, :twitter_name, value: b.twitter_name %> <%= text_field :bot, :twitter_id, value: b.twitter_id %><%= text_field :bot, :access_token, value: b.access_token %> <%= text_field :bot, :hash_tags, value: b.hash_tags %><%= b.access_token %><%= b.access_secret %> <%= button_tag('bot複製') %> <%= link_to('bot削除', "bot/#{b.id}", method: :delete) %>