Skip to content

Commit 41bd8ba

Browse files
author
clliu
committedAug 23, 2021
在线打分
1 parent 8b6c88a commit 41bd8ba

File tree

17 files changed

+216
-4
lines changed

17 files changed

+216
-4
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Place all the behaviors and hooks related to the matching controller here.
2+
# All this logic will automatically be available in application.js.
3+
# You can use CoffeeScript in this file: http://coffeescript.org/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Place all the styles related to the golden_idea::score_records controller here.
2+
// They will automatically be included in application.css.
3+
// You can use Sass (SCSS) here: http://sass-lang.com/

‎app/controllers/golden_idea/ideas_controller.rb

+2
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ def update
7979

8080
def show
8181
@golden_idea = Idea.find(params[:id])
82+
@score_record = @golden_idea.score_records.find_by_employee_id(current_employee.id)
83+
@score_record ||= @golden_idea.score_records.new
8284
end
8385

8486
#def show_admin
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
module GoldenIdea
2+
class ScoreRecordsController < ApplicationController
3+
def index
4+
5+
end
6+
7+
def new
8+
@score_record = ScoreRecord.find params[:id]
9+
render layout: false
10+
end
11+
12+
def edit
13+
@score_record = ScoreRecord.find params[:id]
14+
render layout: false
15+
end
16+
17+
def create
18+
@score_record = ScoreRecord.create(score_record_params)
19+
if @score_record.errors.present?
20+
flash["danger"] = "打分不成功:#{@score_record.errors.full_messages.join(",")}"
21+
else
22+
flash["success"] = "打分成功"
23+
end
24+
redirect_to golden_idea_idea_path(@score_record.idea)
25+
end
26+
27+
def update
28+
@score_record = ScoreRecord.find(params[:id])
29+
if @score_record.update_attributes(score_record_params)
30+
flash["success"] = "修改成功"
31+
else
32+
flash["danger"] = "修改不错误"
33+
end
34+
redirect_to golden_idea_idea_path(@score_record.idea)
35+
end
36+
37+
private
38+
def score_record_params
39+
params.require(:golden_idea_score_record).permit(:benefit, :cost, :risk, :idea_id)
40+
end
41+
end
42+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module GoldenIdea::ScoreRecordsHelper
2+
end

‎app/models/golden_idea/idea.rb

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ class Idea < ApplicationRecord
66
belongs_to :season, optional: true
77
belongs_to :reporter, class_name: "Employee", optional: true
88
has_many :assign_score_records
9+
has_many :score_records
910

1011
scope :top_5, ->{order(score: :desc)}
1112

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module GoldenIdea
2+
class ScoreRecord < ApplicationRecord
3+
belongs_to :idea
4+
belongs_to :employee, optional: true
5+
6+
before_create :set_employee
7+
before_save :update_score
8+
9+
validates :benefit, :cost, :risk, presence: true
10+
11+
12+
def set_employee
13+
self.employee_id = Employee.current_employee.id
14+
end
15+
16+
def update_score
17+
self.score = (benefit * 0.5) + (cost * 0.3) + (risk * 0.2)
18+
end
19+
end
20+
end

‎app/views/golden_idea/ideas/show.html.erb

+50-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<div class="modal-dialog modal-lg">
44
<div class="modal-content">
55
<div class="modal-header">
6-
<h4 class="modal-title">Edit Golden Idea</h4>
6+
<h4 class="modal-title">打分</h4>
77
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
88
<span aria-hidden="true">×</span>
99
</button>
@@ -13,7 +13,7 @@
1313
</div>
1414
<div class="modal-footer justify-content-between">
1515
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
16-
<button type="button" class="btn btn-primary" onclick="document.getElementById('golden_idea_score_form').submit();">Save changes</button>
16+
<button type="button" class="btn btn-primary" onclick="document.getElementById('score_record_form').submit();">Save changes</button>
1717
</div>
1818
</div>
1919
<!-- /.modal-content -->
@@ -64,6 +64,7 @@
6464

6565
<!-- Main content -->
6666
<section class="content">
67+
<%= render "golden_idea/application/flash" %>
6768

