populate-me 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b5d6a2a8ba2877bb7de857c1befde67845b87fd6
4
- data.tar.gz: 38ff9b163189c90109d325815ffd2cde81ca55ac
3
+ metadata.gz: 04d5994053986cb49a277559ebabe1caf7ca5e6a
4
+ data.tar.gz: 16f1d48e316b78fd11db94d03d70e6c78df8fb30
5
5
  SHA512:
6
- metadata.gz: c2d852eeff231cbb2d85865ba5f097d63fd8ed9d65279820189ea234bb2721bd610b53ec3b64b11de006d826aeb87fde5f0e2572c9d38b57f768ec2190372485
7
- data.tar.gz: d2af5a7f0e800f73c126fff0329aea00ba5c6b47727c36de2de84b1c476141cd38062de253eb37a4294fb3b2a9e7116989cfe17ad3c241704422b8159f9f1e49
6
+ metadata.gz: 2672c99f4bad24d2c1b30eae320001006e022455fdb0a9d3b638b916c8702a4f9ee45dc3e477d5aefca121ed493de31129e7faec245111df758495b30a8ff289
7
+ data.tar.gz: 2e93eebd1c02c7a52183f9d257cd888bd9649349ef546295a0a8a9750b2f522278903858d8f6a268338f581d2e68ef9f6679b6c7e20c9682757abe07bcf1a6aa
data/README.md CHANGED
@@ -80,6 +80,13 @@ Available types are:
80
80
  A `:list` type exists as well for nested documents, but it is not
81
81
  fully working yet.
82
82
 
83
+ The `field` method creates a getter and a setter for this particular field.
84
+
85
+ ```ruby
86
+ blog_article.published # Returns true or false
87
+ blog_article.published = true
88
+ ```
89
+
83
90
  ### Validations
84
91
 
85
92
  In its simplest form, validations are done by overriding the `#validate` method and declaring errors with the `#error_on` method.
@@ -130,6 +137,19 @@ class BlogArticle::Comment < PopulateMe::Document
130
137
  end
