From 874f180655d35fda1decf6bfb1f39caf63fc1ff6 Mon Sep 17 00:00:00 2001
From: saltfactory <saltfactory@gmail.com>
Date: Tue, 19 Jan 2016 16:42:59 +0900
Subject: [PATCH] =?UTF-8?q?=EC=82=AC=EC=9A=A9=EC=9E=90=20=ED=95=84?=
 =?UTF-8?q?=EB=93=9C=EB=A5=BC=20=EC=B6=94=EA=B0=80=ED=95=98=EA=B8=B0=20?=
 =?UTF-8?q?=EC=9C=84=ED=95=9C=20expander=20=EB=AA=A8=EB=8D=B8=20=EC=B6=94?=
 =?UTF-8?q?=EA=B0=80?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

설교 게시판에 설교말씀 등 추가적인 사용자 필드를 유동적으로 관리하기 위한 모델
---
 rails/sshb/app/controllers/articles_controller.rb        | 12 ++++++------
 rails/sshb/app/models/article.rb                         |  4 +++-
 rails/sshb/app/models/expander.rb                        |  3 +++
 rails/sshb/app/views/articles/show.html.erb              |  9 ++++++++-
 rails/sshb/app/views/layouts/_header.html.erb            |  1 +
 rails/sshb/db/migrate/20160118155446_create_expanders.rb | 11 +++++++++++
 rails/sshb/db/schema.rb                                  | 13 ++++++++++++-
 rails/sshb/db/seeds.rb                                   |  8 ++++++++
 rails/sshb/test/fixtures/expanders.yml                   | 13 +++++++++++++
 rails/sshb/test/models/expander_test.rb                  |  7 +++++++
 10 files changed, 72 insertions(+), 9 deletions(-)
 create mode 100644 rails/sshb/app/models/expander.rb
 create mode 100644 rails/sshb/db/migrate/20160118155446_create_expanders.rb
 create mode 100644 rails/sshb/test/fixtures/expanders.yml
 create mode 100644 rails/sshb/test/models/expander_test.rb

diff --git a/rails/sshb/app/controllers/articles_controller.rb b/rails/sshb/app/controllers/articles_controller.rb
index a03bde1..a49488f 100644
--- a/rails/sshb/app/controllers/articles_controller.rb
+++ b/rails/sshb/app/controllers/articles_controller.rb
@@ -7,12 +7,12 @@ class ArticlesController < ApplicationController
   # GET /articles
   # GET /articles.json
   def index
-
-    if params[:board_id]
-      @articles = Article.all.order("id desc").page(params[:page])
-    else
-      @articles = Article.where({board: @board}).order("id desc").page(params[:page])
-    end
+    @articles = Article.where({board: @board}).order("id desc").page(params[:page])
+    # if params[:board_id]
+    #   @articles = Article.all.order("id desc").page(params[:page])
+    # else
+    #   @articles = Article.where({board: @board}).order("id desc").page(params[:page])
+    # end
 
     # if user_signed_in?
     #   @articles = Article.where('board_id = ? and (published = "t" or (author_id = ? and published = "f"))', @board.id, current_user.id)
diff --git a/rails/sshb/app/models/article.rb b/rails/sshb/app/models/article.rb
index 31459d7..a4915a5 100644
--- a/rails/sshb/app/models/article.rb
+++ b/rails/sshb/app/models/article.rb
@@ -4,8 +4,10 @@ class Article < ActiveRecord::Base
   belongs_to :author, polymorphic: true
   belongs_to :board
 
-  has_many :attachments, as: :attachable
+  has_many :expanders, as: :expandable, autosave: true
+  accepts_nested_attributes_for :expanders
 
+  has_many :attachments, as: :attachable
   accepts_nested_attributes_for :attachments
   # , allow_destroy: true
 
diff --git a/rails/sshb/app/models/expander.rb b/rails/sshb/app/models/expander.rb
new file mode 100644
index 0000000..61d626e
--- /dev/null
+++ b/rails/sshb/app/models/expander.rb
@@ -0,0 +1,3 @@
+class Expander < ActiveRecord::Base
+  belongs_to :expandable, polymorphic: true
+end
diff --git a/rails/sshb/app/views/articles/show.html.erb b/rails/sshb/app/views/articles/show.html.erb
index 135936a..cda5962 100644
--- a/rails/sshb/app/views/articles/show.html.erb
+++ b/rails/sshb/app/views/articles/show.html.erb
@@ -5,6 +5,13 @@
   <%= @article.subject %>
 </p>
 
