diff --git a/app/controllers/projects/hooks_controller.rb b/app/controllers/projects/hooks_controller.rb index 35763082c..f95a19801 100644 --- a/app/controllers/projects/hooks_controller.rb +++ b/app/controllers/projects/hooks_controller.rb @@ -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 } - else - format.html { render action: "new" } - format.json { render json: @hook.errors, status: :unprocessable_entity } - end + if @hook.save + redirect_to project_hooks_path(@project, :name => @hook.name), :notice => 'Hook was successfully created.' + else + 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 } - else - format.html { render action: "edit" } - format.json { render json: @hook.errors, status: :unprocessable_entity } - end + if @hook.update_attributes(params[:hook]) + redirect_to project_hooks_path(@project, :name => @hook.name), :notice => 'Hook was successfully updated.' + else + flash[:error] = t('flash.hook.save_error') + flash[:warning] = @hook.errors.full_messages.join('. ') + render :edit end end diff --git a/app/helpers/hooks_helper.rb b/app/helpers/hooks_helper.rb index 14354af9c..5b26f317d 100644 --- a/app/helpers/hooks_helper.rb +++ b/app/helpers/hooks_helper.rb @@ -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 diff --git a/app/models/hook.rb b/app/models/hook.rb index de6bee945..ec5c38a5c 100644 --- a/app/models/hook.rb +++ b/app/models/hook.rb @@ -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 diff --git a/app/views/projects/hooks/_form.html.haml b/app/views/projects/hooks/_form.html.haml index ca390ed16..82b495912 100644 --- a/app/views/projects/hooks/_form.html.haml +++ b/app/views/projects/hooks/_form.html.haml @@ -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 \ No newline at end of file diff --git a/app/views/projects/hooks/edit.html.haml b/app/views/projects/hooks/edit.html.haml index 104620b6c..ae359439e 100644 --- a/app/views/projects/hooks/edit.html.haml +++ b/app/views/projects/hooks/edit.html.haml @@ -4,8 +4,7 @@ %h1 Editing hook -= render 'form' - -= link_to 'Show', @hook -\| -= link_to 'Back', project_hooks_path(@project) += form_for @hook, :url => project_hook_path(@project, @hook), :method => :put, :html => { :class => :form } do |f| + = render 'form' + .actions + = f.submit 'Update' \ No newline at end of file diff --git a/app/views/projects/hooks/index.html.haml b/app/views/projects/hooks/index.html.haml index bad0ec5b2..cf477132c 100644 --- a/app/views/projects/hooks/index.html.haml +++ b/app/views/projects/hooks/index.html.haml @@ -4,22 +4,15 @@ %h1 Listing hooks -%table - %tr - %th Data - %th Type - %th - %th - %th - - - @hooks.each do |hook| +%table#datatable.tablesorter{:cellpadding => "0", :cellspacing => "0"} + %thead %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) + %th.th1= t('activerecord.attributes.hook.service') + %th + %tbody + - Hook::NAMES.each do |name| + %tr + %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' diff --git a/app/views/projects/hooks/new.html.haml b/app/views/projects/hooks/new.html.haml index eac8e7135..7b93ea425 100644 --- a/app/views/projects/hooks/new.html.haml +++ b/app/views/projects/hooks/new.html.haml @@ -4,6 +4,7 @@ %h1 New hook -= render 'form' - -= link_to 'Back', project_hooks_path(@project) += form_for @hook, :url => project_hooks_path(@project), :method => :post, :html => { :class => :form } do |f| + = render 'form' + .actions + = f.submit 'Save' diff --git a/app/views/projects/hooks/show.html.haml b/app/views/projects/hooks/show.html.haml index 0ebe9b618..267f4e034 100644 --- a/app/views/projects/hooks/show.html.haml +++ b/app/views/projects/hooks/show.html.haml @@ -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 diff --git a/db/migrate/20130412124536_create_hooks.rb b/db/migrate/20130412124536_create_hooks.rb index 134c97dc9..cdfbc1857 100644 --- a/db/migrate/20130412124536_create_hooks.rb +++ b/db/migrate/20130412124536_create_hooks.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index c67784a1f..d2fe3a76b 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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