131
138
  ```
132
139
 
140
+ The `relationship` method creates 2 getters for this particular field,
141
+ one with the same name and one with `_first` at the end. Both are cached
142
+ so that the database is queried only once.
143
+
144
+ ```ruby
145
+ blog_article.comments # Returns all the comments for this article
146
+ blog_article.comments_first # Returns the first comment for this article
147
+ ```
148
+
149
+ It uses the `PopulateMe::Document::admin_find` and
150
+ `PopulateMe::Document::admin_find_first` methods in the background,
151
+ so default sorting order is respected.
152
+
133
153
  ### Callbacks
134
154
 
135
155
  There are some classic hooks which trigger the callbacks you declare.
data/example/config.ru CHANGED
@@ -12,8 +12,8 @@ require 'populate_me/document'
12
12
  # DB = MONGO.database
13
13
  # PopulateMe::Mongo.set :db, DB
14
14
 
15
- # require 'populate_me/attachment'
16
- # PopulateMe::Document.set :default_attachment_class, PopulateMe::Attachment
15
+ require 'populate_me/attachment'
16
+ PopulateMe::Document.set :default_attachment_class, PopulateMe::Attachment
17
17
  # require 'populate_me/file_system_attachment'
18
18
  # PopulateMe::Document.set :default_attachment_class, PopulateMe::FileSystemAttachment
19
19
  #
@@ -21,16 +21,16 @@ require 'populate_me/document'
21
21
  # PopulateMe::Mongo.set :default_attachment_class, PopulateMe::GridFS
22
22
  # PopulateMe::GridFS.set :db, DB
23
23
 
24
- require 'populate_me/s3_attachment'
25
- s3_resource = Aws::S3::Resource.new
26
- s3_bucket = s3_resource.bucket(ENV['BUCKET'])
27
- PopulateMe::Document.set :default_attachment_class, PopulateMe::S3Attachment
28
- PopulateMe::S3Attachment.set :bucket, s3_bucket
24
+ # require 'populate_me/s3_attachment'
25
+ # s3_resource = Aws::S3::Resource.new
26
+ # s3_bucket = s3_resource.bucket(ENV['BUCKET'])
27
+ # PopulateMe::Document.set :default_attachment_class, PopulateMe::S3Attachment
28
+ # PopulateMe::S3Attachment.set :bucket, s3_bucket
29
29
 
30
30
 
31
31
  class BlogPost < PopulateMe::Document
32
32
  field :title, required: true
33
- field :thumbnail, type: :attachment, variations: [
33
+ field :thumbnail, type: :attachment, max_size: 600000, variations: [
34
34
  PopulateMe::Variation.new_image_magick_job(:populate_me_thumb, :jpg, "-resize '400x225^' -gravity center -extent 400x225")
35
35
  ]
36
36
  field :content, type: :text
@@ -72,7 +72,7 @@ class Admin < PopulateMe::Admin
72
72
  ]
73
73
  end
74
74
 
75
- # use PopulateMe::Attachment::Middleware
75
+ use PopulateMe::Attachment::Middleware
76
76
  # use PopulateMe::FileSystemAttachment::Middleware
77
77
  # use PopulateMe::GridFS::Middleware
78
78
  run Admin
@@ -101,44 +101,76 @@ $(function() {
101
101
  });
102
102
  };
103
103
 
104
+ // JS Validations
105
+ var jsValidationsPassed = function(context) {
106
+ if (window.File && window.FileReader && window.FileList && window.Blob) {
107
+ try {
108
+ var max_size_fields = $('input[type=file][data-max-size]', context);
109
+ max_size_fields.each(function() {
110
+ var field = $(this);
111
+ var max_size = parseInt(field.data('max-size'));
112
+ if (field[0].files[0]) {
113
+ var fsize = field[0].files[0].size;
114
+ var fname = field[0].files[0].name;
115
+ if (fsize>max_size) {
116
+ alert('File too big: '+fname+' should be less than '+max_size/1000+'KB.');
117
+ throw "Validation error";
118
+ }
119
+ }
120
+ });
121
+ } catch(e) {
122
+ if (e==='Validation error') {
123
+ return false;
124
+ } else {
125
+ throw(e);
126
+ }
127
+ }
128
+ }
129
+ return true;
130
+ };
131
+
104
132
  // Ajax form
105
133
  $('body').on('submit','form.admin-post, form.admin-put', function(e) {
106
134
  e.preventDefault();
107
135
  var self = $(this);
108
- var submit_button = $('input[type=submit]',self);
109
- submit_button.hide();
110
- $.ajax({
111
- url: self.attr('action'),
112
- type: (self.is('.admin-put') ? 'put' : 'post'),
113
- data: new FormData(this),
114
- processData: false,
115
- contentType: false,
116
- success: function(res) {
117
- if (res.success==true) {
118
- var reloader = finder.find('> li:nth-last-child(3) .selected')
119
- if (reloader.size()>0) {
120
- reloader.trigger('click.columnav',[function(cb_object) {
121
- var target = $('[data-id='+res.data.id+']', cb_object.column);
122
- if (target.size()>0) {
123
- scroll_to(target, cb_object.column);
124
- }
125
- }]);
126
- } else {
127
- finder.trigger('pop.columnav');
136
+ if (jsValidationsPassed(self)) {
137
+
138
+ var submit_button = $('input[type=submit]',self);
139
+ submit_button.hide();
140
+ $.ajax({
141
+ url: self.attr('action'),
142
+ type: (self.is('.admin-put') ? 'put' : 'post'),
143
+ data: new FormData(this),
144
+ processData: false,
145
+ contentType: false,
146
+ success: function(res) {
147
+ if (res.success==true) {
148
+ var reloader = finder.find('> li:nth-last-child(3) .selected')
149
+ if (reloader.size()>0) {
150
+ reloader.trigger('click.columnav',[function(cb_object) {
151
+ var target = $('[data-id='+res.data.id+']', cb_object.column);
152
+ if (target.size()>0) {
153
+ scroll_to(target, cb_object.column);
154
+ }
155
+ }]);
156
+ } else {
157
+ finder.trigger('pop.columnav');
158
+ }
159
+ }
160
+ },
161
+ error: function(xhr) {
162
+ res = xhr.responseJSON;
163
+ if (res.success==false) {
164
+ $('.invalid',self).removeClass('invalid');
165
+ $('.errors',self).remove();
166
+ mark_errors(self,res.data);
167
+ scroll_to(self.find('.invalid:first'));
168
+ submit_button.show();
128
169
  }
129
170
  }
130
- },
131
- error: function(xhr) {
132
- res = xhr.responseJSON;
133
- if (res.success==false) {
134
- $('.invalid',self).removeClass('invalid');
135
- $('.errors',self).remove();
136
- mark_errors(self,res.data);
137
- scroll_to(self.find('.invalid:first'));
138
- submit_button.show();
139
- }
140
- }
141
- });
171
+ });
172
+
173
+ }
142
174
  });
143
175
 
144
176
  // Create nested document
@@ -129,7 +129,7 @@
129
129
  <button class='attachment-deleter'>x</button>
130
130
  <br />
131
131
  {{/url}}
132
- <input type='file' name='{{input_name}}' {{{build_input_atrributes}}} />
132
+ <input type='file' name='{{input_name}}' {{#max_size}}data-max-size='{{max_size}}'{{/max_size}} {{{build_input_atrributes}}} />
133
133
  </script>
134
134
 
135
135
  <script id="template-list-field" type="x-tmpl-mustache">
@@ -1,4 +1,4 @@
1
1
  module PopulateMe
2
- VERSION = '0.3.0'
2
+ VERSION = '0.4.0'
3
3
  end
4
4
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: populate-me
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mickael Riga
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-01-23 00:00:00.000000000 Z
11
+ date: 2018-01-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: web-utils