Skip to content

Commit

Permalink
Enable Crowd Input
Browse files Browse the repository at this point in the history
  • Loading branch information
fikriauliya committed Jul 12, 2014
1 parent 1565536 commit b541fcb
Show file tree
Hide file tree
Showing 18 changed files with 228 additions and 1 deletion.
3 changes: 3 additions & 0 deletions app/assets/javascripts/crowd_inputs.js.coffee
Original file line number Diff line number Diff line change
@@ -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/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/crowd_inputs.css.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the CrowdInputs controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
44 changes: 44 additions & 0 deletions app/controllers/crowd_inputs_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
class CrowdInputsController < ApplicationController
before_action :set_crowd_input, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!

# GET /crowd_inputs
# GET /crowd_inputs.json
def index
@crowd_inputs = CrowdInput.all
end

# GET /crowd_inputs/new
def new
@crowd_input = CrowdInput.new
@crowd_input.tps_id = Tps.random_id
end

# POST /crowd_inputs
# POST /crowd_inputs.json
def create
@crowd_input = CrowdInput.new(crowd_input_params)
@crowd_input.user_id = current_user.id

respond_to do |format|
if @crowd_input.save
format.html { redirect_to new_crowd_input_path, notice: 'Your previous input was successfully added.' }
format.json { render :show, status: :created, location: @crowd_input }
else
format.html { render :new }
format.json { render json: @crowd_input.errors, status: :unprocessable_entity }
end
end
end

private
# Use callbacks to share common setup or constraints between actions.
def set_crowd_input
@crowd_input = CrowdInput.find(params[:id])
end

# Never trust parameters from the scary internet, only allow the white list through.
def crowd_input_params
params.require(:crowd_input).permit(:user_id, :tps_id, :prabowo_count, :jokowi_count, :broken_count, :problem)
end
end
3 changes: 3 additions & 0 deletions app/controllers/pages_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
class PagesController < ApplicationController
def home
if user_signed_in?
redirect_to new_crowd_input_path
end
end
end
2 changes: 2 additions & 0 deletions app/helpers/crowd_inputs_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module CrowdInputsHelper
end
8 changes: 8 additions & 0 deletions app/models/crowd_input.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class CrowdInput < ActiveRecord::Base
belongs_to :tps
belongs_to :user

def c1_url
return tps.c1_url
end
end
4 changes: 4 additions & 0 deletions app/models/tps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@ class Tps < ActiveRecord::Base
def c1_url
return "http://scanc1.kpu.go.id/viewp.php?f=#{self.kelurahan_id}#{self.tps_id.rjust(3, '0')}04.jpg"
end

def self.random_id
return self.offset(rand(self.count)).first.id
end
end
28 changes: 28 additions & 0 deletions app/views/crowd_inputs/_form.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
.row
.col-md-3
= form_for @crowd_input do |f|
- if @crowd_input.errors.any?
#error_explanation
%h2= "#{pluralize(@crowd_input.errors.count, "error")} prohibited this crowd_input from being saved:"
%ul
- @crowd_input.errors.full_messages.each do |msg|
%li= msg
.form-group
= f.hidden_field :tps_id
.form-group
= f.label :Prabowo
= f.number_field :prabowo_count, :class => 'form-control'
.form-group
= f.label :Jokowi
= f.number_field :jokowi_count, :class => 'form-control'
.form-group
= f.label :Rusak
= f.number_field :broken_count, :class => 'form-control'
.form-group
= f.label :problem
= f.text_area :problem, :class => 'form-control'
.actions
= f.submit 'Save', :class => 'btn btn-primary'
= link_to "Lihat hasil", crowd_inputs_path, :class => 'btn btn-default'
.col-md-9
= image_tag @crowd_input.c1_url, size:"600x800"
23 changes: 23 additions & 0 deletions app/views/crowd_inputs/index.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
.row
.col-md-12
%table.table.table-striped
%tr
%th User
%th Tps
%th Prabowo
%th Jokowi
%th Broken
%th Problem