6869
<!-- Default box -->
6970
<div class="card">
@@ -125,7 +126,7 @@
125126
<a href="<%= request.referer %>" class="btn btn-sm btn-warning"><%= t 'back' %></a>
126127
<% if current_employee.is_golden_idea_admin? %>
127128
<%= link_to t('edit'), edit_golden_idea_idea_path(@golden_idea), class: "btn btn-sm btn-primary" %>
128-
<%= link_to t('score'), score_view_golden_idea_idea_path(@golden_idea), class: "btn btn-sm btn-primary","data-toggle"=>"modal", "data-target"=>"#modal-score", "data-remote"=>"#{score_view_golden_idea_idea_path(@golden_idea)}" %>
129+
<%# = link_to t('score'), score_view_golden_idea_idea_path(@golden_idea), class: "btn btn-sm btn-primary","data-toggle"=>"modal", "data-target"=>"#modal-score", "data-remote"=>"#{score_view_golden_idea_idea_path(@golden_idea)}" %>
129130
<%= link_to t('delete'), golden_idea_idea_path(@golden_idea), method: :delete, data: {confirm: 'Are you sure?'}, class: "btn btn-sm btn-danger" %></td>
130131
<% elsif !@golden_idea.is_complate? && @golden_idea.is_edit?(current_employee.id) %>
131132
<%= link_to t('edit'), edit_golden_idea_idea_path(@golden_idea), class: "btn btn-sm btn-primary" %>
@@ -169,6 +170,52 @@
169170

170171
</section>
171172
<!-- /.content -->
173+
174+
<!-- Main content -->
175+
<section class="content">
176+
<div class="container-fluid">
177+
<div class="row">
178+
<!-- left column -->
179+
<div class="col-md-12">
180+
<!-- jquery validation -->
181+
<div class="card card-primary">
182+
<div class="card-header">
183+
<h3 class="card-title">评分</h3>
184+
</div>
185+
<!-- /.card-header -->
186+
<!-- form start -->
187+
<% if !@score_record.new_record? %>
188+
<div class="card-body">
189+
<div>
190+
<label>
191+
Benefit: <%= @score_record.benefit %>, Cost: <%= @score_record.cost %>, Risk: <%= @score_record.risk %> <br/>
192+
Score: <%= @score_record.score %> <br/>
193+
<%= link_to t('edit'), edit_golden_idea_score_record_path(@score_record), class: "btn btn-sm btn-primary","data-toggle"=>"modal", "data-target"=>"#modal-score", "data-remote"=>"#{edit_golden_idea_score_record_path(@score_record)}" %>
194+
</label>
195+
</div>
196+
</div>
197+
<% else %>
198+
<%= render('golden_idea/score_records/new', score_record: @score_record) %>
199+
<% end %>
200+
201+
<%# if @score_record.new_record? %>
202+
<%#= render('golden_idea/score_records/new', score_record: @score_record) %>
203+
<%# else %>
204+
<%#= render('golden_idea/score_records/edit', score_record: @score_record) %>
205+
<%# end %>
206+
</div>
207+
<!-- /.card -->
208+
</div>
209+
<!--/.col (left) -->
210+
<!-- right column -->
211+
<div class="col-md-6">
212+
213+
</div>
214+
<!--/.col (right) -->
215+
</div>
216+
<!-- /.row -->
217+
</div><!-- /.container-fluid -->
218+
</section>
172219
</div>
173220
<!-- /.content-wrapper -->
174221

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<%= form_for score_record, url: golden_idea_score_records_path, html: { class: "form-horizontal row-fluid", id: "score_record_form" } do |f| %>
2+
<div class="card-body">
3+
<%= f.hidden_field :idea_id %>
4+
<div class="form-group">
5+
<label for="exampleInputEmail1">Benefit</label>
6+
<%= f.text_field :benefit, placeholder: "", class: "form-control" %>
7+
</div>
8+
<div class="form-group">
9+
<label for="exampleInputEmail1">Cost</label>
10+
<%= f.text_field :cost, placeholder: "", class: "form-control" %>
11+
</div>
12+
<div class="form-group">
13+
<label for="exampleInputEmail1">Risks</label>
14+
<%= f.text_field :risk, placeholder: "", class: "form-control" %>
15+
</div>
16+
</div>
17+
<!-- /.card-body -->
18+
<div class="card-footer">
19+
<button type="submit" class="btn btn-primary">Submit</button>
20+
</div>
21+
<% end %>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<%= form_for @score_record, url: golden_idea_score_record_path(@score_record), html: { class: "form-horizontal row-fluid", id: "score_record_form" } do |f| %>
2+
<div class="card-body">
3+
<%= f.hidden_field :idea_id %>
4+
<div class="form-group">
5+
<label for="exampleInputEmail1">Benefit</label>
6+
<%= f.text_field :benefit, placeholder: "", class: "form-control" %>
7+
</div>
8+
<div class="form-group">
9+
<label for="exampleInputEmail1">Cost</label>
10+
<%= f.text_field :cost, placeholder: "", class: "form-control" %>
11+
</div>
12+
<div class="form-group">
13+
<label for="exampleInputEmail1">Risks</label>
14+
<%= f.text_field :risk, placeholder: "", class: "form-control" %>
15+
</div>
16+
</div>
17+
<% end %>

