infopark-crm-helpers 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d849f354829856eaffcdbd4defeda32f94c57df6
4
+ data.tar.gz: 59fd4f8b9f3931b444d614a606076b1cbd11e56a
5
+ SHA512:
6
+ metadata.gz: a170ccc134798e6e81cd17bef8f01d89f5758251df5c6a22102433b6b34d88801af4d5e8251143d553478d9c6cc969aa36b5d449f8ecde7221c718c24638d82a
7
+ data.tar.gz: 20482c0d3363acf8a1edbfbb8d000dbfe42fc25424755b643eded326c445ab53fe937f682bb2b23973693cb3234ab0f6461891441f5fb021aa0db97f8a5afe9d
data/.codeclimate.yml ADDED
@@ -0,0 +1,14 @@
1
+ ratings:
2
+ paths:
3
+ - 'lib/**/*'
4
+ - 'spec/**/*'
5
+
6
+ engines:
7
+ duplication:
8
+ enabled: true
9
+ exclude_fingerprints:
10
+ - '1266db484c8bb3c808ac9d572ba54770'
11
+ - '37c399f0597fc2bc85085f68529d3426'
12
+ config:
13
+ languages:
14
+ ruby:
data/.crm.example.yml ADDED
@@ -0,0 +1,3 @@
1
+ tenant: your-tenant-here
2
+ login: john.smith@example.org
3
+ api_key: 1234567890abcdefghijklmnopqrstuvwxyz
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.crm.yml
3
+ /.yardoc
4
+ /Gemfile.lock
5
+ /_yardoc/
6
+ /coverage/
7
+ /doc/
8
+ /pkg/
9
+ /spec/reports/
10
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format progress
2
+ --colour
3
+ --profile
data/.travis.yml ADDED
@@ -0,0 +1,21 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3.1
4
+ - 2.2.5
5
+ - 2.1.10
6
+
7
+ before_install: gem install bundler -v 1.11.2
8
+ install: bundle install --without doc --retry=3
9
+ script:
10
+ - bundle exec rake spec
11
+
12
+ env:
13
+ global:
14
+ - 'WEBCRM_INTEGRATION=true'
15
+ - 'CODECLIMATE_REPO_TOKEN=81caf6b3c3c3587d37103c70e5213cab4c2fee6b08918557736baa075021b6d0'
16
+ - secure: "dRGHULKMHj8/9Y7L1JMc+RwjD6c+djCBvQsewVGII+a5loiRQFV72zb5Td0JBPzGI2K1t3EXc2F8X8D+1QR+jpb0SFFSAUHzb2mvWU+FPixHfTxAPpktfiT+qAHFPkeaQMY86ktosE50+J555ke8YbtRKIBsqDbvZU+vnr3N6Ui4JhomCPMyCmnuFFATHoGBdhcrvWc1vY1Z7gK/qd0nhgyDb+sfOnbLinz7qR4oEI2nbGA6z5AzwfbD4JkXqagPirQy2tUJGAY71QVpDevg3qjfV2Hb9Ow1ESW9M0DsZZjFXXx6VJ4z9xikaqdauUMcJSCNMXtSK6OIUiy7Ln+WQKjDuGLxOLpG5oUxV7FrKEp63cQX9+6BDEPRuilwumZMBP0goE0kMFZmjai1bm3sMCddm08i05w9NraswxRHElBlPQxAXCDtiEQR2PKkI0G9j1/OheTvxn73GK0owPA6aOpOn52Jp+nwt8llZzPCLMvax4CmECESRvIJ7MN+ye1yre4IopXnbdZhHo/eoeGO0WTZL/t3qrmjGPyTljTkwGgp3LI+NxIoZG6UjOo2WJb6A3D8eZRbmMphZns1j7zcSUrCRNmpS6UmPsG1jExz+65LMSbScMs5xvCEtyW1X7qznf1NKLsfJxQUF3LlLSUrp/LoSTaDsrcR0E9qU9xt7/c="
17
+
18
+ matrix:
19
+ - 'RAILS_VERSION=4.2.0'
20
+ - 'RAILS_VERSION=4.1.0'
21
+ - 'RAILS_VERSION=4.0.0'
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in infopark-crm-helpers.gemspec
4
+ gemspec
5
+
6
+ gem 'codeclimate-test-reporter', group: :test, require: nil
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Huy Dinh
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,189 @@
1
+ [![infopark-crm-helpers build status on Travis CI](https://travis-ci.org/Skudo/infopark-crm-helpers.svg?branch=develop)](https://travis-ci.org/Skudo/infopark-crm-helpers)
2
+ [![infopark-crm-helpers on CodeClimate](https://codeclimate.com/github/Skudo/infopark-crm-helpers/badges/gpa.svg)](https://codeclimate.com/github/Skudo/infopark-crm-helpers)
3
+ [![infopark-crm-helpers test coverage on CodeClimate](https://codeclimate.com/github/Skudo/infopark-crm-helpers/badges/coverage.svg)](https://codeclimate.com/github/Skudo/infopark-crm-helpers/coverage)
4
+
5
+ # infopark-crm-helpers
6
+
7
+ [Infopark WebCRM](https://infopark.com) is a cloud-based CRM. This gem `infopark-crm-helpers` addresses two "annoying" things when you have your own classes and want to use the WebCRM as a data store.
8
+
9
+ * `Crm::Contact` and its friends don't provide setters for attributes and if you build your own classes around them, you will find that collecting all the relevant attributes with which to call `#update` (or `#create`) can be a bit tedious.
10
+ * There is no easy way to validate your objects against the type definitions set up in the WebCRM. You however want to validate your entire object before you try to write your data to the WebCRM, because not doing so can cause an awkward user experience: You validate the object to match _your_ expectations of what a valid object is and the user works on fixing their input to match _your_ expectations. Then, you send your data to the WebCRM and... it tells you your data is invalid. The user will have to deal with another round of fixing their input, which can end up in nasty tableflips.
11
+
12
+ `infopark-crm-helpers` attempts to address these things in a convenient way.
13
+
14
+ ## Installation
15
+
16
+ Add 'infopark-crm-helpers' into your `Gemfile`.
17
+
18
+ ```
19
+ gem 'infopark-crm-helpers`
20
+ ```
21
+
22
+ Install the gem with [Bundler](https://bundler.io/).
23
+
24
+ ```
25
+ bundle install
26
+ ```
27
+
28
+ ## Configuration
29
+
30
+ No direct configuration of `infopark-crm-helpers` is required. Refer to [infopark/webcrm_sdk](https://github.com/infopark/webcrm_sdk#configuration) for instructions on setting up your installation of `infopark_webcrm_sdk`.
31
+
32
+ ## Examples
33
+
34
+ ### Create your own class
35
+
36
+ Let's build a class `Customer` which is based on WebCRM contacts.
37
+
38
+ ```ruby
39
+ class Customer
40
+ include Crm::Helpers::Attributes
41
+
42
+ represents_crm_type :contact
43
+ end
44
+
45
+ Customer.mandatory_crm_attributes
46
+ # => [:language, :last_name]
47
+
48
+ Customer.crm_attributes[:language]
49
+ # => {"attribute_type"=>"enum", "create"=>true, "mandatory"=>true, "read"=>true,
50
+ # "title"=>"Language", "update"=>true, "valid_values"=>["de", "en"]}
51
+
52
+ Customer.crm_attributes[:last_name]
53
+ # => {"attribute_type"=>"string", "create"=>true, "mandatory"=>true,
54
+ # "read"=>true, "title"=>"Last Name", "update"=>true}
55
+ ```
56
+
57
+ ### Use attributes from the CRM contact
58
+
59
+ ```ruby
60
+ class Customer
61
+ include Crm::Helpers::Attributes
62
+
63
+ represents_crm_type :contact
64
+
65
+ crm_attr_accessor :first_name, :last_name, :email
66
+ end
67
+
68
+ customer = Customer.new
69
+ # => #<Customer>
70
+
71
+ customer.first_name = 'Huy'
72
+ # => "Huy"
73
+
74
+ customer.last_name = 'Dinh'
75
+ # => "Dinh"
76
+
77
+ customer.email = 'huy.dinh@infopark.de'
78
+ # => "huy.dinh@infopark.de"
79
+
80
+ customer.crm_attributes
81
+ # => {:first_name=>"Huy", :last_name=>"Dinh", :email=>"huy.dinh@infopark.de"}
82
+ ```
83
+
84
+ ### Validate an object
85
+
86
+ ```ruby
87
+ class Customer
88
+ include ActiveModel::Validations
89
+ include Crm::Helpers::Attributes
90
+
91
+ represents_crm_type :contact
92
+
93
+ crm_attr_accessor :first_name, :last_name, :email
94
+ validates_with Crm::Helpers::Validators::CrmTypeValidator
95
+ end
96
+
97
+ customer = Customer.new
98
+ # => #<Customer>
99
+
100
+ customer.valid?
101
+ # => false
102
+
103
+ customer.errors
104
+ # => #<ActiveModel::Errors @base=#<Customer:0x007feb8f3b3d00
105
+ # @validation_context=nil, @errors=#<ActiveModel::Errors:0x007feb8f381dc8 ...>,
106
+ # @crm_attributes={}>, @messages={:language=>["can't be blank"],
107
+ # :last_name=>["can't be blank"]}>```
108
+ ```
109
+
110
+ ### Fetch and update an object
111
+
112
+ ```ruby
113
+ class Customer
114
+ include ActiveModel::Validations
115
+ include Crm::Helpers::Attributes
116
+
117
+ represents_crm_type :contact
118
+
119
+ crm_attr_accessor :first_name, :last_name, :email
120
+ validates_with Crm::Helpers::Validators::CrmTypeValidator
121
+
122
+ def self.find(crm_id)
123
+ crm_contact = Crm::Contact.find(crm_id)
124
+ return nil if crm_contact.nil?
125
+
126
+ new(crm_contact.attributes)
127
+ end
128
+
129
+ def initialize(attributes = {})
130
+ @crm_attributes = attributes.dup
131
+ end
132
+
133
+ def save
134
+ return false if invalid?
135
+
136
+ crm_contact = Crm::Contact.find(crm_id)
137
+ crm_contact.update(crm_attributes)
138
+ @crm_attributes = crm_contact.attributes
139
+ true
140
+ end
141
+ end
142
+
143
+ customer = Customer.find('fc851ba935f8420824498aee739ac897')
144
+ # => #<Customer>
145
+
146
+ customer.last_name
147
+ # => "Dinh"
148
+
149
+ customer.first_name
150
+ # => "Huy"
151
+
152
+ customer.last_name = 'Smith'
153
+ # => "Smith"
154
+
155
+ customer.first_name = 'John'
156
+ # => "John"
157
+
158
+ customer.save
159
+ # true
160
+
161
+ same_customer = Customer.find('fc851ba935f8420824498aee739ac897')
162
+ # => #<Customer>
163
+
164
+ same_customer.last_name
165
+ # => "Smith"
166
+
167
+ same_customer.first_name
168
+ # => "John"
169
+ ```
170
+
171
+ ## Validators
172
+
173
+ Each CRM attribute type has its own validator:
174
+
175
+ * `Crm::Helpers::Validators::CrmBooleanValidator`
176
+ * `Crm::Helpers::Validators::CrmDatetimeValidator`
177
+ * `Crm::Helpers::Validators::CrmEnumValidator`
178
+ * `Crm::Helpers::Validators::CrmIntegerValidator`
179
+ * `Crm::Helpers::Validators::CrmListValidator`
180
+ * `Crm::Helpers::Validators::CrmMultienumValidator`
181
+ * `Crm::Helpers::Validators::CrmStringValidator`
182
+ * `Crm::Helpers::Validators::CrmTextValidator`
183
+
184
+ You can use those to explicitly validate attributes for a certain format.
185
+
186
+ ```ruby
187
+ validates_with Crm::Helpers::Validators::CrmBooleanValidator,
188
+ attributes: [:custom_has_ps4, :custom_has_xbox_one]
189
+ ```
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 'crm/helpers'
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
data/changelog.md ADDED
@@ -0,0 +1,3 @@
1
+ ### 1.0.0
2
+
3
+ - Initial release.
@@ -0,0 +1,38 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'crm/helpers/version'
5
+
6
+ rails_version = ENV['RAILS_VERSION'] || 'default'
7
+ rails = case rails_version
8
+ when 'default' then '~> 4.0'
9
+ else "~> #{rails_version}"
10
+ end
11
+
12
+ Gem::Specification.new do |spec|
13
+ spec.name = 'infopark-crm-helpers'
14
+ spec.version = Crm::Helpers::VERSION
15
+ spec.authors = ['Huy Dinh']
16
+ spec.email = ['mail@huydinh.eu']
17
+
18
+ spec.summary = 'Helpers for the Infopark WebCRM SDK for Ruby.'
19
+ spec.description = 'infopark-crm-helpers provides helper mixins and validators to use with the Infopark WebCRM.'
20
+ spec.description += ' It is based on the Infopark WebCRM SDK for Ruby.'
21
+ spec.homepage = 'https://github.com/Skudo/infopark-crm-helpers'
22
+ spec.license = 'MIT'
23
+
24
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ spec.bindir = 'exe'
26
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ['lib']
28
+
29
+ spec.add_dependency 'activesupport', rails
30
+ spec.add_dependency 'activemodel', rails
31
+ spec.add_dependency 'infopark_webcrm_sdk'
32
+
33
+ spec.add_development_dependency 'bundler', '~> 1.11'
34
+ spec.add_development_dependency 'fakeweb'
35
+ spec.add_development_dependency 'pry'
36
+ spec.add_development_dependency 'rake', '~> 10.0'
37
+ spec.add_development_dependency 'rspec', '~> 3.0'
38
+ end
data/lib/crm/helper.rb ADDED
@@ -0,0 +1,5 @@
1
+ module Crm
2
+ module Helper
3
+ include Crm::Helpers::Attributes
4
+ end
5
+ end
@@ -0,0 +1,88 @@
1
+ module Crm
2
+ module Helpers
3
+ module Attributes
4
+ def self.included(base)
5
+ base.extend ClassMethods
6
+ end
7
+
8
+ module ClassMethods
9
+ attr_reader :crm_type
10
+
11
+ def represents_crm_type(type)
12
+ @crm_type = type
13
+ @crm_attributes = {}.with_indifferent_access
14
+ crm_attr_accessor(*mandatory_crm_attributes)
15
+ end
16
+
17
+ def mandatory_crm_attributes
18
+ crm_attributes.select { |_, definition| definition[:mandatory] }.keys.sort.map(&:to_sym)
19
+ end
20
+
21
+ def crm_attributes
22
+ return @crm_attributes if @crm_attributes.present?
23
+ raise "#{name}.represents_crm_type(type) needs to be called to fetch its CRM attributes." if crm_type.blank?
24
+
25
+ collect_crm_attributes_data(crm_type)
26
+ end
27
+
28
+ def crm_attr_reader(*attributes)
29
+ @crm_attr_readers ||= []
30
+
31
+ attributes.each do |attribute|
32
+ @crm_attr_readers << attribute unless attribute.in?(@crm_attr_readers)
33
+ next if methods.include?(attribute.to_sym)
34
+ raise "Attribute '#{attribute}' does not exist for a CRM #{crm_type}." if crm_attributes[attribute].blank?
35
+
36
+ define_method attribute do
37
+ crm_attributes[attribute]
38
+ end
39
+ end
40
+ end
41
+
42
+ def crm_attr_readers
43
+ @crm_attr_readers.sort ||= []
44
+ end
45
+
46
+ def crm_attr_writer(*attributes)
47
+ @crm_attr_writers ||= []
48
+
49
+ attributes.each do |attribute|
50
+ method_name = "#{attribute}=".to_sym
51
+ @crm_attr_writers << method_name unless method_name.in?(@crm_attr_writers)
52
+ next if methods.include?(method_name)
53
+ raise "Attribute '#{attribute}' does not exist for a CRM #{crm_type}." if crm_attributes[attribute].blank?
54
+
55
+ define_method method_name do |value|
56
+ crm_attributes[attribute] = value
57
+ end
58
+ end
59
+ end
60
+
61
+ def crm_attr_writers
62
+ @crm_attr_writers.sort || []
63
+ end
64
+
65
+ def crm_attr_accessor(*attributes)
66
+ crm_attr_reader(*attributes)
67
+ crm_attr_writer(*attributes)
68
+ end
69
+
70
+ protected
71
+
72
+ def collect_crm_attributes_data(crm_type)
73
+ type = Crm::Type.find(crm_type)
74
+ @crm_attributes = type.standard_attribute_definitions
75
+ # This is a lovely hack, because the language attribute does not get
76
+ # the correct valid values in #standard_attribute_definitions. Maybe
77
+ # soon, when Thomas Ritz is back from warkation...
78
+ @crm_attributes[:language][:valid_values] += type.languages if @crm_attributes[:language].present?
79
+ @crm_attributes.merge!(type.attribute_definitions)
80
+ end
81
+ end
82
+
83
+ def crm_attributes
84
+ @crm_attributes ||= {}
85
+ end
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,11 @@
1
+ module Crm
2
+ module Helpers
3
+ module Validators
4
+ class CrmBooleanValidator < CrmEachValidator
5
+ def validate_each(record, attribute, _)
6
+ record.validates_inclusion_of attribute, in: [true, false]
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,15 @@
1
+ module Crm
2
+ module Helpers
3
+ module Validators
4
+ class CrmDatetimeValidator < CrmEachValidator
5
+ def validate_each(record, attribute, value)
6
+ return if value.is_a?(Date)
7
+ return if value.is_a?(Time)
8
+ return if value.is_a?(DateTime)
9
+
10
+ record.errors.add(attribute, I18n.t('activerecord.errors.messages.invalid_datetime'))
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,9 @@
1
+ module Crm
2
+ module Helpers
3
+ module Validators
4
+ class CrmEachValidator < ActiveModel::EachValidator
5
+ include CrmValidatorHelper
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,12 @@
1
+ module Crm
2
+ module Helpers
3
+ module Validators
4
+ class CrmEnumValidator < CrmEachValidator
5
+ def validate_each(record, attribute, _)
6
+ definition = crm_attribute_definition(record, attribute)
7
+ record.validates_inclusion_of attribute, in: definition['valid_values']
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,11 @@
1
+ module Crm
2
+ module Helpers
3
+ module Validators
4
+ class CrmIntegerValidator < CrmEachValidator
5
+ def validate_each(record, attribute, _)
6
+ record.validates_numericality_of attribute, only_integer: true
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,13 @@
1
+ module Crm
2
+ module Helpers
3
+ module Validators
4
+ class CrmListValidator < CrmEachValidator
5
+ def validate_each(record, attribute, value)
6
+ return if value.is_a?(Array)
7
+
8
+ record.errors.add(attribute, I18n.t('activerecord.errors.messages.not_a_list'))
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,31 @@
1
+ module Crm
2
+ module Helpers
3
+ module Validators
4
+ class CrmMultienumValidator < CrmEachValidator
5
+ def validate_each(record, attribute, values)
6
+ return unless a_multienum?(record, attribute, values)
7
+
8
+ definition = crm_attribute_definition(record, attribute)
9
+ valid_values = definition['valid_values']
10
+
11
+ invalid_values = values.reject { |value| value.in?(valid_values) }
12
+ return if invalid_values.blank?
13
+
14
+ message = I18n.t('activerecord.errors.messages.cannot_contain_values', values: invalid_values.join(', '))
15
+ record.errors.add(attribute, message)
16
+ end
17
+
18
+ protected
19
+
20
+ def a_multienum?(record, attribute, values)
21
+ unless values.is_a?(Array)
22
+ record.errors.add(attribute, I18n.t('activerecord.errors.messages.not_a_list'))
23
+ return false
24
+ end
25
+
26
+ true
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,14 @@
1
+ module Crm
2
+ module Helpers
3
+ module Validators
4
+ class CrmStringValidator < CrmEachValidator
5
+ def validate_each(record, attribute, _)
6
+ definition = crm_attribute_definition(record, attribute)
7
+ return if definition['max_length'].blank?
8
+
9
+ record.validates_length_of attribute, maximum: definition['max_length']
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ module Crm
2
+ module Helpers
3
+ module Validators
4
+ class CrmTextValidator < CrmEachValidator
5
+ def validate_each(record, attribute, _)
6
+ definition = crm_attribute_definition(record, attribute)
7
+ return if definition['max_length'].blank?
8
+
9
+ record.validates_length_of attribute, maximum: definition['max_length']
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,24 @@
1
+ module Crm
2
+ module Helpers
3
+ module Validators
4
+ class CrmTypeValidator < ActiveModel::Validator
5
+ include CrmValidatorHelper
6
+
7
+ def validate(record)
8
+ crm_type_definition(record).each_pair do |attribute, definition|
9
+ record.validates_presence_of attribute if definition[:mandatory]
10
+
11
+ reader = attribute.to_sym
12
+ next unless record.respond_to?(reader)
13
+ value = record.send(reader)
14
+ next if value.blank?
15
+
16
+ attribute_type = definition['attribute_type']
17
+ validator = "::Crm::Helpers::Validators::Crm#{attribute_type.camelcase}Validator".constantize
18
+ record.validates_with validator, attributes: [attribute]
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,17 @@
1
+ module Crm
2
+ module Helpers
3
+ module Validators
4
+ module CrmValidatorHelper
5
+ protected
6
+
7
+ def crm_type_definition(record)
8
+ record.class.try(:crm_attributes) || {}
9
+ end
10
+
11
+ def crm_attribute_definition(record, attribute)
12
+ crm_type_definition(record)[attribute]
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,5 @@
1
+ module Crm
2
+ module Helpers
3
+ VERSION = '1.0.0'.freeze
4
+ end
5
+ end
@@ -0,0 +1,26 @@
1
+ require 'active_model'
2
+ require 'active_support/all'
3
+ require 'infopark_webcrm_sdk'
4
+
5
+ require 'crm/helpers/validators/crm_validator_helper'
6
+ require 'crm/helpers/validators/crm_each_validator'
7
+
8
+ require 'crm/helpers/validators/crm_boolean_validator'
9
+ require 'crm/helpers/validators/crm_datetime_validator'
10
+ require 'crm/helpers/validators/crm_enum_validator'
11
+ require 'crm/helpers/validators/crm_integer_validator'
12
+ require 'crm/helpers/validators/crm_list_validator'
13
+ require 'crm/helpers/validators/crm_multienum_validator'
14
+ require 'crm/helpers/validators/crm_string_validator'
15
+ require 'crm/helpers/validators/crm_text_validator'
16
+ require 'crm/helpers/validators/crm_type_validator'
17
+
18
+ require 'crm/helpers/attributes'
19
+ require 'crm/helpers/version'
20
+
21
+ require 'crm/helper'
22
+
23
+ module Crm
24
+ module Helpers
25
+ end
26
+ end
metadata ADDED
@@ -0,0 +1,185 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: infopark-crm-helpers
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Huy Dinh
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-05-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activemodel
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '4.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '4.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: infopark_webcrm_sdk
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: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.11'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.11'
69
+ - !ruby/object:Gem::Dependency
70
+ name: fakeweb
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rake
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '10.0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '10.0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rspec
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '3.0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '3.0'
125
+ description: infopark-crm-helpers provides helper mixins and validators to use with
126
+ the Infopark WebCRM. It is based on the Infopark WebCRM SDK for Ruby.
127
+ email:
128
+ - mail@huydinh.eu
129
+ executables: []
130
+ extensions: []
131
+ extra_rdoc_files: []
132
+ files:
133
+ - ".codeclimate.yml"
134
+ - ".crm.example.yml"
135
+ - ".gitignore"
136
+ - ".rspec"
137
+ - ".travis.yml"
138
+ - Gemfile
139
+ - LICENSE.txt
140
+ - README.md
141
+ - Rakefile
142
+ - bin/console
143
+ - bin/setup
144
+ - changelog.md
145
+ - infopark-crm-helpers.gemspec
146
+ - lib/crm/helper.rb
147
+ - lib/crm/helpers.rb
148
+ - lib/crm/helpers/attributes.rb
149
+ - lib/crm/helpers/validators/crm_boolean_validator.rb
150
+ - lib/crm/helpers/validators/crm_datetime_validator.rb
151
+ - lib/crm/helpers/validators/crm_each_validator.rb
152
+ - lib/crm/helpers/validators/crm_enum_validator.rb
153
+ - lib/crm/helpers/validators/crm_integer_validator.rb
154
+ - lib/crm/helpers/validators/crm_list_validator.rb
155
+ - lib/crm/helpers/validators/crm_multienum_validator.rb
156
+ - lib/crm/helpers/validators/crm_string_validator.rb
157
+ - lib/crm/helpers/validators/crm_text_validator.rb
158
+ - lib/crm/helpers/validators/crm_type_validator.rb
159
+ - lib/crm/helpers/validators/crm_validator_helper.rb
160
+ - lib/crm/helpers/version.rb
161
+ homepage: https://github.com/Skudo/infopark-crm-helpers
162
+ licenses:
163
+ - MIT
164
+ metadata: {}
165
+ post_install_message:
166
+ rdoc_options: []
167
+ require_paths:
168
+ - lib
169
+ required_ruby_version: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ required_rubygems_version: !ruby/object:Gem::Requirement
175
+ requirements:
176
+ - - ">="
177
+ - !ruby/object:Gem::Version
178
+ version: '0'
179
+ requirements: []
180
+ rubyforge_project:
181
+ rubygems_version: 2.5.1
182
+ signing_key:
183
+ specification_version: 4
184
+ summary: Helpers for the Infopark WebCRM SDK for Ruby.
185
+ test_files: []