Skip to content
Browse files

사용자 필드를 추가하기 위한 expander 모델 추가

설교 게시판에 설교말씀 등 추가적인 사용자 필드를 유동적으로 관리하기 위한 모델
  • Loading branch information...
1 parent f3630c4 commit 874f180655d35fda1decf6bfb1f39caf63fc1ff6 @saltfactory saltfactory committed
View
12 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)
View
4 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
View
3 rails/sshb/app/models/expander.rb
@@ -0,0 +1,3 @@
+class Expander < ActiveRecord::Base
+ belongs_to :expandable, polymorphic: true
+end
View
9 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) %>
View
1 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 %>
View
11 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
View
13 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"
View
8 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"
View
13 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
View
7 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

0 comments on commit 874f180

Please sign in to comment.
Something went wrong with that request. Please try again.