parent
							
								
									17670003e2
								
							
						
					
					
						commit
						0ad1b56a27
					
				| @ -0,0 +1,2 @@ | ||||
| // Place all the behaviors and hooks related to the matching controller here.
 | ||||
| // All this logic will automatically be available in application.js.
 | ||||
| @ -0,0 +1,3 @@ | ||||
| // Place all the styles related to the trustie_hacks controller here. | ||||
| // They will automatically be included in application.css. | ||||
| // You can use Sass (SCSS) here: http://sass-lang.com/ | ||||
| @ -0,0 +1,48 @@ | ||||
| class TrustieHacksController < ApplicationController | ||||
|   before_action :require_admin, :except => [:index] | ||||
|   before_action :require_login, :except => [:index] | ||||
|   before_action :find_hackathon | ||||
|   before_action :find_hack, :except => [:create, :index] | ||||
| 
 | ||||
|   def index | ||||
|     ## 分页参数 | ||||
|     page  = params[:page]  || 1 | ||||
|     limit = params[:limit] || 16 | ||||
| 
 | ||||
|     hacks = @hackathon.trustie_hacks | ||||
|     @hackathon_users_count = hacks.sum(:hack_users_count) | ||||
| 
 | ||||
|     @hacks = hacks.page(page).per(limit) | ||||
| 
 | ||||
|   end | ||||
| 
 | ||||
|   def edit ;end | ||||
| 
 | ||||
|   def create | ||||
|     @hackathon.trustie_hacks.create!(name: params[:name], description: params[:description]) | ||||
|     render_ok | ||||
|   end | ||||
| 
 | ||||
|   def update | ||||
|     @hack.update_attributes(name: params[:name], description: params[:description]) | ||||
|   end | ||||
| 
 | ||||
|   def edit_hackathon ;end | ||||
| 
 | ||||
|   def update_hackathon | ||||
|     @hackathon.update_attributes(name: params[:name], description: params[:description]) | ||||
|   end | ||||
| 
 | ||||
| 
 | ||||
|   private | ||||
| 
 | ||||
|   def find_hackathon | ||||
|     @hackathon = TrustieHackathon.first || | ||||
|         TrustieHackathon.create(name: params[:name], description: params[:description]) | ||||
|   end | ||||
| 
 | ||||
|   def find_hack | ||||
|     @hack = TrustieHack.find params[:id] | ||||
|   end | ||||
| 
 | ||||
| end | ||||
| @ -0,0 +1,2 @@ | ||||
| module TrustieHacksHelper | ||||
| end | ||||
| @ -0,0 +1,3 @@ | ||||
| class HackUser < ApplicationRecord | ||||
|   belongs_to :trustie_hack, counter_cache: true | ||||
| end | ||||
| @ -0,0 +1,4 @@ | ||||
| class TrustieHack < ApplicationRecord | ||||
|   has_many :hack_users, :dependent => :destroy | ||||
|   belongs_to :trustie_hackathon, counter_cache: true | ||||
| end | ||||
| @ -0,0 +1,5 @@ | ||||
| class TrustieHackathon < ApplicationRecord | ||||
| 
 | ||||
|   has_many :trustie_hacks, :dependent => :destroy | ||||
| 
 | ||||
| end | ||||
| @ -0,0 +1 @@ | ||||
| json.(@hack, :id, :name, :description) | ||||
| @ -0,0 +1,2 @@ | ||||
| json.name @hackathon&.name | ||||
| json.description @hackathon&.description | ||||
| @ -0,0 +1,9 @@ | ||||
| json.hackathon do | ||||
|   json.(@hackathon, :id, :name, :description) | ||||
|   json.hackathon_users_count @hackathon_users_count | ||||
| end | ||||
| 
 | ||||
| json.hacks @hacks do |hack| | ||||
|   json.(hack, :id, :name, :description, :hack_users_count) | ||||
| end | ||||
| 
 | ||||
| @ -0,0 +1,10 @@ | ||||
| class CreateTrustieHackathons < ActiveRecord::Migration[5.2] | ||||
|   def change | ||||
|     create_table :trustie_hackathons do |t| | ||||
|       t.string :name | ||||
|       t.string :description | ||||
|       t.integer :trustie_hacks_count, default: 0 | ||||
|       t.timestamps | ||||
|     end | ||||
|   end | ||||
| end | ||||
| @ -0,0 +1,11 @@ | ||||
| class CreateTrustieHacks < ActiveRecord::Migration[5.2] | ||||
|   def change | ||||
|     create_table :trustie_hacks do |t| | ||||
|       t.string :name | ||||
|       t.string :description | ||||
|       t.references :user | ||||
|       t.integer :hack_users_count, default: 0 | ||||
|       t.timestamps | ||||
|     end | ||||
|   end | ||||
| end | ||||
| @ -0,0 +1,9 @@ | ||||
| class CreateHackUsers < ActiveRecord::Migration[5.2] | ||||
|   def change | ||||
|     create_table :hack_users do |t| | ||||
|       t.references :user | ||||
|       t.references :trustie_hack | ||||
|       t.timestamps | ||||
|     end | ||||
|   end | ||||
| end | ||||
| @ -0,0 +1,5 @@ | ||||
| require 'rails_helper' | ||||
| 
 | ||||
| RSpec.describe TrustieHacksController, type: :controller do | ||||
| 
 | ||||
| end | ||||
| @ -0,0 +1,15 @@ | ||||
| require 'rails_helper' | ||||
| 
 | ||||
| # Specs in this file have access to a helper object that includes | ||||
| # the TrustieHacksHelper. For example: | ||||
| # | ||||
| # describe TrustieHacksHelper 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 | ||||
| RSpec.describe TrustieHacksHelper, type: :helper do | ||||
|   pending "add some examples to (or delete) #{__FILE__}" | ||||
| end | ||||
| @ -0,0 +1,5 @@ | ||||
| require 'rails_helper' | ||||
| 
 | ||||
| RSpec.describe HackUser, type: :model do | ||||
|   pending "add some examples to (or delete) #{__FILE__}" | ||||
| end | ||||
| @ -0,0 +1,5 @@ | ||||
| require 'rails_helper' | ||||
| 
 | ||||
| RSpec.describe TrustieHack, type: :model do | ||||
|   pending "add some examples to (or delete) #{__FILE__}" | ||||
| end | ||||
| @ -0,0 +1,5 @@ | ||||
| require 'rails_helper' | ||||
| 
 | ||||
| RSpec.describe TrustieHackathon, type: :model do | ||||
|   pending "add some examples to (or delete) #{__FILE__}" | ||||
| end | ||||
					Loading…
					
					
				
		Reference in new issue