Class: Service

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Sortable
Defined in:
app/models/service.rb

Overview

To add new service you should build a class inherited from Service and implement a set of methods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.available_services_namesObject


188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'app/models/service.rb', line 188

def self.available_services_names
  %w(
    asana
    assembla
    bamboo
    buildkite
    builds_email
    campfire
    custom_issue_tracker
    drone_ci
    emails_on_push
    external_wiki
    flowdock
    gemnasium
    hipchat
    irker
    jira
    pivotaltracker
    pushover
    redmine
    slack
    teamcity
  )
end

.boolean_accessor(*args) ⇒ Object

Provide convenient boolean accessor methods for each serialized property. Also keep track of updated properties in a similar way as ActiveModel::Dirty


152
153
154
155
156
157
158
159
160
161
162
# File 'app/models/service.rb', line 152

def self.boolean_accessor(*args)
  self.prop_accessor(*args)

  args.each do |arg|
    class_eval %{
      def #{arg}?
        ActiveRecord::ConnectionAdapters::Column::TRUE_VALUES.include?(#{arg})
      end
    }
  end
end

.create_from_template(project_id, template) ⇒ Object


213
214
215
216
217
218
# File 'app/models/service.rb', line 213

def self.create_from_template(project_id, template)
  service = template.dup
  service.template = false
  service.project_id = project_id
  service if service.save
end

.prop_accessor(*args) ⇒ Object

Provide convenient accessor methods for each serialized property. Also keep track of updated properties in a similar way as ActiveModel::Dirty


122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'app/models/service.rb', line 122

def self.prop_accessor(*args)
  args.each do |arg|
    class_eval %{
      def #{arg}
        properties['#{arg}']
      end

      def #{arg}=(value)
        updated_properties['#{arg}'] = #{arg} unless #{arg}_changed?
        self.properties['#{arg}'] = value
      end

      def #{arg}_changed?
        #{arg}_touched? && #{arg} != #{arg}_was
      end

      def #{arg}_touched?
        updated_properties.include?('#{arg}')
      end

      def #{arg}_was
        updated_properties['#{arg}']
      end
    }
  end
end

Instance Method Details

#activated?Boolean

Returns:

  • (Boolean)

64
65
66
# File 'app/models/service.rb', line 64

def activated?
  active
end

#async_execute(data) ⇒ Object


178
179
180
181
182
# File 'app/models/service.rb', line 178

def async_execute(data)
  return unless supported_events.include?(data[:object_kind])

  Sidekiq::Client.enqueue(ProjectServiceWorker, id, data)
end

#can_test?Boolean

Returns:

  • (Boolean)

115
116
117
# File 'app/models/service.rb', line 115

def can_test?
  !project.empty_repo?
end

#categoryObject


72
73
74
# File 'app/models/service.rb', line 72

def category
  read_attribute(:category).to_sym
end

#descriptionObject


84
85
86
# File 'app/models/service.rb', line 84

def description
  # implement inside child
end

#execute(data) ⇒ Object


105
106
107
# File 'app/models/service.rb', line 105

def execute(data)
  # implement inside child
end

#fieldsObject


96
97
98
99
# File 'app/models/service.rb', line 96

def fields
  # implement inside child
  []
end

#helpObject


88
89
90
# File 'app/models/service.rb', line 88

def help
  # implement inside child
end

#initialize_propertiesObject


76
77
78
# File 'app/models/service.rb', line 76

def initialize_properties
  self.properties = {} if properties.nil?
end

#issue_tracker?Boolean

Returns:

  • (Boolean)

184
185
186
# File 'app/models/service.rb', line 184

def issue_tracker?
  self.category == :issue_tracker
end

#reset_updated_propertiesObject


174
175
176
# File 'app/models/service.rb', line 174

def reset_updated_properties
  @updated_properties = nil
end

#supported_eventsObject


101
102
103
# File 'app/models/service.rb', line 101

def supported_events
  %w(push tag_push issue merge_request wiki_page)
end

#template?Boolean

Returns:

  • (Boolean)

68
69
70
# File 'app/models/service.rb', line 68

def template?
  template
end

#test(data) ⇒ Object


109
110
111
112
113
# File 'app/models/service.rb', line 109

def test(data)
  # default implementation
  result = execute(data)
  { success: result.present?, result: result }
end

#titleObject


80
81
82
# File 'app/models/service.rb', line 80

def title
  # implement inside child
end

#to_paramObject


92
93
94
# File 'app/models/service.rb', line 92

def to_param
  # implement inside child
end

#updated_propertiesObject

Returns a hash of the properties that have been assigned a new value since last save, indicating their original values (attr => original value). ActiveRecord does not provide a mechanism to track changes in serialized keys, so we need a specific implementation for service properties. This allows to track changes to properties set with the accessor methods, but not direct manipulation of properties hash.


170
171
172
# File 'app/models/service.rb', line 170

def updated_properties
  @updated_properties ||= ActiveSupport::HashWithIndifferentAccess.new
end