- @crowd_inputs.each do |crowd_input|
%tr
%td= crowd_input.user.name
%td= link_to crowd_input.tps_id, crowd_input.c1_url
%td= crowd_input.prabowo_count
%td= crowd_input.jokowi_count
%td= crowd_input.broken_count
%td= crowd_input.problem

%br

= link_to 'New input', new_crowd_input_path, "class" => 'btn btn-primary'
4 changes: 4 additions & 0 deletions app/views/crowd_inputs/index.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
json.array!(@crowd_inputs) do |crowd_input|
json.extract! crowd_input, :id, :user_id, :tps_id, :prabowo_count, :jokowi_count, :broken_count, :problem
json.url crowd_input_url(crowd_input, format: :json)
end
1 change: 1 addition & 0 deletions app/views/crowd_inputs/new.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
= render 'form'
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
Rails.application.routes.draw do
resources :crowd_inputs

resources :tps

get 'profiles/:id', :controller => 'profiles', :action => 'show'
Expand Down
14 changes: 14 additions & 0 deletions db/migrate/20140712052106_create_crowd_inputs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class CreateCrowdInputs < ActiveRecord::Migration
def change
create_table :crowd_inputs do |t|
t.integer :user_id
t.integer :tps_id
t.integer :prabowo_count
t.integer :jokowi_count
t.integer :broken_count
t.text :problem

t.timestamps
end
end
end
13 changes: 12 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,18 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20140712050723) do
ActiveRecord::Schema.define(version: 20140712052106) do

create_table "crowd_inputs", force: true do |t|
t.integer "user_id"
t.integer "tps_id"
t.integer "prabowo_count"
t.integer "jokowi_count"
t.integer "broken_count"
t.text "problem"
t.datetime "created_at"
t.datetime "updated_at"
end

create_table "tps", force: true do |t|
t.string "desa"
Expand Down
49 changes: 49 additions & 0 deletions test/controllers/crowd_inputs_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
require 'test_helper'

class CrowdInputsControllerTest < ActionController::TestCase
setup do
@crowd_input = crowd_inputs(:one)
end

test "should get index" do
get :index
assert_response :success
assert_not_nil assigns(:crowd_inputs)
end

test "should get new" do
get :new
assert_response :success
end

test "should create crowd_input" do
assert_difference('CrowdInput.count') do
post :create, crowd_input: { broken_count: @crowd_input.broken_count, jokowi_count: @crowd_input.jokowi_count, prabowo_count: @crowd_input.prabowo_count, problem: @crowd_input.problem, tps_id: @crowd_input.tps_id, user_id: @crowd_input.user_id }
end

assert_redirected_to crowd_input_path(assigns(:crowd_input))
end

test "should show crowd_input" do
get :show, id: @crowd_input
assert_response :success
end

test "should get edit" do
get :edit, id: @crowd_input
assert_response :success
end

test "should update crowd_input" do
patch :update, id: @crowd_input, crowd_input: { broken_count: @crowd_input.broken_count, jokowi_count: @crowd_input.jokowi_count, prabowo_count: @crowd_input.prabowo_count, problem: @crowd_input.problem, tps_id: @crowd_input.tps_id, user_id: @crowd_input.user_id }
assert_redirected_to crowd_input_path(assigns(:crowd_input))
end

test "should destroy crowd_input" do
assert_difference('CrowdInput.count', -1) do
delete :destroy, id: @crowd_input
end

assert_redirected_to crowd_inputs_path
end
end
17 changes: 17 additions & 0 deletions test/fixtures/crowd_inputs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

one:
user_id: 1
tps_id: 1
prabowo_count: 1
jokowi_count: 1
broken_count: 1
problem: MyText

two:
user_id: 1
tps_id: 1
prabowo_count: 1
jokowi_count: 1
broken_count: 1
problem: MyText
4 changes: 4 additions & 0 deletions test/helpers/crowd_inputs_helper_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require 'test_helper'

class CrowdInputsHelperTest < ActionView::TestCase
end
7 changes: 7 additions & 0 deletions test/models/crowd_input_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class CrowdInputTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

0 comments on commit b541fcb

Please sign in to comment.