‎config/routes.rb

+1
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@
136136
get 'reply', on: :member
137137
get 'slide_show', on: :member
138138
end
139+
resources :score_records
139140
end
140141

141142
get '/signin' => 'sessions#new'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class CreateGoldenIdeaScoreRecords < ActiveRecord::Migration[5.1]
2+
def change
3+
create_table :golden_idea_score_records do |t|
4+
t.integer :employee_id
5+
t.integer :idea_id
6+
t.decimal :score, precision: 8, scale: 3
7+
t.timestamps
8+
end
9+
end
10+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class AddBenefitCostRiskToGoldenIdeaScoreRecords < ActiveRecord::Migration[5.1]
2+
def change
3+
add_column :golden_idea_score_records, :benefit, :decimal, :precision => 8, :scale => 3
4+
add_column :golden_idea_score_records, :cost, :decimal, :precision => 8, :scale => 3
5+
add_column :golden_idea_score_records, :risk, :decimal, :precision => 8, :scale => 3
6+
end
7+
end

‎db/schema.rb

+12-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema.define(version: 20210413141202) do
13+
ActiveRecord::Schema.define(version: 20210823023551) do
1414

1515
create_table "attachments", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4" do |t|
1616
t.string "attachment"
@@ -189,6 +189,17 @@
189189
t.index ["season_id"], name: "index_golden_idea_ideas_on_season_id"
190190
end
191191

192+
create_table "golden_idea_score_records", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4" do |t|
193+
t.integer "employee_id"
194+
t.integer "idea_id"
195+
t.decimal "score", precision: 8, scale: 3
196+
t.datetime "created_at", null: false
197+
t.datetime "updated_at", null: false
198+
t.decimal "benefit", precision: 8, scale: 3
199+
t.decimal "cost", precision: 8, scale: 3
200+
t.decimal "risk", precision: 8, scale: 3
201+
end
202+
192203
create_table "golden_idea_seasons", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4" do |t|
193204
t.string "name"
194205
t.date "start_date"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
require 'test_helper'
2+
3+
class GoldenIdea::ScoreRecordsControllerTest < ActionDispatch::IntegrationTest
4+
# test "the truth" do
5+
# assert true
6+
# end
7+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
2+
3+
# This model initially had no columns defined. If you add columns to the
4+
# model remove the '{}' from the fixture names and add the columns immediately
5+
# below each fixture, per the syntax in the comments below
6+
#
7+
one: {}
8+
# column: value
9+
#
10+
two: {}
11+
# column: value
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
require 'test_helper'
2+
3+
class GoldenIdea::ScoreRecordTest < ActiveSupport::TestCase
4+
# test "the truth" do
5+
# assert true
6+
# end
7+
end

0 commit comments

Comments
 (0)
Please sign in to comment.