populate-me 0.13.1 → 0.16.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
  SHA256:
3
- metadata.gz: 77ffe6684618f49dc39e02b871af4e4099fe306f95e78fbe4ebcd6610910cdde
4
- data.tar.gz: 884fd2dd42222de62bc684ab6671c88671836d764d0b985a808fb4990eee062c
3
+ metadata.gz: a92596c5ed11fb3d67f18b48fdc1011ecf423f26390c2f71c3587c3c2cae93cf
4
+ data.tar.gz: 70e4e6572ed7e98ace3444ee6c6c18ff937b0ed72aa35b80a5c5d2ad99865d91
5
5
  SHA512:
6
- metadata.gz: 454c3c3bbfd309e6449ec58f9c46246dcc359fee2ae04aa04e684285c68619d8f9a8b529ec9d843e5ab722a52dfff0e70aac697d8713f023e45f5bace935a42a
7
- data.tar.gz: 22786caacc68f4b8d9ca60837a416c86ab037b87a3826519506b0eadf78b97693232b07312206e72cafba69b9e8bf447a02d38fe57c3585981b88f68f14580e7
6
+ metadata.gz: cd01c21a0a760cb1691453ca86f22a75cbabd423134bd25c699fbd0ea96ef6a1845a88d130c61f62fcec9a2f77fb527759152307fb0fb762f0778a2f657bbe3d
7
+ data.tar.gz: fc764eb27f6c4dd76bde7b952432dc0a647872a061f564f3ac9104511b7baecd0a67866b9777726e329619cd6e1b3663e107f3e774d908e794cfe100592c44b4
@@ -413,6 +413,8 @@ $(function() {
413
413
  process_data: function(data) {
414
414
  if (typeof data == 'object') {
415
415
  return PopulateMe.mustache_render(data);
416
+ } else if (typeof data == 'string' && data[0] == '{') {
417
+ return PopulateMe.mustache_render(JSON.parse(data));
416
418
  } else {
417
419
  return data;
418
420
  }
@@ -117,7 +117,14 @@
117
117
  </script>
118
118
 
119
119
  <script id="template-string-field" type="x-tmpl-mustache">
120
- <input name='{{input_name}}' value='{{input_value}}' {{#required}}required{{/required}}{{{build_input_attributes}}} />
120
+ <input name='{{input_name}}' value='{{input_value}}' {{#required}}required{{/required}}{{{build_input_attributes}}} {{#autocomplete.length}}list='datalist-{{id}}'{{/autocomplete.length}} />
121
+ {{#autocomplete.length}}
122
+ <datalist id='datalist-{{id}}'>
123
+ {{#autocomplete}}
124
+ <option value='{{.}}' />
125
+ {{/autocomplete}}
126
+ </datalist>
127
+ {{/autocomplete.length}}
121
128
  </script>
122
129
 
123
130
  <script id="template-text-field" type="x-tmpl-mustache">
@@ -145,7 +152,7 @@
145
152
 
146
153
  <script id="template-attachment-field" type="x-tmpl-mustache">
147
154
  {{#url}}
148
- <img src='{{url}}{{cache_buster}}' alt='Preview' width='150' />
155
+ <img src='{{url}}{{cache_buster}}' alt='Preview' width='150' onerror="this.src='<%= request.script_name %>/__assets__/img/file.png'; this.onerror=null;" />
149
156
  <button class='attachment-deleter'>x</button>
150
157
  <br />
151
158
  {{/url}}
@@ -67,6 +67,7 @@ class PopulateMe::Admin < Sinatra::Base
67
67
  if request.path_info=='/menu'
68
68
  items.push({title: '?', href: "#{request.script_name}/help", new_page: false})
69
69
  end
70
+ content_type :json
70
71
  {
71
72
  template: 'template_menu',
72
73
  page_title: page_title,
@@ -76,6 +77,7 @@ class PopulateMe::Admin < Sinatra::Base
76
77
 
77
78
  get '/list/:class_name' do
78
79
  @model_class = resolve_model_class params[:class_name]
80
+ content_type :json
79
81
  @model_class.to_admin_list(request: request, params: params).to_json
80
82
  end
81
83
 
@@ -87,6 +89,7 @@ class PopulateMe::Admin < Sinatra::Base
87
89
  else
88
90
  @model_instance = resolve_model_instance @model_class, params[:id]
89
91
  end
92
+ content_type :json
90
93
  @model_instance.to_admin_form(
91
94
  request: request,
92
95
  params: params,
@@ -102,6 +105,7 @@ class PopulateMe::Admin < Sinatra::Base
102
105
 
103
106
  not_found do
104
107
  response.headers['X-Cascade'] = 'pass'
108
+ content_type :json
105
109
  {'success'=>false,'message'=>'Not Found'}.to_json
106
110
  end
107
111
 
@@ -109,6 +113,7 @@ class PopulateMe::Admin < Sinatra::Base
109
113
  puts
110
114
  puts env['sinatra.error'].inspect
111
115
  puts
116
+ content_type :json
112
117
  {'success'=>false,'message'=>env['sinatra.error'].message}.to_json
113
118
  end
114
119
 
@@ -118,11 +123,17 @@ class PopulateMe::Admin < Sinatra::Base
118
123
  # Method = overridable = testable
119
124
  ENV['CERBERUS_PASS']
120
125
  end
126
+
121
127
  def cerberus_available?
122
128
  # Method = overridable = testable
123
129
  Rack.const_defined?(:Cerberus)
124
130
  end
125
131
 
132
+ def cerberus_auth user, pass, req
133
+ pass == cerberus_pass
134
+ end
135
+
136
+
126
137
  private
127
138
 
128
139
  def setup_default_middleware builder
@@ -146,8 +157,8 @@ class PopulateMe::Admin < Sinatra::Base
146
157
  return unless settings.cerberus_active
147
158
  cerberus_settings = settings.cerberus==true ? {} : settings.cerberus
148
159
  cerberus_settings[:session_key] = 'populate_me_user'
149
- builder.use Rack::Cerberus, cerberus_settings do |user,pass,req|
150
- pass==cerberus_pass
160
+ builder.use Rack::Cerberus, cerberus_settings do |user, pass, req|
161
+ cerberus_auth user, pass, req
151
162
  end
152
163
  end
153
164
 
@@ -21,6 +21,14 @@ module PopulateMe
21
21
  end
22
22
  end
23
23
 
24
+ def outcast_string field, item, o={}
25
+ if item.key? :autocomplete
26
+ item = item.dup
27
+ item[:autocomplete] = WebUtils.deep_copy(WebUtils.get_value(item[:autocomplete],self))
28
+ end
29
+ item
30
+ end
31
+
24
32
  def outcast_list field, item, o={}
25
33
  item = item.dup
26
34
  item[:items] = self.__send__(field).map do |nested|
@@ -1,4 +1,4 @@
1
1
  module PopulateMe
2
- VERSION = '0.13.1'
2
+ VERSION = '0.16.0'
3
3
  end
4
4
 
data/populate-me.gemspec CHANGED
@@ -21,7 +21,7 @@ Gem::Specification.new do |s|
21
21
  s.add_dependency 'sinatra', '~> 2'
22
22
  s.add_dependency 'json', '~> 2.1'
23
23
 
24
- s.add_development_dependency 'bundler', '~> 1.13'
24
+ s.add_development_dependency 'bundler', '>= 2.2.10'
25
25
  s.add_development_dependency 'minitest', '~> 5.8'
26
26
  s.add_development_dependency 'rack-test', '~> 0.6'
27
27
  s.add_development_dependency 'rack-cerberus', '~> 1.0'
data/test/test_admin.rb CHANGED
@@ -22,6 +22,12 @@ class AdminWithCerberusPass < Admin
22
22
  end
23
23
  end
24
24
 
25
+ class AdminWithCustomCerberusAuth < AdminWithCerberusPass
26
+ def self.cerberus_auth user, pass, req
27
+ [user, pass] == ['mario', '1234']
28
+ end
29
+ end
30
+
25
31
  class AdminCerberusNotAvailable < AdminWithCerberusPass
26
32
  def self.cerberus_available?
27
33
  false
@@ -106,19 +112,54 @@ describe PopulateMe::Admin do
106
112
  assert_equal 'text/css', last_response.content_type
107
113
  end
108
114
 
109
- describe 'when cerberus is active' do
115
+ describe 'When cerberus is active' do
110
116
  let(:app) { AdminWithCerberusPass.new }
111
117
  it 'Uses Cerberus for authentication' do
112
118
  get '/'
113
119
  assert_equal 401, last_response.status
114
120
  end
121
+ it 'Authenticates with right login details' do
122
+ get '/', {
123
+ cerberus_login: 'admin',
124
+ cerberus_pass: '123',
125
+ _method: 'get'
126
+ }
127
+ assert_equal 200, last_response.status
128
+ end
129
+ it 'Fails authentication when login details are wrong' do
130
+ get '/', {
131
+ cerberus_login: 'admin',
132
+ cerberus_pass: 'xxx',
133
+ _method: 'get'
134
+ }
135
+ assert_equal 401, last_response.status
136
+ end
115
137
  end
116
- describe 'when cerberus is inactive' do
138
+ describe 'When cerberus is inactive' do
117
139
  it 'Does not use Cerberus' do
118
140
  get '/'
119
141
  assert_predicate last_response, :ok?
120
142
  end
121
143
  end
144
+ describe 'When cerberus_auth is overridden' do
145
+ let(:app) { AdminWithCustomCerberusAuth.new }
146
+ it 'Authenticates with right login details' do
147
+ get '/', {
148
+ cerberus_login: 'mario',
149
+ cerberus_pass: '1234',
150
+ _method: 'get'
151
+ }
152
+ assert_equal 200, last_response.status
153
+ end
154
+ it 'Fails authentication when login details are wrong' do
155
+ get '/', {
156
+ cerberus_login: 'admin',
157
+ cerberus_pass: '123',
158
+ _method: 'get'
159
+ }
160
+ assert_equal 401, last_response.status
161
+ end
162
+ end
122
163
 
123
164
  end
124
165
 
@@ -6,6 +6,7 @@ class Outcasted < PopulateMe::Document
6
6
  set :default_attachment_class, PopulateMe::Attachment
7
7
 
8
8
  field :name
9
+ field :category, autocomplete: ['Fish', 'Cat', 'Bunny']
9
10
  field :size, type: :select, select_options: [
10
11
  {description: 'small', value: 's'},
11
12
  {description: 'medium', value: 'm'},
@@ -23,6 +24,10 @@ class Outcasted < PopulateMe::Document
23
24
  field :weirdo, type: :strange
24
25
  field :price, type: :price
25
26
 
27
+ def get_category_autocomplete_list
28
+ ['Horse', 'Bear']
29
+ end
30
+
26
31
  def get_size_options
27
32
  [
28
33
  [:small, :s],
@@ -65,6 +70,36 @@ describe PopulateMe::Document, 'Outcasting' do
65
70
 
66
71
  end
67
72
 
73
+ describe '#outcast_string' do
74
+
75
+ it 'Generates the autocomplete options when needed' do
76
+ original = Outcasted.fields[:category]
77
+ output = Outcasted.new.outcast(:category, original, {input_name_prefix: 'data'})
78
+ assert_equal ['Fish', 'Cat', 'Bunny'], output[:autocomplete]
79
+ refute original.equal?(output)
80
+
81
+ original = Outcasted.fields[:name]
82
+ output = Outcasted.new.outcast(:name, original, {input_name_prefix: 'data'})
83
+ refute output.key?(:autocomplete)
84
+ end
85
+
86
+ it 'Generates the autocomplete options from a proc' do
87
+ original = Outcasted.fields[:category].dup
88
+ original[:autocomplete] = proc{ ['Dog', 'Snake'] }
89
+ output = Outcasted.new.outcast(:category, original, {input_name_prefix: 'data'})
90
+ assert_equal ['Dog', 'Snake'], output[:autocomplete]
91
+ assert original[:autocomplete].is_a?(Proc)
92
+ end
93
+
94
+ it 'Generates the autocomplete options from a method name' do
95
+ original = Outcasted.fields[:category].dup
96
+ original[:autocomplete] = :get_category_autocomplete_list
97
+ output = Outcasted.new.outcast(:category, original, {input_name_prefix: 'data'})
98
+ assert_equal ['Horse', 'Bear'], output[:autocomplete]
99
+ assert original[:autocomplete].is_a?(Symbol)
100
+ end
101
+ end
102
+
68
103
  describe '#outcast_list' do
69
104
 
70
105
  it 'Has no value and an empty list of items when list is empty' do
@@ -1,5 +1,6 @@
1
1
  require 'helper'
2
2
  require 'populate_me/document'
3
+ require 'populate_me/attachment'
3
4
 
4
5
  describe PopulateMe::Document, 'Schema' do
5
6
 
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.13.1
4
+ version: 0.16.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mickael Riga
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-09-28 00:00:00.000000000 Z
11
+ date: 2021-12-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: web-utils
@@ -56,16 +56,16 @@ dependencies:
56
56
  name: bundler
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - "~>"
59
+ - - ">="
60
60
  - !ruby/object:Gem::Version
61
- version: '1.13'
61
+ version: 2.2.10
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - "~>"
66
+ - - ">="
67
67
  - !ruby/object:Gem::Version
68
- version: '1.13'
68
+ version: 2.2.10
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: minitest
71
71
  requirement: !ruby/object:Gem::Requirement
@@ -198,6 +198,7 @@ files:
198
198
  - lib/populate_me/admin/__assets__/css/asmselect.css
199
199
  - lib/populate_me/admin/__assets__/css/jquery-ui.min.css
200
200
  - lib/populate_me/admin/__assets__/css/main.css
201
+ - lib/populate_me/admin/__assets__/img/file.png
201
202
  - lib/populate_me/admin/__assets__/img/help/children.png
202
203
  - lib/populate_me/admin/__assets__/img/help/create.png
203
204
  - lib/populate_me/admin/__assets__/img/help/delete.png
@@ -257,7 +258,7 @@ homepage: https://github.com/mig-hub/populate-me
257
258
  licenses:
258
259
  - MIT
259
260
  metadata: {}
260
- post_install_message:
261
+ post_install_message:
261
262
  rdoc_options: []
262
263
  require_paths:
263
264
  - lib
@@ -272,8 +273,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
272
273
  - !ruby/object:Gem::Version
273
274
  version: '0'
274
275
  requirements: []
275
- rubygems_version: 3.0.3
276
- signing_key:
276
+ rubygems_version: 3.2.16
277
+ signing_key:
277
278
  specification_version: 4
278
279
  summary: PopulateMe is an admin system for web applications.
279
280
  test_files: