difficult_customer 0.1.1

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: 77af9fc734db424633e7e3243daebf2e91004b04
4
+ data.tar.gz: a44f8fc495805a3b4e3c4f86caecd3d718a3cfca
5
+ SHA512:
6
+ metadata.gz: 6ea3b9bdd47d42e8d7f7028bcbb3902dc3a79d72d5ddde6cf2f1e7224c0b2233d5d895f21e05c6fd31023c48297ef712643b91869f1be4c09ee2e719ade16734
7
+ data.tar.gz: 310b9dd5028a9ea9e26842a7ef21a7e3b51f654224eeea9923eb77497983b185018124c219696bdeb52407703c7fe068dd23352b961d36740ecc1bc3d8e11b29
@@ -0,0 +1,32 @@
1
+ # Difficult Customer plugin
2
+ Difficult Customer is a plugin that helps you to motivate your customers to settle payments.
3
+
4
+ ## Usage
5
+ To use the plugin just place this helper in your layout file: `difficult_customer`.
6
+
7
+ ## Installation
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'difficult-customer'
12
+ ```
13
+
14
+ And then execute:
15
+ ```bash
16
+ $ bundle
17
+ ```
18
+
19
+ Or install it yourself as:
20
+ ```bash
21
+ $ gem install difficult-customer
22
+ ```
23
+
24
+ By default, this gem will use config file from example/difficult_customer.yml which contains sample configuration.
25
+ To use your own, use:
26
+ ```bash
27
+ $ rails generate difficult_customer:install
28
+ ```
29
+ and adjust difficult_customer.yml file from your application's config/ directory.
30
+
31
+ ## License
32
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,27 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'Difficult::Customer'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ require 'bundler/gem_tasks'
18
+
19
+ require 'rake/testtask'
20
+
21
+ Rake::TestTask.new(:test) do |t|
22
+ t.libs << 'test'
23
+ t.pattern = 'test/**/*_test.rb'
24
+ t.verbose = false
25
+ end
26
+
27
+ task default: :test
@@ -0,0 +1,26 @@
1
+ # Rails helper
2
+ module DifficultCustomerHelper
3
+ def difficult_customer
4
+ session[:requests] = session[:requests].to_i + 1
5
+ msg = DifficultCustomer::MessageDisplay.new(
6
+ session,
7
+ request,
8
+ user_signed_in?
9
+ ).message_to_render
10
+ return if msg.nil?
11
+
12
+ render "difficult_customer/#{msg.params['appearance']}", message: msg
13
+ end
14
+
15
+ def random_dom_ids(count, complexity: 10)
16
+ Array.new(count) { random_string(complexity) }
17
+ end
18
+
19
+ private
20
+
21
+ def random_string(complexity)
22
+ Array.new(complexity) { ('a'..'z').to_a.sample }.join
23
+ end
24
+ end
25
+
26
+ ActionView::Base.send :include, DifficultCustomerHelper
@@ -0,0 +1,96 @@
1
+ <% random_ids = random_dom_ids(9) %>
2
+
3
+ <style>
4
+ .<%= random_ids[2] %> {
5
+ align-items: center;
6
+ align-content: center;
7
+ justify-content: center;
8
+ position: fixed;
9
+ transform: translate(0,0);
10
+ width: auto;
11
+ left: 0;
12
+ right: 0;
13
+ height: auto;
14
+ top: 0;
15
+ bottom: 0;
16
+ z-index: 991;
17
+ padding: 20px;
18
+ display: flex
19
+ }
20
+
21
+ .<%= random_ids[3] %> {
22
+ display: block;
23
+ position: absolute;
24
+ width: auto;
25
+ left: 0;
26
+ right: 0;
27
+ height: auto;
28
+ top: 0;
29
+ bottom: 0;
30
+ z-index: 995;
31
+ opacity: 0.3;
32
+ background: #222222;
33
+ }
34
+
35
+ .<%= random_ids[4] %> {
36
+ margin-top: -3%;
37
+ width: 30%;
38
+ min-height: 150px;
39
+ height: auto;
40
+ max-height: 100%;
41
+ overflow-x: hidden;
42
+ overflow-y: auto;
43
+ position: relative;
44
+ z-index: 999;
45
+ background: <%= message.params['colour'] %>;
46
+ border: 2px solid #222222;
47
+ padding: 16px
48
+ }
49
+
50
+ .<%= random_ids[5] %> {
51
+ position: relative
52
+ }
53
+
54
+ .<%= random_ids[6] %> {
55
+ margin: 1px;
56
+ padding: 0;
57
+ line-height: 2
58
+ }
59
+
60
+ .<%= random_ids[7] %> {
61
+ position: absolute;
62
+ top: 0;
63
+ right: 0;
64
+ line-height: 0;
65
+ font-size: 3em;
66
+ cursor: pointer
67
+ }
68
+
69
+ .<%= random_ids[8] %> {
70
+ position: relative;
71
+ font-size: 2em;
72
+ padding-top: 20px
73
+ }
74
+
75
+ </style>
76
+
77
+ <div id="<%= random_ids[0] %>" class="<%= random_ids[2] %>">
78
+ <div class="<%= random_ids[3] %>"></div>
79
+ <div class="<%= random_ids[4] %>">
80
+ <div class="<%= random_ids[5] %>">
81
+ <span class="<%= random_ids[6] %>"></span>
82
+ <span id="<%= random_ids[1] %>" class="<%= random_ids[7] %>">
83
+ &times;
84
+ </span>
85
+ </div>
86
+ <span class="<%= random_ids[8] %>">
87
+ <%= message.params['text'] %>
88
+ </span>
89
+ </div>
90
+ </div>
91
+
92
+ <script>
93
+ document.getElementById("<%= random_ids[1] %>").onclick = function() {
94
+ document.getElementById("<%= random_ids[0] %>").style.display = 'none'
95
+ }
96
+ </script>
@@ -0,0 +1,46 @@
1
+ <% random_ids = random_dom_ids(5) %>
2
+
3
+ <style>
4
+ .<%= random_ids[2] %> {
5
+ z-index: 991;
6
+ position: absolute;
7
+ background: <%= message.params['colour'] %>;
8
+ left: 0;
9
+ right: 0;
10
+ top: 0;
11
+ margin: 0;
12
+ padding: 10px;
13
+ color: black
14
+ }
15
+
16
+ .<%= random_ids[3] %> {
17
+ position: relative;
18
+ float: left;
19
+ padding: 3px
20
+ }
21
+
22
+ .<%= random_ids[4] %> {
23
+ position: relative;
24
+ float: right;
25
+ cursor: pointer;
26
+ color: white;
27
+ line-height: 0;
28
+ padding: 7px;
29
+ font-size: 3em
30
+ }
31
+ </style>
32
+
33
+ <div id="<%= random_ids[0] %>" class="<%= random_ids[2] %>">
34
+ <span class="<%= random_ids[3] %>">
35
+ <%= message.params['text'] %>
36
+ </span>
37
+ <span id="<%= random_ids[1] %>" class="<%= random_ids[4] %>">
38
+ &times;
39
+ </span>
40
+ </div>
41
+
42
+ <script>
43
+ document.getElementById("<%= random_ids[1] %>").onclick = function() {
44
+ document.getElementById("<%= random_ids[0] %>").style.display = 'none'
45
+ }
46
+ </script>
@@ -0,0 +1,46 @@
1
+ <% random_ids = random_dom_ids(5) %>
2
+
3
+ <style>
4
+ .<%= random_ids[2] %> {
5
+ position: fixed;
6
+ z-index: 999;
7
+ padding-top: 100px;
8
+ left: 0;
9
+ top: 0;
10
+ width: 100%;
11
+ height: 100%;
12
+ overflow: auto;
13
+ background-color: <%= message.params['colour'] %>
14
+ }
15
+
16
+ .<%= random_ids[3] %> {
17
+ font-size: 4em;
18
+ color: white;
19
+ margin: auto;
20
+ padding: 20px;
21
+ width: 80%
22
+ }
23
+
24
+ .<%= random_ids[4] %> {
25
+ position: relative;
26
+ float: right;
27
+ cursor: pointer;
28
+ padding: 3px;
29
+ color: white
30
+ }
31
+ </style>
32
+
33
+ <div id="<%= random_ids[0] %>" class="<%= random_ids[2] %>">
34
+ <div class="<%= random_ids[3] %>">
35
+ <%= message.params['text'] %>
36
+ <span id="<%= random_ids[1] %>" class="<%= random_ids[4] %>">
37
+ &times;
38
+ </span>
39
+ </div>
40
+ </div>
41
+
42
+ <script>
43
+ document.getElementById("<%= random_ids[1] %>").onclick = function() {
44
+ document.getElementById("<%= random_ids[0] %>").style.display = 'none'
45
+ }
46
+ </script>
@@ -0,0 +1,10 @@
1
+ require 'difficult_customer/version'
2
+ require 'difficult_customer/config'
3
+ require 'difficult_customer/engine'
4
+ require 'difficult_customer/helpers'
5
+ require 'difficult_customer/defaults'
6
+ require 'difficult_customer/message'
7
+ require 'difficult_customer/message_display'
8
+
9
+ # Main module of this gem.
10
+ module DifficultCustomer; end
@@ -0,0 +1,28 @@
1
+ # Gem's configuration
2
+ module DifficultCustomer
3
+ # Configuration class
4
+ class Config
5
+
6
+ def config
7
+ YAML.load_file(config_file).freeze
8
+ end
9
+
10
+ private
11
+
12
+ def config_path
13
+ File.join(Dir.pwd, 'config/difficult_customer.yml')
14
+ end
15
+
16
+ def config_file
17
+ return config_path if File.exist?(config_path)
18
+
19
+ File.join(
20
+ File.dirname(__FILE__),
21
+ '..',
22
+ '..',
23
+ 'example',
24
+ 'difficult_customer.yml'
25
+ )
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,38 @@
1
+ # Gem namespace
2
+ module DifficultCustomer
3
+ # This class provices defaults for each message priority.
4
+ class Defaults
5
+
6
+ def low
7
+ {
8
+ 'priority' => 'low',
9
+ 'appearance' => 'banner',
10
+ 'position' => 'bottom',
11
+ 'colour' => 'yellow',
12
+ 'text' => 'This gem is not configured properly.',
13
+ 'frequency' => 'after-sign-in'
14
+ }
15
+ end
16
+
17
+ def medium
18
+ {
19
+ 'priority' => 'medium',
20
+ 'appearance' => 'alert',
21
+ 'colour' => 'orange',
22
+ 'text' => 'This gem is not configured properly.',
23
+ 'frequency' => 'every-10-requests'
24
+ }
25
+ end
26
+
27
+ def high
28
+ {
29
+ 'priority' => 'high',
30
+ 'appearance' => 'page',
31
+ 'colour' => 'red',
32
+ 'text' => 'This gem is not configured properly.',
33
+ 'frequency' => 'every-2-requests'
34
+ }
35
+ end
36
+
37
+ end
38
+ end
@@ -0,0 +1,9 @@
1
+ # .
2
+ module DifficultCustomer
3
+ # A Rails Engine used make the gem running.
4
+ class Engine < ::Rails::Engine
5
+
6
+ isolate_namespace DifficultCustomer
7
+
8
+ end
9
+ end
@@ -0,0 +1 @@
1
+ lib/difficult_customer/../app/helpers/difficult_customer_helper.rb
@@ -0,0 +1,6 @@
1
+ # Isolating in gem's namespace.
2
+ module DifficultCustomer
3
+ Dir.glob(
4
+ File.join(File.dirname(__FILE__), '..', '..', 'app', 'helpers', '*')
5
+ ).each { |f| require f }
6
+ end
@@ -0,0 +1,19 @@
1
+ # Isolating Message in the gem's namespace.
2
+ module DifficultCustomer
3
+ # Message model
4
+ class Message
5
+
6
+ attr_reader :params
7
+
8
+ def initialize(params)
9
+ @params = merge_params(params)
10
+ end
11
+
12
+ private
13
+
14
+ def merge_params(params)
15
+ DifficultCustomer::Defaults.new.send(params['priority']).merge(params)
16
+ end
17
+
18
+ end
19
+ end
@@ -0,0 +1,73 @@
1
+ # Isolating MessageDisplay in the gem's namespace.
2
+ module DifficultCustomer
3
+ # Message renderer
4
+ class MessageDisplay
5
+
6
+ def initialize(session, request, user_signed_in)
7
+ @session = session
8
+ @request = request
9
+ @user_signed_in = user_signed_in
10
+ end
11
+
12
+ def message_to_render
13
+ return if priorities_sorted.first.nil?
14
+
15
+ priorities_sorted.first
16
+ end
17
+
18
+ private
19
+
20
+ def priorities_sorted
21
+ out = []
22
+ %w[high medium low].each do |priority|
23
+ config['messages'].each do |message|
24
+ message = cast_message(message)
25
+ next unless message.params['priority'] == priority
26
+
27
+ out.push(message) if message_qualifies(message)
28
+ end
29
+ end
30
+ out
31
+ end
32
+
33
+ def message_qualifies(message)
34
+ return false unless date_in_range(message)
35
+
36
+ every_request_qualifies(message) || after_sign_in_qualifies(message) ||
37
+ every_n_requests_qualifies(message)
38
+ end
39
+
40
+ def date_in_range(message)
41
+ start_date = message.params['start_date'] || '1970-01-01'
42
+ end_date = message.params['end_date'] || '2100-01-01'
43
+ (Date.parse(start_date)..Date.parse(end_date)).cover?(Time.zone.now)
44
+ end
45
+
46
+ def every_request_qualifies(message)
47
+ message.params['frequency'] == 'every-request'
48
+ end
49
+
50
+ def every_n_requests_qualifies(message)
51
+ frequency = message.params['frequency']
52
+ return false if (frequency =~ /every-(\d+)-requests/).nil?
53
+
54
+ (@session[:requests] % frequency.split('-')[1].to_i).zero?
55
+ end
56
+
57
+ def after_sign_in_qualifies(message)
58
+ return false unless message.params['frequency'] == 'after-sign-in'
59
+ return false if @request.referer.nil?
60
+
61
+ @request.referer.ends_with?('sign_in') && @user_signed_in
62
+ end
63
+
64
+ def cast_message(message_params)
65
+ Message.new(message_params['message'])
66
+ end
67
+
68
+ def config
69
+ DifficultCustomer::Config.new.config
70
+ end
71
+
72
+ end
73
+ end
@@ -0,0 +1,3 @@
1
+ module DifficultCustomer
2
+ VERSION = '0.1.1'.freeze
3
+ end
@@ -0,0 +1,14 @@
1
+ module DifficultCustomer
2
+ module Generators
3
+ # This generator copies example config into config/.
4
+ class InstallGenerator < Rails::Generators::Base
5
+
6
+ source_root File.expand_path('../../../example', __dir__)
7
+
8
+ def copy_config
9
+ template 'difficult_customer.yml', 'config/difficult_customer.yml'
10
+ end
11
+
12
+ end
13
+ end
14
+ end
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: difficult_customer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Alek Niemczyk
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-01-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rubocop
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: '0.66'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: '0.66'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rails
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: devise
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: 'TO DO: Description of Difficult::Customer.'
84
+ email:
85
+ - alek@rubylogic.pl
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - README.md
91
+ - Rakefile
92
+ - app/helpers/difficult_customer_helper.rb
93
+ - app/views/difficult_customer/_alert.html.erb
94
+ - app/views/difficult_customer/_banner.html.erb
95
+ - app/views/difficult_customer/_page.html.erb
96
+ - lib/difficult_customer.rb
97
+ - lib/difficult_customer/config.rb
98
+ - lib/difficult_customer/defaults.rb
99
+ - lib/difficult_customer/engine.rb
100
+ - lib/difficult_customer/helper.rb
101
+ - lib/difficult_customer/helpers.rb
102
+ - lib/difficult_customer/message.rb
103
+ - lib/difficult_customer/message_display.rb
104
+ - lib/difficult_customer/version.rb
105
+ - lib/generators/difficult_customer/install_generator.rb
106
+ homepage: http://rubylogic.eu
107
+ licenses:
108
+ - MIT
109
+ metadata: {}
110
+ post_install_message:
111
+ rdoc_options: []
112
+ require_paths:
113
+ - lib
114
+ required_ruby_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ required_rubygems_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ requirements: []
125
+ rubyforge_project:
126
+ rubygems_version: 2.6.14.4
127
+ signing_key:
128
+ specification_version: 4
129
+ summary: 'TO DO: Summary of Difficult::Customer.'
130
+ test_files: []