#34: update controller, views, model of Hook

This commit is contained in:
Vokhmin Alexey V 2013-04-14 19:46:56 +04:00
parent d28a2b0173
commit 9e286bb5a2
10 changed files with 86 additions and 101 deletions

View File

@ -6,34 +6,19 @@ class Projects::HooksController < Projects::BaseController
# GET /../hooks
# GET /../hooks.json
def index
@hooks = @project.hooks.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @hooks }
end
end
# GET /../hooks/1
# GET /../hooks/1.json
def show
@hook = @project.hooks.find params[:id]
respond_to do |format|
format.html # show.html.erb
format.json { render json: @hook }
@name = params[:name]
@hooks = @project.hooks.for_name(@name).order('name asc, created_at asc')
if @name.present?
render :show
else
render :index
end
end
# GET /../hooks/new
# GET /../hooks/new.json
def new
@hook = @project.hooks.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @hook }
end
@hook = @project.hooks.new(params[:hook])
end
# GET /../hooks/1/edit
@ -45,15 +30,12 @@ class Projects::HooksController < Projects::BaseController
# POST /../hooks.json
def create
@hook = @project.hooks.new params[:hook]
respond_to do |format|
if @hook.save
format.html { redirect_to @hook, notice: 'Hook was successfully created.' }
format.json { render json: @hook, status: :created, location: @hook }
redirect_to project_hooks_path(@project, :name => @hook.name), :notice => 'Hook was successfully created.'
else
format.html { render action: "new" }
format.json { render json: @hook.errors, status: :unprocessable_entity }
end
flash[:error] = t('flash.hook.save_error')
flash[:warning] = @hook.errors.full_messages.join('. ')
render :new
end
end
@ -61,15 +43,12 @@ class Projects::HooksController < Projects::BaseController
# PUT /../hooks/1.json
def update
@hook = @project.hooks.find params[:id]
respond_to do |format|
if @hook.update_attributes(params[:hook])
format.html { redirect_to @hook, notice: 'Hook was successfully updated.' }
format.json { head :no_content }
redirect_to project_hooks_path(@project, :name => @hook.name), :notice => 'Hook was successfully updated.'
else
format.html { render action: "edit" }
format.json { render json: @hook.errors, status: :unprocessable_entity }
end
flash[:error] = t('flash.hook.save_error')
flash[:warning] = @hook.errors.full_messages.join('. ')
render :edit
end
end

View File

@ -1,2 +1,16 @@
module HooksHelper
def web_fields
{:url => :string}
end
def hipchat_fields
{
:auth_token => :string,
:room => :string,
:restrict_to_branch => :string,
:notify => :boolean
}
end
end

View File

@ -1,14 +1,19 @@
class Hook < ActiveRecord::Base
TYPES = [1, 2]
NAMES = %w[
web
hipchat
].freeze
belongs_to :project
validates :project_id, :presence => true
validates :data, :presence => true
validates :type, :presence => true, :inclusion => {:in => TYPES}
validates :type, :uniqueness => {:scope => :project_id}
validates :name, :presence => true, :inclusion => {:in => NAMES}
attr_accessible :data, :type
attr_accessible :data, :name
serialize :data, Hash
scope :for_name, lambda {|name| where(:name => name) if name.present? }
end

View File

@ -1,16 +1,9 @@
= form_for [@project, @hook] do |f|
- if @hook.errors.any?
#error_explanation
%h2= "#{pluralize(@hook.errors.count, "error")} prohibited this hook from being saved:"
%ul
- @hook.errors.full_messages.each do |msg|
%li= msg
.field
= f.label :type
= f.select :type, Hook::TYPES
.field
= f.label :data
= f.text_area :data
.actions
= f.submit 'Save'
= hidden_field_tag 'hook[name]', @hook.name
- send("#{@hook.name}_fields").each do |field, type|
.leftlist= field
.rightlist
- if type == :boolean
= check_box_tag "hook[data][#{field}]", @hook.data[field], 1
- else
= text_field_tag "hook[data][#{field}]", @hook.data[field]
.both

View File

@ -4,8 +4,7 @@
%h1 Editing hook
= form_for @hook, :url => project_hook_path(@project, @hook), :method => :put, :html => { :class => :form } do |f|
= render 'form'
= link_to 'Show', @hook
\|
= link_to 'Back', project_hooks_path(@project)
.actions
= f.submit 'Update'

View File

@ -4,22 +4,15 @@
%h1 Listing hooks
%table
%table#datatable.tablesorter{:cellpadding => "0", :cellspacing => "0"}
%thead
%tr
%th Data
%th Type
%th.th1= t('activerecord.attributes.hook.service')
%th
%th
%th
- @hooks.each do |hook|
%tbody
- Hook::NAMES.each do |name|
%tr
%td= hook.data
%td= hook.type
%td= link_to 'Show', hook
%td= link_to 'Edit', edit_project_hook_path(hook)
%td= link_to 'Destroy', hook, method: :delete, data: { confirm: 'Are you sure?' }
%br
= link_to 'New Hook', new_project_hook_path(@project)
%td
= link_to "#{name} (#{@hooks.where(:name => name).count})", project_hooks_path(@project, :name => name)
%td.right
= link_to 'New', new_project_hook_path(@project, :hook => {:name => name}), :class => 'button'

View File

@ -4,6 +4,7 @@
%h1 New hook
= form_for @hook, :url => project_hooks_path(@project), :method => :post, :html => { :class => :form } do |f|
= render 'form'
= link_to 'Back', project_hooks_path(@project)
.actions
= f.submit 'Save'

View File

@ -2,18 +2,19 @@
= render 'submenu'
= render 'sidebar'
%p#notice= notice
%h1= "Listing of #{@name}"
%p
%b Data:
= @hook.data
%p
%b Project:
= @hook.project_id
%p
%b Type:
= @hook.type
= link_to 'New', new_project_hook_path(@project, :hook => {:name => @name}), :class => 'button'
.booth
%br
= link_to 'Edit', edit_project_hook_path(@hook)
\|
= link_to 'Back', project_hooks_path
- @hooks.all.each do |hook|
- hook.data.each do |k, v|
.leftlist= k
.rightlist= v
.both
.leftlist
.rightlist
= link_to 'Edit', edit_project_hook_path(@project, hook), :class => 'button'
.hr

View File

@ -3,7 +3,7 @@ class CreateHooks < ActiveRecord::Migration
create_table :hooks do |t|
t.text :data
t.integer :project_id
t.integer :type
t.string :name
t.timestamps
end

View File

@ -207,7 +207,7 @@ ActiveRecord::Schema.define(:version => 20130412124536) do
create_table "hooks", :force => true do |t|
t.text "data"
t.integer "project_id"
t.integer "type"
t.string "name"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end