faalis_contacts 0.1.0 → 0.3.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: 72d5b3c9afd35acd10df5e77c27fbbb41bcc6c59
4
- data.tar.gz: 0d20369344fed60c149abf2479a42d4dff001c74
3
+ metadata.gz: 7a36c809d5f0f7f6843cedfc245ba4a88efe4f36
4
+ data.tar.gz: 0f1b83bc292c7e01bf220f552c2bbfc46566e72a
5
5
  SHA512:
6
- metadata.gz: d87494409daf5a7b2470f0512ace91b1337e8ff97be953854d8f3c31dd057f1b48346e18e23b59b836c1e0bcbd38edfff0a10d93ff7307d5864cc5c4da0e6daf
7
- data.tar.gz: 780bcdf556b46b977e5a0e50dd708ca0af5d414bf21655e909d226e66e70bc12b585fac2fbee3847a17998373610c48c27807be78623f241b896b0f52a71995a
6
+ metadata.gz: 342f28942b576db72489e94abf45e895d1de1896232bd142f36cbf088606fe03595d6659c164110f5bf5acecd0e46563f8bfebdb5a576e7fdc38f10fcfec6775
7
+ data.tar.gz: df1b97486077e0bd7d0a5b2f5939bf28a3a36d55084ca35d4511907ed7405a9240d2a20b0b1ce795d82f93376eb0ddfd8f0a1e4225e7d4262296c76a0c07aa76
@@ -288,7 +288,15 @@ Contacts.controller("AddContactController", ["Restangular", "$scope", "$location
288
288
 
289
289
  $scope.remove_row = function(){
290
290
  $scope.details_count.pop();
291
- $scope.details.pop();
291
+ var lastobj = $scope.details.pop();
292
+ if ("id" in lastobj) {
293
+ API.all("contact_details").customDELETE(lastobj.id)
294
+ .then(function(data) {
295
+ success_message(data.msg);
296
+ }, function(data){
297
+ catch_error(data);
298
+ });
299
+ }
292
300
  };
293
301
 
294
302
  $scope.have = function(field, obj_id) {
@@ -307,6 +315,9 @@ Contacts.controller("AddContactController", ["Restangular", "$scope", "$location
307
315
  };
308
316
 
309
317
  $scope.save = function(save_another){
318
+ $("small.error").html("");
319
+ $("small.error").removeClass("error");
320
+
310
321
  var contact = {contact: {
311
322
  prefix: $scope.prefix,
312
323
  first_name: $scope.first_name,
@@ -105,7 +105,11 @@ ContactFields.controller("AddContactFieldController", ["Restangular", "$scope",
105
105
  var is_copy = false;
106
106
 
107
107
 
108
-
108
+ $scope.value_type_choices = _.sortBy([
109
+ {name: "string", title: "String"},
110
+ {name: "numeric", title: "Numeric"},
111
+ {name: "email", title: "Email"},
112
+ ], "title");
109
113
  if( "id" in $routeParams ){
110
114
  $scope.obj_id = $routeParams.id;
111
115
  $scope.editing = true;
@@ -118,6 +122,8 @@ ContactFields.controller("AddContactFieldController", ["Restangular", "$scope",
118
122
  .then(function(data) {
119
123
 
120
124
  $scope.name = data.name;
125
+ $scope.value_type = data.value_type;
126
+ $scope.validation_rules = data.validation_rules;
121
127
  }, function(data){
122
128
  catch_error(data);
123
129
  });
@@ -143,6 +149,8 @@ ContactFields.controller("AddContactFieldController", ["Restangular", "$scope",
143
149
  $scope.save = function(save_another){
144
150
  var contact_field = {contact_field: {
145
151
  name: $scope.name,
152
+ value_type: $scope.value_type,
153
+ validation_rules: $scope.validation_rules,
146
154
  __res__: 0
147
155
  }};
148
156
  if (($scope.obj_id) && (is_copy === false)) {
@@ -0,0 +1,18 @@
1
+ class API::V1::ContactDetailsController < Faalis::APIController
2
+
3
+ def destroy
4
+ ids = params[:id].split(",")
5
+ @contact_details = ::ContactDetails.where(:id => ids)
6
+ authorize! :destroy, ContactDetails
7
+ @contact_details.destroy_all
8
+ end
9
+
10
+ def build_resource
11
+ @contact_details = ::ContactDetails.new(resource_params)
12
+ end
13
+
14
+ def resource_params
15
+ params.require(:contact_details).permit(:id, :details_value)
16
+ end
17
+
18
+ end
@@ -8,7 +8,7 @@ class API::V1::ContactFieldsController < Faalis::APIController
8
8
  end
9
9
 
10
10
  def create
11
-
11
+
12
12
  if @contact_field.save
13
13
  respond_with(@contact_field)
14
14
  else
@@ -23,7 +23,7 @@ class API::V1::ContactFieldsController < Faalis::APIController
23
23
  end
24
24
 
25
25
  def update
26
-
26
+
27
27
 
28
28
  if @contact_field.update(resource_params)
29
29
  respond_with(@contact_field)
@@ -46,7 +46,7 @@ class API::V1::ContactFieldsController < Faalis::APIController
46
46
  end
47
47
 
48
48
  def resource_params
49
- params.require(:contact_field).permit(:id, :name)
49
+ params.require(:contact_field).permit(:id, :name, :value_type, :validation_rules)
50
50
  end
51
51
 
52
52
  end
@@ -8,24 +8,7 @@ class API::V1::ContactsController < Faalis::APIController
8
8
  end
9
9
 
10
10
  def create
11
- details = params[:contact][:details] || []
12
- details_list = []
13
- details.each do |detail|
14
- contact_detail = ContactDetails.create!(:detail_field => detail[:field],
15
- :detail_type => detail[:type],
16
- :detail_value => detail[:value])
17
- details_list << contact_detail
18
- end
19
-
20
- @contact.details = details_list
21
-
22
- if @contact.save
23
- respond_with(@contact)
24
- else
25
- respond_to do |format|
26
- format.json { render :json => {:fields => @contact.errors}, :status => :unprocessable_entity }
27
- end
28
- end
11
+ create_or_update(:create)
29
12
  end
30
13
 
31
14
  def show
@@ -33,24 +16,7 @@ class API::V1::ContactsController < Faalis::APIController
33
16
  end
34
17
 
35
18
  def update
36
- details = params[:contact][:details] || []
37
- details_list = []
38
- details.each do |detail|
39
- contact_detail = ContactDetails.create!(:detail_field => detail[:field],
40
- :detail_type => detail[:type],
41
- :detail_value => detail[:value])
42
- details_list << contact_detail
43
- end
44
-
45
- @contact.details = details_list
46
-
47
- if @contact.update(resource_params)
48
- respond_with(@contact)
49
- else
50
- respond_to do |format|
51
- format.json { render :json => {:fields => @contact.errors}, :status => :unprocessable_entity }
52
- end
53
- end
19
+ create_or_update(:update)
54
20
  end
55
21
 
56
22
  def destroy
@@ -68,4 +34,76 @@ class API::V1::ContactsController < Faalis::APIController
68
34
  params.require(:contact).permit(:id, :prefix, :first_name, :middle_name, :last_name, :suffix, :organization, :is_organization)
69
35
  end
70
36
 
37
+ private
38
+
39
+ def create_or_update(action)
40
+ details = params[:contact][:details] || []
41
+ details_list = []
42
+ details_errors = []
43
+ valid = true
44
+
45
+ details.each do |detail|
46
+
47
+ contact_detail = ContactDetails.find_or_initialize_by(:detail_field_id => detail[:field],
48
+ :detail_type => detail[:type],
49
+ :detail_value => detail[:value])
50
+
51
+ if not contact_detail.valid?
52
+ details_errors << contact_detail.errors
53
+ valid = false
54
+ else
55
+ details_errors << {}
56
+ details_list << contact_detail
57
+ end
58
+ end
59
+
60
+ if valid
61
+ details_list.each { |x| x.save }
62
+ @contact.details = details_list
63
+ end
64
+
65
+ result = @contact.update(resource_params) if action == :update
66
+ result = @contact.save if action == :create
67
+
68
+ if result and valid
69
+ respond_with(@contact)
70
+ else
71
+ errors = get_details_error(@contact, details_errors)
72
+ respond_to do |format|
73
+ format.json { render :json => errors, :status => :unprocessable_entity }
74
+ end
75
+ end
76
+
77
+
78
+ end
79
+
80
+ def get_details_error(contact, gerrors)
81
+ errors = {:fields => contact.errors}
82
+ counter = 0
83
+
84
+ contact.details.each do |detail|
85
+ if not detail.errors.empty?
86
+ detail.errors.each do |key, value|
87
+ errors[:fields]["#{key}_#{counter}".to_sym] = value
88
+ if errors[:fields]["#{key}_#{counter}".to_sym].length > 1
89
+ errors[:fields]["#{key}_#{counter}".to_sym].uniq!
90
+ end
91
+ end
92
+ end
93
+ counter += 1
94
+ end
95
+
96
+ counter = 0
97
+ gerrors.each do |err|
98
+ err.each do |key, value|
99
+ errors[:fields]["#{key}_#{counter}".to_sym] = value
100
+ if errors[:fields]["#{key}_#{counter}".to_sym].length > 1
101
+ errors[:fields]["#{key}_#{counter}".to_sym].uniq!
102
+ end
103
+ end
104
+ counter += 1
105
+ end
106
+ errors
107
+ end
108
+
71
109
  end
@@ -3,4 +3,5 @@ class Contact < ActiveRecord::Base
3
3
 
4
4
  validates :first_name, :presence => true
5
5
  validates :last_name, :presence => true
6
+ #validates_with ValueValidator
6
7
  end
@@ -1,5 +1,34 @@
1
1
  class ContactDetails < ActiveRecord::Base
2
+ @already_validate = false
3
+
2
4
  belongs_to :contact
5
+ belongs_to :field, :class_name => "ContactField"
6
+
7
+ validates :detail_field_id, :presence => true
8
+ validates :detail_type, :presence => true
9
+ validates :detail_value, :presence => true
10
+
11
+ validate :validate_value
12
+
3
13
 
14
+ def validate_value
15
+ require 'json'
16
+ if @already_validate
17
+ return
18
+ end
4
19
 
20
+ print "----" * 1000
21
+ if detail_field_id.nil?
22
+ errors[:detail_field] = _("Field can not be empty.")
23
+ return
24
+ end
25
+ rules = ContactField.find(detail_field_id).validation_rules
26
+ params = JSON.parse(rules).deep_symbolize_keys
27
+ @already_validate = true
28
+ begin
29
+ self.class.validates :detail_value, **params
30
+ rescue ArgumentError => e
31
+ errors[:error] = _("Field validation rules is not valid!")
32
+ end
33
+ end
5
34
  end
@@ -1,2 +1,4 @@
1
1
  class ContactField < ActiveRecord::Base
2
+ validates :name, :presence => true
3
+ validates :value_type, :presence => true
2
4
  end
@@ -4,6 +4,14 @@
4
4
  <b translate>Name :</b> {{object.name}}
5
5
  </p>
6
6
 
7
+ <p>
8
+ <b translate>Value_type :</b> {{object.value_type}}
9
+ </p>
10
+
11
+ <p>
12
+ <b translate>Validation_rules :</b> {{object.validation_rules}}
13
+ </p>
14
+
7
15
  <ul class="button-group float-right">
8
16
  <a ng-href="#/contact_fields/{{ object.id }}/edit" class="small">
9
17
  <span class="fa fa-edit"></span> <span translated>Edit</span>
@@ -20,6 +20,30 @@
20
20
 
21
21
 
22
22
 
23
+
24
+ <div class="small-4 columns float-left small-offset-2">
25
+ <label><span translate>Value type</span> :</label>
26
+
27
+ <select id="id_value_type" ui-select2="select2options" ng-model="value_type" name="value_type" >
28
+ <option ng-repeat="item in value_type_choices" value="{{ item.name }}" title="{{ item.title }}" ng-selected="value_type === item.name">{{ item.title }}</option>
29
+ </select>
30
+
31
+ <small id="id_value_type_msg"></small>
32
+ </div>
33
+ </div>
34
+
35
+
36
+ <div class="row">
37
+ <div class="small-4 columns ">
38
+ <label><span translate>Validation rules</span> :</label>
39
+
40
+ <textarea id="id_validation_rules" class="rounded" ng-model="validation_rules" name="validation_rules" ></textarea>
41
+
42
+ <small id="id_validation_rules_msg"></small>
43
+ </div>
44
+
45
+
46
+
23
47
  </fieldset>
24
48
  </form>
25
49
 
@@ -0,0 +1 @@
1
+ json.msg _("Items removed successfully.")
@@ -1,2 +1,2 @@
1
- json.extract! @contact_field, :id, :name
1
+ json.extract! @contact_field, :id, :name, :value_type, :validation_rules
2
2
 
@@ -1,3 +1,3 @@
1
1
  json.array! @contact_fields do |contact_field|
2
- json.extract! contact_field, :id, :name
2
+ json.extract! contact_field, :id, :name, :value_type, :validation_rules
3
3
  end
@@ -1,2 +1,4 @@
1
- json.extract! @contact_field, :id, :name
1
+ json.extract! @contact_field, :id, :name, :value_type, :validation_rules
2
+
3
+
2
4
 
@@ -1,2 +1,2 @@
1
- json.extract! @contact_field, :id, :name
1
+ json.extract! @contact_field, :id, :name, :value_type, :validation_rules
2
2
 
@@ -3,7 +3,7 @@ json.array! @contacts do |contact|
3
3
  json.details contact.details do |detail|
4
4
  json.id detail.id
5
5
  json.type detail.detail_type
6
- json.field detail.detail_field
6
+ json.field detail.detail_field_id
7
7
  json.value detail.detail_value
8
8
  end
9
9
  end
@@ -2,6 +2,6 @@ json.extract! @contact, :id, :prefix, :first_name, :middle_name, :last_name, :su
2
2
  json.details @contact.details do |detail|
3
3
  json.id detail.id
4
4
  json.type detail.detail_type
5
- json.field detail.detail_field
5
+ json.field detail.detail_field_id
6
6
  json.value detail.detail_value
7
7
  end
data/config/routes.rb CHANGED
@@ -4,6 +4,7 @@ Rails.application.routes.draw do
4
4
  resources :contacts, :except => [:new]
5
5
  resources :contact_types, :except => [:new]
6
6
  resources :contact_fields, :except => [:new]
7
+ resources :contact_details, :only => [:destroy], :class_name => "ContactDetails"
7
8
  end
8
9
  end
9
10
  end
@@ -1,7 +1,7 @@
1
1
  class CreateContactDetails < ActiveRecord::Migration
2
2
  def change
3
3
  create_table :contact_details do |t|
4
- t.string :detail_field
4
+ t.integer :detail_field_id
5
5
  t.string :detail_type
6
6
  t.string :detail_value
7
7
 
@@ -2,7 +2,8 @@ class CreateContactFields < ActiveRecord::Migration
2
2
  def change
3
3
  create_table :contact_fields do |t|
4
4
  t.string :name
5
-
5
+ t.string :value_type
6
+ t.text :validation_rules
6
7
  t.timestamps
7
8
  end
8
9
  end
@@ -1,3 +1,3 @@
1
1
  module FaalisContacts
2
- VERSION = "0.1.0"
2
+ VERSION = "0.3.0"
3
3
  end
data/lib/tasks/build.rake CHANGED
@@ -7,7 +7,7 @@ namespace :build do
7
7
 
8
8
  desc "Create contact fields scaffold"
9
9
  task :fields do
10
- system "rails g faalis:js_scaffold contact_field name:string --no-bulk --required name"
10
+ system "rails g faalis:js_scaffold contact_field name:string value_type:in:string,numeric,email validation_rules:text --no-bulk --required name --path faalis_contacts"
11
11
  end
12
12
 
13
13
  desc "Create contact type scaffold"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: faalis_contacts
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sameer Rahmani
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-01-13 00:00:00.000000000 Z
12
+ date: 2014-01-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -145,12 +145,14 @@ files:
145
145
  - app/views/api/v1/contacts/create.json.jbuilder
146
146
  - app/views/api/v1/contacts/update.json.jbuilder
147
147
  - app/views/api/v1/contacts/index.json.jbuilder
148
+ - app/views/api/v1/contact_details/destroy.json.jbuilder
148
149
  - app/assets/javascripts/faalis_contacts/application.js
149
150
  - app/assets/javascripts/faalis_contacts/modules/contact.js
150
151
  - app/assets/javascripts/faalis_contacts/modules/contact_type.js
151
152
  - app/assets/javascripts/faalis_contacts/modules/contact_field.js
152
153
  - app/controllers/api/v1/contact_fields_controller.rb
153
154
  - app/controllers/api/v1/contact_types_controller.rb
155
+ - app/controllers/api/v1/contact_details_controller.rb
154
156
  - app/controllers/api/v1/contacts_controller.rb
155
157
  - config/routes.rb
156
158
  - db/migrate/20140113071029_create_contact_fields.rb