douglas 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6429b33ba850b79cbe5f57063e76476eb34365c7
4
+ data.tar.gz: c4a15d8e66cdebcd25161a0f729faa7bf41f159e
5
+ SHA512:
6
+ metadata.gz: 2f4638939b3c1b8b34920a1b3715361e5e80611ffe8ad94f6367fd74d55b17931a8727f9a11105ccec5201e51e1a7ad309ad1e94d5426d54ebb18b95e141fc7f
7
+ data.tar.gz: a7e6f13f2aedfe3474438125385a9eebf1a0de8bd42860e6a715cdeb54d269f4f2a7655affc38933f62f792dae3702511ded817730194965e99d1fd8940a717f
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.0
5
+ before_install: gem install bundler -v 1.12.5
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at ileeftimov@gmail.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in douglas.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Ilija Eftimov
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.
@@ -0,0 +1,127 @@
1
+ # Douglas
2
+
3
+ [![Active Development](https://img.shields.io/badge/Maintenance%20Level-Actively%20Developed-brightgreen.svg)](https://gist.github.com/cheerfulstoic/d107229326a01ff0f333a1d3476e068d)
4
+
5
+ **Status: Under heavy development**
6
+
7
+ Douglas is a Ruby gem that adds the ability to easily add `created_by` and
8
+ `updated_by` attributes to any ActiveRecord model.
9
+ It allows easy user-stamping for any Active Record model.
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'douglas'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+
24
+ ## Usage
25
+
26
+ ### Built-in generator
27
+
28
+ When added in a Rails application, Douglas adds a generator which enables the users
29
+ to easily generate migrations which need to be stamped.
30
+
31
+ To generate these migrations, you can use Douglas' `table` generator:
32
+
33
+ ```
34
+ rails generate douglas:table NAME
35
+ ```
36
+
37
+ This will generate a migration:
38
+
39
+ ```
40
+ create db/migrate/20170322225126_add_created_by_and_updated_by_to_<model-name>.rb
41
+ ```
42
+
43
+ The contents of this migration will be:
44
+
45
+ ```ruby
46
+ class AddCreatedByAndUpdatedByTo<model-name> < ActiveRecord::Migration[5.0]
47
+ def change
48
+ add_column :<model-name>, :created_by, :integer
49
+ add_column :<model-name>, :updated_by, :integer
50
+
51
+ add_index :<model-name>, :created_by
52
+ add_index :<model-name>, :updated_by
53
+ end
54
+ end
55
+ ```
56
+
57
+ ### Plug it into a controller
58
+
59
+ In your `ApplicationController` add a `before_action`:
60
+
61
+ ```ruby
62
+ class ApplicationController
63
+ before_action :set_douglas_the_stamper
64
+ end
65
+ ```
66
+
67
+ This will enable `Douglas` to know the current user that peroforms the action (create/update) on an model object.
68
+
69
+ ### Use it in models
70
+
71
+ In the model (that you ran the generate task for), add this:
72
+
73
+ ```ruby
74
+ class User < ApplicationRecord
75
+ has_stamps
76
+ end
77
+ ```
78
+
79
+ This will populat the `created_by` and `updated_by` attributes accordintly. Under
80
+ the hood this injects callbacks that are executed before create and update.
81
+
82
+ ### Use it in console
83
+
84
+ Since the `{created,updated}_by` columns cannot be `null` it means that the `Douglas.the_stamper` has to always be set to a value. This is normally done via the controller `before_filter` method, but in console you have to use the `Douglas.with_stamper` method. This method expects two arguments, an identifier of the user that does the action and a block. This means that the user will be logged as the person that has done the actions within the block.
85
+
86
+ For example:
87
+
88
+ ```
89
+ >> Douglas.with_stamper(123) do
90
+ ?> p = Post.first
91
+ >> p.body = 'Lorem ipsum dolor sit amet''
92
+ >> p.save
93
+ >> end
94
+ ```
95
+
96
+ The `Post` will be updated, and the `updated_by` attribute will be set to the first passed argument - `123`.
97
+
98
+ ## Progress
99
+
100
+ - [x] Add migrations generator (https://github.com/fteem/douglas/pull/1)
101
+ - [x] Find way to hijack `current_user` and store in `RequestStore`
102
+ - [x] Find way to plug-in `Douglas` in model lifecycle
103
+ - [ ] Make it work with Rails.version < 3.2
104
+ - [ ] Make logged attribute (currently `id`) to be configurable
105
+ - [ ] Add `deleted_by` attribute?
106
+
107
+ ## Development
108
+
109
+ 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.
110
+
111
+ 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).
112
+
113
+ ## Contributing
114
+
115
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/douglas. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
116
+
117
+ ## Why Douglas?
118
+
119
+ Named after [Douglas "Doug" **Stamper**](http://house-of-cards.wikia.com/wiki/Doug_Stamper),
120
+ President Frank Underwood's White House Chief of Staff and former director of strategy.
121
+
122
+ ![](http://thediscussion.net/main/wp-content/uploads/2014/08/Doug-n-Brick-S1.gif)
123
+
124
+ ## License
125
+
126
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
127
+
@@ -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
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "douglas"
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
@@ -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,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'douglas/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'douglas'
8
+ spec.version = Douglas::VERSION
9
+ spec.authors = ['Ilija Eftimov']
10
+ spec.email = ['ileeftimov@gmail.com']
11
+
12
+ spec.summary = 'Douglas adds stamps to any ActiveRecord model'
13
+ spec.description = 'Douglas adds stamps to any ActiveRecord model, like {created,updated}_by'
14
+ spec.homepage = 'https://github.com/fteem/douglas'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = 'exe'
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_development_dependency 'bundler', '~> 1.12'
23
+ spec.add_development_dependency 'rake', '~> 10.0'
24
+ spec.add_development_dependency 'rspec', '~> 3.0'
25
+
26
+ spec.add_runtime_dependency 'activerecord', '>= 4.0.0', '< 5.1'
27
+ spec.add_runtime_dependency 'request_store', '~> 1.3'
28
+ end
@@ -0,0 +1,31 @@
1
+ require 'request_store'
2
+ require 'douglas/version'
3
+ require 'douglas/model'
4
+
5
+ module Douglas
6
+ class << self
7
+ def the_stamper
8
+ RequestStore.store[:the_stamper]
9
+ end
10
+
11
+ def the_stamper= user
12
+ RequestStore.store[:the_stamper] = user
13
+ end
14
+
15
+ def with_stamper user
16
+ return unless block_given?
17
+
18
+ self.the_stamper = user
19
+ yield
20
+ self.the_stamper = nil
21
+ end
22
+ end
23
+ end
24
+
25
+ if defined?(::Rails) && ActiveRecord::VERSION::STRING >= '3.2'
26
+ require 'douglas/frameworks/rails'
27
+ end
28
+
29
+ ActiveSupport.on_load(:active_record) do
30
+ ActiveRecord::Base.send :extend, Douglas::Model
31
+ end
@@ -0,0 +1 @@
1
+ require 'douglas/frameworks/rails/controller'
@@ -0,0 +1,34 @@
1
+ module Douglas
2
+ module Rails
3
+ # Extensions to rails controllers. Provides convenient ways to pass certain
4
+ # information to the model layer via `the_stamper`.
5
+ module Controller
6
+ protected
7
+
8
+ # Returns the user who is responsible for any changes that occur.
9
+ # By default this calls `current_user` and returns the result.
10
+ #
11
+ # Override this method in your controller to call a different
12
+ # method, e.g. `current_person`, or anything you like.
13
+ def user_for_douglas
14
+ return unless defined?(current_user)
15
+ ActiveSupport::VERSION::MAJOR >= 4 ? current_user.try!(:id) : current_user.try(:id)
16
+ rescue NoMethodError
17
+ current_user
18
+ end
19
+
20
+ private
21
+
22
+ # Tells Douglas who is responsible for any changes that occur
23
+ def set_douglas_the_stamper
24
+ ::Douglas.the_stamper = user_for_douglas
25
+ end
26
+ end
27
+ end
28
+ end
29
+
30
+ if defined?(::ActionController)
31
+ ::ActiveSupport.on_load(:action_controller) do
32
+ include ::Douglas::Rails::Controller
33
+ end
34
+ end
@@ -0,0 +1,9 @@
1
+ require 'douglas/model_config'
2
+
3
+ module Douglas
4
+ module Model
5
+ def has_stamps
6
+ Douglas::ModelConfig.new(self).setup
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,18 @@
1
+ module Douglas
2
+ class ModelConfig
3
+ def initialize(model_class)
4
+ @model_class = model_class
5
+ end
6
+
7
+ # Set up `@model_class` for Douglas. Installs callbacks.
8
+ def setup
9
+ @model_class.before_create do |r|
10
+ r.created_by = Douglas.the_stamper
11
+ end
12
+
13
+ @model_class.before_save(on: :update) do |r|
14
+ r.updated_by = Douglas.the_stamper
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,3 @@
1
+ module Douglas
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,31 @@
1
+ require 'rails/generators/active_record'
2
+
3
+ module Douglas
4
+ module Generators
5
+ class TableGenerator < ::Rails::Generators::NamedBase
6
+ include ::Rails::Generators::Migration
7
+
8
+ source_root File.expand_path('../templates', __FILE__)
9
+ desc 'Create migration for for a table that should be stamped'
10
+
11
+ def self.next_migration_number(dirname)
12
+ next_migration_number = current_migration_number(dirname) + 1
13
+ ActiveRecord::Migration.next_migration_number(next_migration_number)
14
+ end
15
+
16
+ def copy_douglas_migration
17
+ migration_template 'migration.rb', "db/migrate/add_created_by_and_updated_by_to_#{table_name}.rb", migration_version: migration_version
18
+ end
19
+
20
+ def migration_version
21
+ if rails5?
22
+ "[#{::Rails::VERSION::MAJOR}.#{::Rails::VERSION::MINOR}]"
23
+ end
24
+ end
25
+
26
+ def rails5?
27
+ ::Rails.version.start_with? '5'
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,9 @@
1
+ class AddCreatedByAndUpdatedByTo<%= table_name.camelize %> < ActiveRecord::Migration<%= migration_version %>
2
+ def change
3
+ add_column :<%= table_name %>, :created_by, :integer, null: false
4
+ add_column :<%= table_name %>, :updated_by, :integer, null: false
5
+
6
+ add_index :<%= table_name %>, :created_by
7
+ add_index :<%= table_name %>, :updated_by
8
+ end
9
+ end
metadata ADDED
@@ -0,0 +1,140 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: douglas
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ilija Eftimov
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-04-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.12'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.12'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activerecord
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: 4.0.0
62
+ - - "<"
63
+ - !ruby/object:Gem::Version
64
+ version: '5.1'
65
+ type: :runtime
66
+ prerelease: false
67
+ version_requirements: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: 4.0.0
72
+ - - "<"
73
+ - !ruby/object:Gem::Version
74
+ version: '5.1'
75
+ - !ruby/object:Gem::Dependency
76
+ name: request_store
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '1.3'
82
+ type: :runtime
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '1.3'
89
+ description: Douglas adds stamps to any ActiveRecord model, like {created,updated}_by
90
+ email:
91
+ - ileeftimov@gmail.com
92
+ executables: []
93
+ extensions: []
94
+ extra_rdoc_files: []
95
+ files:
96
+ - ".gitignore"
97
+ - ".rspec"
98
+ - ".travis.yml"
99
+ - CODE_OF_CONDUCT.md
100
+ - Gemfile
101
+ - LICENSE.txt
102
+ - README.md
103
+ - Rakefile
104
+ - bin/console
105
+ - bin/setup
106
+ - douglas.gemspec
107
+ - lib/douglas.rb
108
+ - lib/douglas/frameworks/rails.rb
109
+ - lib/douglas/frameworks/rails/controller.rb
110
+ - lib/douglas/model.rb
111
+ - lib/douglas/model_config.rb
112
+ - lib/douglas/version.rb
113
+ - lib/generators/douglas/table_generator.rb
114
+ - lib/generators/douglas/templates/migration.rb
115
+ homepage: https://github.com/fteem/douglas
116
+ licenses:
117
+ - MIT
118
+ metadata: {}
119
+ post_install_message:
120
+ rdoc_options: []
121
+ require_paths:
122
+ - lib
123
+ required_ruby_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ required_rubygems_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ requirements: []
134
+ rubyforge_project:
135
+ rubygems_version: 2.5.1
136
+ signing_key:
137
+ specification_version: 4
138
+ summary: Douglas adds stamps to any ActiveRecord model
139
+ test_files: []
140
+ has_rdoc: