infopark-crm-helpers 1.0.2 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f67a52709806c3af34201d8521a7f4ffb380f8f0
4
- data.tar.gz: 080c86daf942030ffda72ff30836cff775ea350c
3
+ metadata.gz: eea6fceef4486786190a225cbc9bed5eb085df59
4
+ data.tar.gz: fe8160fcbd9a2a8c363e3775448125c4c5066bb3
5
5
  SHA512:
6
- metadata.gz: 6a4181f5800b1e9efed341d41671fb3be5bb3d934deaba0a6e8cfee25c432d139bbbe4d72f322604d14665aa9f16cc3810454887d01dd7a782bd3315f4e36026
7
- data.tar.gz: 94fd635728f590b81f1471508304729ddfdf6d29d97de0309b449471d2e5cfec731c0b10c4d6382c66de2fce4b8a698073e9e9fb3da79dceefd9878ef9a9306e
6
+ metadata.gz: e25ab420786e572ddd640aee45f2c829a60feb4bbed053690f1fe8877eb320203bec1d428657203551242cdedaf8e3bc3a2020e1920aa495b7165f11de4b74a2
7
+ data.tar.gz: 3fcbe7e7877fd07be90bfce405351d90d59d6005b5a2003fc7fd6caee8c9e418512f9a3e50eb46df4fe73a21e501efb560111d2b6b1fffe3ddca5cbb47bbcf3c
data/README.md CHANGED
@@ -188,3 +188,59 @@ You can use those to explicitly validate attributes for a certain format.
188
188
  validates_with Crm::Helpers::Validators::CrmBooleanValidator,
189
189
  attributes: [:custom_has_ps4, :custom_has_xbox_one]
190
190
  ```
191
+
192
+ ## Finders
193
+
194
+ ```ruby
195
+ class Customer
196
+ include ActiveModel::Validations
197
+ include Crm::Helpers::Attributes
198
+ include Crm::Helpers::Finders
199
+
200
+ represents_crm_type :contact
201
+
202
+ crm_attr_accessor :first_name, :last_name, :email
203
+ validates_with Crm::Helpers::Validators::CrmTypeValidator
204
+
205
+ def initialize(attributes = {})
206
+ @crm_attributes = attributes.dup
207
+ end
208
+ end
209
+
210
+ customer = Customer.find('fc851ba935f8420824498aee739ac897')
211
+ # => #<Customer>
212
+ ```
213
+
214
+ ## Persistence
215
+
216
+ ```ruby
217
+ class Customer
218
+ include ActiveModel::Validations
219
+ include Crm::Helpers::Attributes
220
+ include Crm::Helpers::Persistence
221
+
222
+ represents_crm_type :contact
223
+
224
+ crm_attr_accessor :first_name, :last_name, :email
225
+ validates_with Crm::Helpers::Validators::CrmTypeValidator
226
+
227
+ def initialize(attributes = {})
228
+ @crm_attributes = attributes.dup
229
+ end
230
+ end
231
+
232
+ customer = Customer.create(language: 'en', last_name: 'Dinh')
233
+ # => #<Customer>
234
+
235
+ customer.language = 'de'
236
+ # => 'de'
237
+
238
+ customer.save
239
+ # => true
240
+
241
+ customer.update(language: 'en')
242
+ # => true
243
+
244
+ customer.destroy
245
+ # => #<Customer>
246
+ ```
data/changelog.md CHANGED
@@ -1,3 +1,9 @@
1
+ ### 1.1.0
2
+
3
+ - Add `assign_attributes` to `Crm::Helpers::Attributes`.
4
+ - Add `Crm::Helpers::Finders`, which provides `.find` and `find_by_query`.
5
+ - Add `Crm::Helpers::Persistence`, which provides `.create`, `#save`, `#save!`, `#update`, `#update!`, and `#destroy`.
6
+
1
7
  ### 1.0.2
2
8
 
3
9
  - Fix an issue wherein some `crm_attr_reader` were not created correctly.
@@ -8,12 +8,23 @@ module Crm
8
8
  module ClassMethods
9
9
  attr_reader :crm_type
10
10
 
11
- def represents_crm_type(type)
12
- @crm_type = type
11
+ def represents_crm_type(crm_type)
12
+ @crm_type = crm_type
13
+ @crm_class = nil
13
14
  @crm_attributes = {}.with_indifferent_access
15
+
14
16
  crm_attr_accessor(*mandatory_crm_attributes)
15
17
  end
16
18
 
19
+ def crm_class
20
+ return nil if @crm_type.blank?
21
+ return @crm_class if @crm_class.present?
22
+
23
+ type_definition = crm_type_definition(crm_type)
24
+ class_name = "Crm::#{type_definition.item_base_type}"
25
+ @crm_class = class_name.constantize
26
+ end
27
+
17
28
  def mandatory_crm_attributes
18
29
  crm_attributes.select { |_, definition| definition[:mandatory] }.keys.sort.map(&:to_sym)
19
30
  end
@@ -70,7 +81,7 @@ module Crm
70
81
  protected
71
82
 
72
83
  def collect_crm_attributes_data(crm_type)
73
- type = Crm::Type.find(crm_type)
84
+ type = crm_type_definition(crm_type)
74
85
  @crm_attributes = type.standard_attribute_definitions
75
86
  # This is a lovely hack, because the language attribute does not get
76
87
  # the correct valid values in #standard_attribute_definitions. Maybe
@@ -78,6 +89,14 @@ module Crm
78
89
  @crm_attributes[:language][:valid_values] += type.languages if @crm_attributes[:language].present?
79
90
  @crm_attributes.merge!(type.attribute_definitions)
80
91
  end
92
+
93
+ def crm_type_definition(crm_type)
94
+ Crm::Type.find(crm_type)
95
+ end
96
+ end
97
+
98
+ def assign_attributes(new_attributes)
99
+ @crm_attributes = crm_attributes.merge(new_attributes)
81
100
  end
82
101
 
83
102
  def crm_attributes
@@ -0,0 +1,25 @@
1
+ module Crm
2
+ module Helpers
3
+ module Finders
4
+ def self.included(base)
5
+ base.extend self
6
+ end
7
+
8
+ def find(id)
9
+ crm_object = crm_class.find(id)
10
+ return nil if crm_object.blank?
11
+
12
+ new(crm_object.attributes)
13
+ end
14
+
15
+ def find_by_query(query, options = {})
16
+ limit = options[:limit] || 50
17
+ sort_order = options[:sort_order] || 'desc'
18
+
19
+ crm_class.query(query).limit(limit).sort_order(sort_order).to_a.map do |crm_object|
20
+ new(crm_object.attributes)
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,67 @@
1
+ module Crm
2
+ module Helpers
3
+ module Persistence
4
+ def self.included(base)
5
+ base.extend ClassMethods
6
+ end
7
+
8
+ module ClassMethods
9
+ def create(attributes = {})
10
+ instance = new(attributes)
11
+ return instance if instance.invalid?
12
+
13
+ instance.save!
14
+ instance
15
+ end
16
+ end
17
+
18
+ def save
19
+ update
20
+ end
21
+
22
+ def save!
23
+ update!
24
+ end
25
+
26
+ def update(attributes = {})
27
+ assign_attributes(attributes)
28
+ return false if invalid?
29
+
30
+ persist
31
+ end
32
+
33
+ def update!(attributes = {})
34
+ assign_attributes(attributes)
35
+ raise "#{self.class.name} object is invalid." if invalid?
36
+
37
+ persist
38
+ end
39
+
40
+ def destroy
41
+ crm_object.destroy
42
+ self
43
+ end
44
+
45
+ def persist
46
+ return false if invalid?
47
+
48
+ @crm_object = if crm_object.nil?
49
+ self.class.crm_class.create(crm_attributes)
50
+ else
51
+ crm_object.update(crm_attributes)
52
+ end
53
+ assign_attributes(crm_object.attributes)
54
+ true
55
+ end
56
+
57
+ protected
58
+
59
+ def crm_object
60
+ return @crm_object if defined?(@crm_object)
61
+ return nil if id.blank?
62
+
63
+ @crm_object = self.class.crm_class.find(id)
64
+ end
65
+ end
66
+ end
67
+ end
@@ -1,5 +1,5 @@
1
1
  module Crm
2
2
  module Helpers
3
- VERSION = '1.0.2'.freeze
3
+ VERSION = '1.1.0'.freeze
4
4
  end
5
5
  end
data/lib/crm/helpers.rb CHANGED
@@ -16,6 +16,8 @@ require 'crm/helpers/validators/crm_text_validator'
16
16
  require 'crm/helpers/validators/crm_type_validator'
17
17
 
18
18
  require 'crm/helpers/attributes'
19
+ require 'crm/helpers/finders'
20
+ require 'crm/helpers/persistence'
19
21
  require 'crm/helpers/version'
20
22
 
21
23
  require 'crm/helper'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: infopark-crm-helpers
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Huy Dinh
@@ -146,6 +146,8 @@ files:
146
146
  - lib/crm/helper.rb
147
147
  - lib/crm/helpers.rb
148
148
  - lib/crm/helpers/attributes.rb
149
+ - lib/crm/helpers/finders.rb
150
+ - lib/crm/helpers/persistence.rb
149
151
  - lib/crm/helpers/validators/crm_boolean_validator.rb
150
152
  - lib/crm/helpers/validators/crm_datetime_validator.rb
151
153
  - lib/crm/helpers/validators/crm_each_validator.rb