central-support 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +13 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +14 -0
  5. data/Gemfile +3 -0
  6. data/LICENSE.txt +21 -0
  7. data/README.md +49 -0
  8. data/Rakefile +6 -0
  9. data/bin/console +14 -0
  10. data/bin/setup +8 -0
  11. data/central-support.gemspec +30 -0
  12. data/lib/central/support.rb +27 -0
  13. data/lib/central/support/concerns/activity_concern.rb +4 -0
  14. data/lib/central/support/concerns/activity_concern/associations.rb +15 -0
  15. data/lib/central/support/concerns/activity_concern/callbacks.rb +25 -0
  16. data/lib/central/support/concerns/activity_concern/scopes.rb +55 -0
  17. data/lib/central/support/concerns/activity_concern/validations.rb +17 -0
  18. data/lib/central/support/concerns/project_concern.rb +5 -0
  19. data/lib/central/support/concerns/project_concern/associations.rb +27 -0
  20. data/lib/central/support/concerns/project_concern/attributes.rb +25 -0
  21. data/lib/central/support/concerns/project_concern/csv.rb +59 -0
  22. data/lib/central/support/concerns/project_concern/scopes.rb +26 -0
  23. data/lib/central/support/concerns/project_concern/validations.rb +43 -0
  24. data/lib/central/support/concerns/story_concern.rb +7 -0
  25. data/lib/central/support/concerns/story_concern/associations.rb +23 -0
  26. data/lib/central/support/concerns/story_concern/attributes.rb +29 -0
  27. data/lib/central/support/concerns/story_concern/callbacks.rb +50 -0
  28. data/lib/central/support/concerns/story_concern/csv.rb +81 -0
  29. data/lib/central/support/concerns/story_concern/scopes.rb +16 -0
  30. data/lib/central/support/concerns/story_concern/transitions.rb +71 -0
  31. data/lib/central/support/concerns/story_concern/validations.rb +45 -0
  32. data/lib/central/support/concerns/team_concern.rb +4 -0
  33. data/lib/central/support/concerns/team_concern/associations.rb +29 -0
  34. data/lib/central/support/concerns/team_concern/domain_validator.rb +19 -0
  35. data/lib/central/support/concerns/team_concern/scopes.rb +15 -0
  36. data/lib/central/support/concerns/team_concern/validations.rb +13 -0
  37. data/lib/central/support/concerns/user_concern.rb +3 -0
  38. data/lib/central/support/concerns/user_concern/associations.rb +21 -0
  39. data/lib/central/support/concerns/user_concern/callbacks.rb +39 -0
  40. data/lib/central/support/concerns/user_concern/validations.rb +14 -0
  41. data/lib/central/support/iteration.rb +51 -0
  42. data/lib/central/support/iteration_service.rb +213 -0
  43. data/lib/central/support/mattermost.rb +36 -0
  44. data/lib/central/support/validators/belongs_to_project_validator.rb +11 -0
  45. data/lib/central/support/validators/changed_validator.rb +7 -0
  46. data/lib/central/support/validators/estimate_validator.rb +11 -0
  47. data/lib/central/support/version.rb +5 -0
  48. metadata +146 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 451d424f9bc9a13d968d8eade8c6a63552cb6bed