+<% @article.expanders.each do |expander| %>
+  <p>
+    <strong><%= expander.name %></strong>
+    <%= expander.value %>
+  </p>
+<% end %>
+
 <p>
   <strong>Content:</strong>
   <%= @article.content.html_safe %>
@@ -32,4 +39,4 @@
 </p>
 
 <%= link_to 'Edit', edit_board_article_path(@article.board, @article) %> |
-<%= link_to 'Back', board_articles_path %>
+<%= link_to 'Back', board_articles_path(@article.board) %>
diff --git a/rails/sshb/app/views/layouts/_header.html.erb b/rails/sshb/app/views/layouts/_header.html.erb
index b233f89..525a4cb 100644
--- a/rails/sshb/app/views/layouts/_header.html.erb
+++ b/rails/sshb/app/views/layouts/_header.html.erb
@@ -21,6 +21,7 @@
           <li><%= link_to "내 정보", profile_path %></li>
           <li><%= link_to "로그아웃", destroy_user_session_path, method: :delete %></li>
         <% else %>
+          <li><%= link_to "회원가입", new_user_registration_path %></li>
           <li><%= link_to "로그인", new_user_session_path %></li>
           <li></li>
       <% end %>
diff --git a/rails/sshb/db/migrate/20160118155446_create_expanders.rb b/rails/sshb/db/migrate/20160118155446_create_expanders.rb
new file mode 100644
index 0000000..1a98586
--- /dev/null
+++ b/rails/sshb/db/migrate/20160118155446_create_expanders.rb
@@ -0,0 +1,11 @@
+class CreateExpanders < ActiveRecord::Migration
+  def change
+    create_table :expanders do |t|
+      t.string :name
+      t.string :value
+      t.references :expandable, polymorphic: true, index: true
+
+      t.timestamps null: false
+    end
+  end
+end
diff --git a/rails/sshb/db/schema.rb b/rails/sshb/db/schema.rb
index 8454e40..4d2e4ea 100644
--- a/rails/sshb/db/schema.rb
+++ b/rails/sshb/db/schema.rb
@@ -11,7 +11,7 @@
 #
 # It's strongly recommended that you check this file into your version control system.
 
-ActiveRecord::Schema.define(version: 20160118141512) do
+ActiveRecord::Schema.define(version: 20160118155446) do
 
   create_table "articles", force: :cascade do |t|
     t.string   "subject"
@@ -61,6 +61,17 @@
     t.datetime "updated_at",  null: false
   end
 
+  create_table "expanders", force: :cascade do |t|
+    t.string   "name"
+    t.string   "value"
+    t.integer  "expandable_id"
+    t.string   "expandable_type"
+    t.datetime "created_at",      null: false
+    t.datetime "updated_at",      null: false
+  end
+
+  add_index "expanders", ["expandable_type", "expandable_id"], name: "index_expanders_on_expandable_type_and_expandable_id"
+
   create_table "ownerships", force: :cascade do |t|
     t.integer  "user_id"
     t.integer  "ownerable_id"
diff --git a/rails/sshb/db/seeds.rb b/rails/sshb/db/seeds.rb
index 352be3f..ad40915 100644
--- a/rails/sshb/db/seeds.rb
+++ b/rails/sshb/db/seeds.rb
@@ -37,6 +37,14 @@
 #
 Article.find_or_create_by({id: 1}) do |article|
   article.author = User.first
+  article.board = Board.find_by_slug("sermons")
+  article.subject = "test sermons"
+  article.content = "test sermons content"
+end
+
+
+Article.find_or_create_by({id: 2}) do |article|
+  article.author = User.first
   article.board = Board.find_by_slug("informs")
   article.subject = "test"
   article.content = "test"
diff --git a/rails/sshb/test/fixtures/expanders.yml b/rails/sshb/test/fixtures/expanders.yml
new file mode 100644
index 0000000..040806a
--- /dev/null
+++ b/rails/sshb/test/fixtures/expanders.yml
@@ -0,0 +1,13 @@
+# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
+
+one:
+  name: MyString
+  value: MyString
+  expandable_id: 
+  expandable_type: Expandable
+
+two:
+  name: MyString
+  value: MyString
+  expandable_id: 
+  expandable_type: Expandable
diff --git a/rails/sshb/test/models/expander_test.rb b/rails/sshb/test/models/expander_test.rb
new file mode 100644
index 0000000..dea9142
--- /dev/null
+++ b/rails/sshb/test/models/expander_test.rb
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class ExpanderTest < ActiveSupport::TestCase
+  # test "the truth" do
+  #   assert true
+  # end
+end
