#34: added new hooks, some refactoring according to Alexander's comments

This commit is contained in:
Vokhmin Alexey V 2013-04-15 17:34:11 +04:00
parent 26c7a5a711
commit f8d0320d23
5 changed files with 87 additions and 22 deletions

View File

@ -83,6 +83,10 @@ group :development do
gem 'rvm-capistrano', :require => false
gem 'cape', :require => false
gem 'capistrano_colors', :require => false
# Better Errors & RailsPanel
gem 'better_errors'
gem 'binding_of_caller'
gem 'meta_request'
end
group :test do

View File

@ -62,6 +62,11 @@ GEM
attr_encrypted (1.2.1)
encryptor (>= 1.1.1)
bcrypt-ruby (3.0.1)
better_errors (0.8.0)
coderay (>= 1.0.0)
erubis (>= 2.6.6)
binding_of_caller (0.7.1)
debug_inspector (>= 0.0.1)
blankslate (3.1.2)
bluepill (0.0.62)
activesupport (>= 3.0.0)
@ -84,6 +89,7 @@ GEM
chronic (0.6.7)
chunky_png (1.2.7)
cocaine (0.4.2)
coderay (1.0.9)
coffee-rails (3.2.2)
coffee-script (>= 2.2.0)
railties (~> 3.2.0)
@ -99,6 +105,7 @@ GEM
compass (>= 0.12.2, < 0.14)
creole (0.5.0)
daemons (1.1.9)
debug_inspector (0.0.2)
devise (2.2.3)
bcrypt-ruby (~> 3.0)
orm_adapter (~> 0.1)
@ -183,6 +190,9 @@ GEM
thin (~> 1.5.0)
meta-tags (1.2.6)
actionpack
meta_request (0.2.3)
rack-contrib
railties
metaclass (0.0.1)
mime-types (1.21)
mocha (0.13.3)
@ -239,6 +249,8 @@ GEM
rack (1.4.5)
rack-cache (1.2)
rack (>= 0.4)
rack-contrib (1.1.0)
rack (>= 0.9.1)
rack-protection (1.5.0)
rack
rack-ssl (1.3.3)
@ -396,6 +408,8 @@ DEPENDENCIES
airbrake (~> 3.1.2)
ancestry (~> 1.3.0)
attr_encrypted (= 1.2.1)
better_errors
binding_of_caller
bluepill (~> 0.0.60)
cancan (= 1.6.7)
cape
@ -420,6 +434,7 @@ DEPENDENCIES
jquery-rails (~> 2.0.2)
mailcatcher
meta-tags (~> 1.2.5)
meta_request
mock_redis (= 0.6.2)
newrelic_rpm (~> 3.5.5.38)
omniauth

View File

@ -1,24 +1,9 @@
class Hook < ActiveRecord::Base
NAMES = %w[
web
hipchat
].freeze
FIELDS = {
:web => {:url => :string},
:hipchat => {
:auth_token => :string,
:room => :string,
:restrict_to_branch => :string,
:notify => :boolean
}
}
include Modules::Models::WebHooks
belongs_to :project
before_validation :cleanup_data
validates :project_id, :presence => true
validates :data, :presence => true
validates :project_id, :data, :presence => true
validates :name, :presence => true, :inclusion => {:in => NAMES}
attr_accessible :data, :name
@ -30,9 +15,9 @@ class Hook < ActiveRecord::Base
protected
def cleanup_data
if self.name.present? && fields = FIELDS[self.name.to_sym]
if self.name.present? && fields = SCHEMA[self.name.to_sym]
new_data = {}
fields.each{ |f, t| new_data[f] = self.data[f] }
fields.each{ |type, field| new_data[field] = self.data[field] }
self.data = new_data
end
end

View File

@ -1,9 +1,12 @@
= hidden_field_tag 'hook[name]', @hook.name
- Hook::FIELDS[@hook.name.to_sym].each do |field, type|
- Hook::SCHEMA[@hook.name.to_sym].each do |type, field|
.leftlist= t("activerecord.attributes.hook.data.#{field}")
.rightlist
- name, value = "hook[data][#{field}]", @hook.data[field]
- if type == :boolean
= check_box_tag "hook[data][#{field}]", @hook.data[field], 1
= check_box_tag name, value, 1
- elsif type == :password
= password_field name, value
- else
= text_field_tag "hook[data][#{field}]", @hook.data[field]
= text_field_tag name, value
.both

View File

@ -0,0 +1,58 @@
# -*- encoding : utf-8 -*-
module Modules::Models::WebHooks
class << self
protected
def add_hook(name)
NAMES << name.to_s
@schema = []
yield if block_given?
SCHEMA[name] = @schema
@schema = []
end
def add_to_schema(type, attrs)
attrs.each do |attr|
@schema << [type, attr.to_sym]
end
end
def boolean(*attrs)
add_to_schema :boolean, attrs
end
def string(*attrs)
add_to_schema :string, attrs
end
def password(*attrs)
add_to_schema :password, attrs
end
end
NAMES = []
SCHEMA = {}
add_hook :web do
string :url
end
add_hook :hipchat do
string :auth_token, :room, :restrict_to_branch
boolean :notify
end
add_hook :irc do
string :server, :port, :room, :nick, :branch_regexes
password :password
boolean :ssl, :message_without_join, :no_colors, :long_url, :notice
end
add_hook :jabber do
string :user
end
add_hook :twitter do
string :token, :secret
boolean :digest, :short_format
end
SCHEMA.freeze
NAMES.freeze
end