4
+ data.tar.gz: 5302040325454b5352264f0a1391612c8c205f87
5
+ SHA512:
6
+ metadata.gz: 662a9c310d13e5774305c3f1f0b5e1b316c769c0e0fc867e8e2d9431de11025f174b558a901c77e1842d1b59759b9f8ee79d9c92d47a355f6eb6fa4e64b72a35
7
+ data.tar.gz: 03d2b03f614ea4b32d5ac53a48e2c0802abdadd177f607bb7b0b53b79a8f61385e79953605caf1961a318a84343a9add473b2c4fb196275953af49017ef9114f
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.log
11
+ *.sqlite3
12
+ *.gem
13
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,14 @@
1
+ sudo: false
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.3.1
6
+ before_install: gem install bundler -v 1.12.5
7
+
8
+ before_script:
9
+ - "cd spec/support/rails_app"
10
+ - "bundle install"
11
+ - "cd ../../.."
12
+
13
+ script:
14
+ - "bundle exec rake spec"
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ eval_gemfile File.join(File.dirname(__FILE__), "spec/support/rails_app/Gemfile")
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 TODO: Write your name
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,49 @@
1
+ # Central::Support
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/central/support`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ This is a library that moves away some of the logic from [Central](https://github.com/Codeminer42/cm42-central) so we can reuse in other projects. Particularly the Iteration calculation.
6
+
7
+ [![Code Climate](https://codeclimate.com/repos/57e072af8a0f4603260024d5/badges/3ed30d50ad1a44162204/gpa.svg)](https://codeclimate.com/repos/57e072af8a0f4603260024d5/feed)
8
+ [![Test Coverage](https://codeclimate.com/repos/57e072af8a0f4603260024d5/badges/3ed30d50ad1a44162204/coverage.svg)](https://codeclimate.com/repos/57e072af8a0f4603260024d5/coverage)
9
+ [![Build Status](https://travis-ci.org/Codeminer42/cm42-central-support.svg?branch=master)](https://travis-ci.org/Codeminer42/cm42-central-support0)
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'central-support'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install central-support
26
+
27
+ ## Usage
28
+
29
+ This library expects a very specific set of pre-existing models (Team, Project, Story, User, Activity, Enrollment, Membership, and Ownership).
30
+
31
+ They are all models that exist in Central and they must also follow a specific database schema.
32
+
33
+ If you want to use this library in another application, refer to the `spec/support/rails_app/` so you know exactly how your new app should look like.
34
+
35
+ ## Development
36
+
37
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
38
+
39
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
40
+
41
+ ## Contributing
42
+
43
+ Bug reports and pull requests are welcome on GitHub at https://github.com/Codeminer-42/central-support.
44
+
45
+
46
+ ## License
47
+
48
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
49
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "central/support"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ # frozen_string_literal: true
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'central/support/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "central-support"
9
+ spec.version = Central::Support::VERSION
10
+ spec.authors = ["Fabio Akita"]
11
+ spec.email = ["boss@akitaonrails.com"]
12
+
13
+ spec.summary = %q{Support library for the Central project}
14
+ spec.description = %q{Extraction of part of the intelligence behind Central}
15
+ spec.homepage = "https://github.com/Codeminer-42/central-support"
16
+ spec.license = "MIT"
17
+
18
+ spec.required_ruby_version = '>= 2.0'
19
+
20
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
22
+ spec.bindir = "exe"
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.add_runtime_dependency "activesupport", "~> 3.2"
27
+ spec.add_runtime_dependency "activerecord", "~> 3.2"
28
+ spec.add_runtime_dependency 'enumerize', "~> 2.0"
29
+ spec.add_runtime_dependency 'transitions', "~> 0.1.9"
30
+ end
@@ -0,0 +1,27 @@
1
+ require 'csv'
2
+
3
+ require "central/support/version"
4
+ require "central/support/iteration_service"
5
+ require "central/support/iteration"
6
+ require "central/support/mattermost"
7
+
8
+ require 'enumerize'
9
+ require 'transitions'
10
+ require 'active_record/transitions'
11
+ require 'active_support/concern'
12
+
13
+ require 'central/support/validators/belongs_to_project_validator'
14
+ require 'central/support/validators/estimate_validator'
15
+ require 'central/support/validators/changed_validator'
16
+
17
+ require 'central/support/concerns/story_concern'
18
+ require 'central/support/concerns/project_concern'
19
+ require 'central/support/concerns/user_concern'
20
+ require 'central/support/concerns/team_concern'
21
+ require 'central/support/concerns/activity_concern'
22
+
23
+ # Ccompatibility mode for drop-in replacement into Central
24
+ # TODO: must remove this after the migration from Central is complete
25
+ IterationService = Central::Support::IterationService
26
+ Iteration = Central::Support::Iteration
27
+ Mattermost = Central::Support::Mattermost
@@ -0,0 +1,4 @@
1
+ require 'central/support/concerns/activity_concern/associations'
2
+ require 'central/support/concerns/activity_concern/validations'
3
+ require 'central/support/concerns/activity_concern/callbacks'
4
+ require 'central/support/concerns/activity_concern/scopes'
@@ -0,0 +1,15 @@
1
+ module Central
2
+ module Support
3
+ module ActivityConcern
4
+ module Associations
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ belongs_to :project
9
+ belongs_to :user
10
+ belongs_to :subject, polymorphic: true
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,25 @@
1
+ module Central
2
+ module Support
3
+ module ActivityConcern
4
+ module Callbacks
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ serialize :subject_changes, Hash
9
+
10
+ before_save :parse_changes
11
+ end
12
+
13
+ def parse_changes
14
+ if action == 'update'
15
+ self.subject_changes = subject.changes
16
+ elsif action == 'destroy'
17
+ self.subject_changes = subject.attributes
18
+ self.subject_destroyed_type = subject.class.name
19
+ self.subject = nil
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,55 @@
1
+ module Central
2
+ module Support
3
+ module ActivityConcern
4
+ module Scopes
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ scope :projects, ->(ids) {
9
+ where(project_id: ids) if ids
10
+ }
11
+
12
+ scope :since, ->(date) {
13
+ where("created_at > ?", date.beginning_of_day) if date
14
+ }
15
+ end
16
+
17
+ module ClassMethods
18
+ def fetch_polymorphic(ids, since)
19
+ stories = where("subject_type in ('Project', 'Story')").includes(:user, :subject).projects(ids).since(since).to_a
20
+ stories + where("subject_type in ('Note', 'Task')").includes(:user, subject: [:story]).projects(ids).since(since).to_a
21
+ end
22
+
23
+ def grouped_activities(allowed_projects, since)
24
+ fetch_polymorphic(allowed_projects.pluck(:id), since).group_by { |activity|
25
+ activity.created_at.beginning_of_day
26
+ }.
27
+ map { |date, activities|
28
+ [
29
+ date,
30
+ activities.group_by { |activity|
31
+ activity.project_id
32
+ }.
33
+ map { |project_id, activities|
34
+ [
35
+ allowed_projects.find { |p| p.id == project_id },
36
+ activities.group_by { |activity|
37
+ activity.subject_destroyed_type || activity.subject_type
38
+ }.
39
+ map { |subject_type, activities|
40
+ [
41
+ subject_type,
42
+ activities.map(&:decorate)
43
+ ]
44
+ }
45
+ ]
46
+ }
47
+ ]
48
+ }
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
55
+
@@ -0,0 +1,17 @@
1
+ module Central
2
+ module Support
3
+ module ActivityConcern
4
+ module Validations
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ validates :action, presence: true, inclusion: { in: %w(create update destroy) }
9
+ validates :project, presence: true
10
+ validates :user, presence: true
11
+ validates :subject, presence: true, changed: true
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+
@@ -0,0 +1,5 @@
1
+ require 'central/support/concerns/project_concern/attributes'
2
+ require 'central/support/concerns/project_concern/associations'
3
+ require 'central/support/concerns/project_concern/validations'
4
+ require 'central/support/concerns/project_concern/scopes'
5
+ require 'central/support/concerns/project_concern/csv'
@@ -0,0 +1,27 @@
1
+ module Central
2
+ module Support
3
+ module ProjectConcern
4
+ module Associations
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ has_many :ownerships
9
+ has_many :teams, through: :ownerships
10
+
11
+ has_many :memberships, dependent: :destroy
12
+ has_many :users, -> { distinct }, through: :memberships
13
+
14
+ has_many :stories, dependent: :destroy do
15
+ def with_dependencies
16
+ includes(:notes, :tasks, :document_files)
17
+ end
18
+
19
+ include Central::Support::ProjectConcern::CSV::Import
20
+ end
21
+
22
+ accepts_nested_attributes_for :users, reject_if: :all_blank
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,25 @@
1
+ module Central
2
+ module Support
3
+ module ProjectConcern
4
+ module Attributes
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ attr_writer :suppress_notifications
9
+ end
10
+
11
+ def suppress_notifications
12
+ @suppress_notifications || false
13
+ end
14
+
15
+ def to_s
16
+ name
17
+ end
18
+
19
+ def iteration_service(since = nil)
20
+ @iteration_service ||= Central::Support::IterationService.new(self, since)
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,59 @@
1
+ module Central
2
+ module Support
3
+ module ProjectConcern
4
+ module CSV
5
+ module InstanceMethods
6
+ def csv_filename
7
+ "#{name}-#{Time.now.strftime('%Y%m%d_%I%M')}.csv"
8
+ end
9
+ end
10
+
11
+ module Import
12
+ # Populates the stories collection from a CSV string.
13
+ def from_csv(csv_string)
14
+
15
+ # Eager load this so that we don't have to make multiple db calls when
16
+ # searching for users by full name from the CSV.
17
+ users = proxy_association.owner.users
18
+
19
+ csv = ::CSV.parse(csv_string, headers: true)
20
+ csv.map do |row|
21
+ row_attrs = row.to_hash
22
+ story = build({
23
+ title: ( row_attrs["Title"] || row_attrs["Story"] || "").truncate(255, omission: '...'),
24
+ story_type: (row_attrs["Type"] || row_attrs["Story Type"]).downcase,
25
+ requested_by: users.detect {|u| u.name == row["Requested By"]},
26
+ owned_by: users.detect {|u| u.name == row["Owned By"]},
27
+ accepted_at: row_attrs["Accepted at"],
28
+ estimate: row_attrs["Estimate"],
29
+ labels: row_attrs["Labels"],
30
+ description: row_attrs["Description"]
31
+ })
32
+
33
+ row_state = ( row_attrs["Current State"] || 'unstarted').downcase
34
+ if Story.available_states.include?(row_state.to_sym)
35
+ story.state = row_state
36
+ end
37
+ story.requested_by_name = ( row["Requested By"] || "").truncate(255)
38
+ story.owned_by_name = ( row["Owned By"] || "").truncate(255)
39
+ story.owned_by_initials = ( row["Owned By"] || "" ).split(' ').map { |n| n[0].upcase }.join('')
40
+
41
+ tasks = []
42
+ row.each do |header, value|
43
+ tasks << "* #{value}" if header == 'Task' && value
44
+ end
45
+ story.description = "#{story.description}\n\nTasks:\n\n#{tasks.join("\n")}" unless tasks.empty?
46
+ story.project.suppress_notifications = true # otherwise the import will generate massive notifications!
47
+ story.save
48
+
49
+ # Generate notes for this story if any are present
50
+ story.notes.from_csv_row(row)
51
+
52
+ story
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end