active_scaffold_camera 0.1.1 → 0.3.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
  SHA256:
3
- metadata.gz: 3c6ae4c3844e0890fb9ef6cf304fb4275816cbf309770b3d26ee2d3632d5a0cb
4
- data.tar.gz: 883d27c3cabbb3c46f0a6c288339525d707dae46c348598c5440290b7b74debf
3
+ metadata.gz: 8497b20150f3eb5d4fcea8deadbc50b702ce52343dd07059d3622a9a356c9401
4
+ data.tar.gz: b45f7073b101da915f5d9b6c121e50402b8b68c6a4d9998c708a15bdea04289a
5
5
  SHA512:
6
- metadata.gz: 2c89c756e9540d6436d2345d5a6677913108d053eb45e530013ae6e86bef19ac81be59e8697f2f72fbbc56e257a89458908c6182b1605f48be6f0432f54a0547
7
- data.tar.gz: a214ea83c56667b5e1355ab6e5107b8a9c762f785ce8d97600d3cfbd53bca5eb1d92840f6335e86c5cc2651ed13dc4e075cd4af4ae74b82bc52abc812cb986ad
6
+ metadata.gz: ca95676e84b4f6467efa21b5b63f19616065607603ff382f769a3aae3a346211cfbdd195e1128f6f3ccefd48ae47ae948e0c4163bdf58489cf835018005d39e8
7
+ data.tar.gz: 867931f1ff2105a56ef1c5427550161ec2d3963655a14ab1b3baebcd74b4adb9860c3fd6e1511262591b0699432b6c3320d64409fb212dc226e78c257a1addcc
data/README.md ADDED
@@ -0,0 +1,76 @@
1
+ # Camera for ActiveScaffold
2
+
3
+ [![Build status](https://github.com/activescaffold/active_scaffold_camera/actions/workflows/ci.yml/badge.svg)](https://github.com/activescaffold/active_scaffold_camera/actions/workflows/ci.yml)
4
+ [![Maintainability](https://qlty.sh/gh/activescaffold/projects/active_scaffold_camera/maintainability.svg)](https://qlty.sh/gh/activescaffold/projects/active_scaffold_camera)
5
+ [![Code Coverage](https://qlty.sh/gh/activescaffold/projects/active_scaffold_camera/coverage.svg)](https://qlty.sh/gh/activescaffold/projects/active_scaffold_camera)
6
+ [![Gem Version](https://badge.fury.io/rb/active_scaffold_camera.svg)](https://badge.fury.io/rb/active_scaffold_camera)
7
+ [![Inline docs](https://inch-ci.org/github/activescaffold/active_scaffold_camera.svg?branch=master)](https://inch-ci.org/github/activescaffold/active_scaffold_camera)
8
+ [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)
9
+
10
+ Currently, this plugin is compatible with ActiveScaffold >= 3.4 gem.
11
+
12
+ ## Overview
13
+
14
+ This plugin adds a camera viewer to take a snapshot from camera on ActiveScaffold forms. It uses [SayCheese.js](https://github.com/leemachin/say-cheese)
15
+
16
+ ### Browser compatibility
17
+
18
+ The plugin uses the modern `navigator.mediaDevices` API when available, including
19
+ [`enumerateDevices()`](https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/enumerateDevices)
20
+ and `deviceId` constraints. The legacy callback-based API, `MediaStreamTrack.getSources`,
21
+ and `sourceId` constraints remain available as a fallback for older browsers.
22
+
23
+ Camera access requires a secure context (HTTPS or localhost) and the user's permission.
24
+
25
+ ## Installation
26
+
27
+ You'll need at least ActiveScaffold 3.4 to use this
28
+
29
+ ```
30
+ gem install active_scaffold_camera
31
+ ```
32
+
33
+ ## Usage
34
+
35
+ #### Step 1
36
+
37
+ Set column to use :snapshot form_ui
38
+
39
+ #### Step 2
40
+
41
+ Setup snapshot using options method on column
42
+
43
+ ```ruby
44
+ # app/controllers/visitors_controller.rb
45
+
46
+ class VisitorsController < ApplicationController
47
+ active_scaffold :visitor do |config|
48
+ config.columns[:id_card].form_ui = :snapshot
49
+ config.columns[:id_card].options = {:source => -1}
50
+ end
51
+ end
52
+ ```
53
+
54
+ Available options: video_not_supported, audio_not_supported, media_forbidden, source
55
+
56
+ Source can be set to use a specific source instead of displaying source selector. It must be the source index, and it can be negative index to start from end. If no device on that index, first device will be used.
57
+
58
+ If video_not_supported, audio_not_supported or media_forbidden are symbols they will be translated (using as_ method, so it must be on active_scaffold namespace). They have a default translation.
59
+
60
+ #### Save image
61
+
62
+ Snapshot form_ui sends a data url, encoded on base64, of the image, so it should be saved on column of blob type.
63
+
64
+ ## Support
65
+
66
+ If you have issues installing the plugin, search / post to the [Active Scaffold](http://groups.google.com/group/activescaffold) forum or [Create an issue](http://github.com/activescaffold/active_scaffold_camera/issues)
67
+
68
+ ## Contributing
69
+
70
+ Fork, hack, push, and request a pull:
71
+
72
+ http://github.com/activescaffold/active_scaffold_camera/
73
+
74
+ ## License
75
+
76
+ Released under the MIT license (included).
@@ -1,13 +1,38 @@
1
- //= require say-cheese
2
- //= require_self
3
-
4
1
  ActiveScaffold.snapshot = function(selector_or_elements, parent) {
2
+ function modernMediaApi() {
3
+ return navigator.mediaDevices && navigator.mediaDevices.getUserMedia;
4
+ }
5
+
5
6
  function videoConstraint(video) {
6
- return typeof(video) == 'string' ? {optional: [{sourceId: video}]} : video;
7
+ if (typeof(video) != 'string') return video;
8
+ return modernMediaApi() ? {deviceId: {exact: video}} : {optional: [{sourceId: video}]};
7
9
  }
8
10
 
9
11
  function audioConstraint(audio) {
10
- return typeof(audio) == 'string' ? {optional: [{sourceId: audio}]} : audio;
12
+ if (typeof(audio) != 'string') return audio;
13
+ return modernMediaApi() ? {deviceId: {exact: audio}} : {optional: [{sourceId: audio}]};
14
+ }
15
+
16
+ function enumerateVideoSources(callback) {
17
+ if (navigator.mediaDevices && navigator.mediaDevices.enumerateDevices) {
18
+ navigator.mediaDevices.enumerateDevices().then(function(devices) {
19
+ callback(devices.filter(function(device) {
20
+ return device.kind == 'videoinput';
21
+ }).map(function(device) {
22
+ return {id: device.deviceId, label: device.label};
23
+ }));
24
+ }).catch(function() {
25
+ callback([]);
26
+ });
27
+ } else if (window.MediaStreamTrack && MediaStreamTrack.getSources) {
28
+ MediaStreamTrack.getSources(function(sourceInfos) {
29
+ callback(sourceInfos.filter(function(source) {
30
+ return source.kind == 'video';
31
+ }));
32
+ });
33
+ } else {
34
+ callback([]);
35
+ }
11
36
  }
12
37
 
13
38
  function startVideo(element, video, audio, video_select) {
@@ -59,22 +84,15 @@ ActiveScaffold.snapshot = function(selector_or_elements, parent) {
59
84
 
60
85
  elements.each(function() {
61
86
  var $element = $(this), selector = '#' + $element.attr('id');
62
- MediaStreamTrack.getSources(function(sourceInfos) {
63
- var sources = [];
64
- for(var i in sourceInfos) {
65
- var source = sourceInfos[i];
66
- if (source.kind == 'video') {
67
- sources.push(source);
68
- }
69
- }
87
+ enumerateVideoSources(function(sources) {
70
88
  var video, source, select, index = $element.data('source');
71
89
  if (index != null) {
72
90
  if (index < 0) source = sources[sources.length + index];
73
91
  else source = sources[index];
74
92
  if (source) video = source.id;
75
93
  } else if (sources.length > 1) {
76
- var select = $('<select>');
77
- for (i in sources) {
94
+ select = $('<select>');
95
+ for (var i in sources) {
78
96
  source = sources[i];
79
97
  select.append($('<option>').val(source.id).html(source.label || 'camera ' + (i + 1)));
80
98
  }
@@ -16,7 +16,7 @@ var SayCheese = (function() {
16
16
 
17
17
  var SayCheese;
18
18
 
19
- navigator.getUserMedia = (navigator.getUserMedia ||
19
+ var legacyGetUserMedia = (navigator.getUserMedia ||
20
20
  navigator.webkitGetUserMedia ||
21
21
  navigator.mozGetUserMedia ||
22
22
  navigator.msGetUserMedia ||
@@ -89,6 +89,8 @@ var SayCheese = (function() {
89
89
  streaming = false;
90
90
 
91
91
  this.video = document.createElement('video');
92
+ this.video.autoplay = true;
93
+ this.video.playsInline = true;
92
94
 
93
95
  this.video.addEventListener('canplay', function() {
94
96
  if (!streaming) {
@@ -137,7 +139,7 @@ var SayCheese = (function() {
137
139
  SayCheese.prototype.start = function start() {
138
140
 
139
141
  // fail fast and softly if browser not supported
140
- if (navigator.getUserMedia === false) {
142
+ if ((!navigator.mediaDevices || !navigator.mediaDevices.getUserMedia) && legacyGetUserMedia === false) {
141
143
  this.trigger('error', 'NOT_SUPPORTED');
142
144
  return false;
143
145
  }
@@ -146,7 +148,9 @@ var SayCheese = (function() {
146
148
  this.stream = stream;
147
149
  this.createVideo();
148
150
 
149
- if (navigator.mozGetUserMedia) {
151
+ if ('srcObject' in this.video) {
152
+ this.video.srcObject = stream;
153
+ } else if (navigator.mozGetUserMedia) {
150
154
  this.video.mozSrcObject = stream;
151
155
  } else {
152
156
  this.video.src = this.getStreamUrl();
@@ -169,16 +173,36 @@ var SayCheese = (function() {
169
173
  this.trigger('error', error);
170
174
  }.bind(this);
171
175
 
172
- return navigator.getUserMedia({ video: this.options.video, audio: this.options.audio }, success, error);
176
+ var constraints = { video: this.options.video, audio: this.options.audio };
177
+
178
+ if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
179
+ return navigator.mediaDevices.getUserMedia(constraints).then(success).catch(error);
180
+ }
181
+
182
+ return legacyGetUserMedia.call(navigator, constraints, success, error);
173
183
  };
174
184
 
175
185
  SayCheese.prototype.stop = function stop() {
176
- this.stream.stop();
186
+ if (!this.stream) {
187
+ return false;
188
+ }
189
+
190
+ if (this.stream.getTracks) {
191
+ this.stream.getTracks().forEach(function(track) {
192
+ track.stop();
193
+ });
194
+ } else if (this.stream.stop) {
195
+ this.stream.stop();
196
+ }
177
197
 
178
- if (window.URL && window.URL.revokeObjectURL) {
198
+ if (this.video && 'srcObject' in this.video) {
199
+ this.video.srcObject = null;
200
+ } else if (this.video && window.URL && window.URL.revokeObjectURL && this.video.src) {
179
201
  window.URL.revokeObjectURL(this.video.src);
180
202
  }
181
203
 
204
+ this.stream = null;
205
+
182
206
  return this.trigger('stop');
183
207
  };
184
208
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveScaffoldCamera
2
4
  class Engine < ::Rails::Engine
3
5
  initializer 'active_scaffold_camera.action_view' do
@@ -1,8 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveScaffoldCamera
2
4
  module Version
3
5
  MAJOR = 0
4
- MINOR = 1
5
- PATCH = 1
6
+ MINOR = 3
7
+ PATCH = 0
6
8
 
7
9
  STRING = [MAJOR, MINOR, PATCH].compact.join('.')
8
10
  end
@@ -1,22 +1,25 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveScaffoldCamera
2
4
  module ViewHelpers
3
- I18N_ATTRIBUTES = [:video_not_supported, :audio_not_supported, :media_forbidden]
5
+ I18N_ATTRIBUTES = %i[video_not_supported audio_not_supported media_forbidden].freeze
4
6
  def snapshot_attributes(column, ui_options: column.options)
5
- attributes = Hash[I18N_ATTRIBUTES.map{ |attr| [attr, ui_options[attr] || attr] }]
7
+ attributes = I18N_ATTRIBUTES.index_with { |attr| ui_options[attr] || attr }
6
8
  attributes[:source] = ui_options[:source] if ui_options[:source]
7
9
  I18N_ATTRIBUTES.each { |attr| attributes[attr] = as_(attributes[attr]) if attributes[attr].is_a? Symbol }
8
10
  attributes
9
11
  end
10
12
 
11
13
  def active_scaffold_input_snapshot(column, html_options, ui_options: column.options)
12
- content_tag :div, :class => "snapshot-input #{html_options[:class]}", :id => html_options[:id], :data => snapshot_attributes(column, ui_options: ui_options) do
13
- hidden_field :record, column.name, :name => html_options[:name]
14
+ content_tag :div, class: "snapshot-input #{html_options[:class]}", id: html_options[:id],
15
+ data: snapshot_attributes(column, ui_options: ui_options) do
16
+ hidden_field :record, column.name, name: html_options[:name]
14
17
  end
15
18
  end
16
19
 
17
20
  def active_scaffold_column_snapshot(record, column, ui_options: column.options)
18
21
  value = record.send(column.name)
19
- tag :img, :src => value if value
22
+ tag.img(src: value) if value
20
23
  end
21
24
  end
22
25
  end
@@ -1,11 +1,13 @@
1
- require "active_scaffold_camera/engine.rb"
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_scaffold_camera/engine'
2
4
 
3
5
  module ActiveScaffoldCamera
4
6
  def self.root
5
- File.dirname(__FILE__) + "/.."
7
+ "#{File.dirname(__FILE__)}/.."
6
8
  end
7
- autoload 'ViewHelpers', 'active_scaffold_camera/view_helpers.rb'
9
+ autoload :ViewHelpers, 'active_scaffold_camera/view_helpers.rb'
8
10
  end
9
11
 
10
12
  ActiveScaffold.stylesheets << 'active_scaffold_camera'
11
- ActiveScaffold.javascripts << 'active_scaffold_camera'
13
+ ActiveScaffold.javascripts.push('say-cheese', 'active_scaffold_camera')
metadata CHANGED
@@ -1,30 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_scaffold_camera
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sergio Cambra
8
8
  - Volker Hochstein
9
- autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2024-04-29 00:00:00.000000000 Z
11
+ date: 1980-01-02 00:00:00.000000000 Z
13
12
  dependencies:
14
- - !ruby/object:Gem::Dependency
15
- name: bundler
16
- requirement: !ruby/object:Gem::Requirement
17
- requirements:
18
- - - "~>"
19
- - !ruby/object:Gem::Version
20
- version: '2.0'
21
- type: :development
22
- prerelease: false
23
- version_requirements: !ruby/object:Gem::Requirement
24
- requirements:
25
- - - "~>"
26
- - !ruby/object:Gem::Version
27
- version: '2.0'
28
13
  - !ruby/object:Gem::Dependency
29
14
  name: active_scaffold
30
15
  requirement: !ruby/object:Gem::Requirement
@@ -46,10 +31,10 @@ executables: []
46
31
  extensions: []
47
32
  extra_rdoc_files:
48
33
  - LICENSE
49
- - README.textile
34
+ - README.md
50
35
  files:
51
36
  - LICENSE
52
- - README.textile
37
+ - README.md
53
38
  - app/assets/javascripts/active_scaffold_camera.js
54
39
  - app/assets/javascripts/say-cheese.js
55
40
  - app/assets/stylesheets/active_scaffold_camera.scss
@@ -62,8 +47,8 @@ files:
62
47
  homepage: http://github.com/activescaffold/active_scaffold_camera
63
48
  licenses:
64
49
  - MIT
65
- metadata: {}
66
- post_install_message:
50
+ metadata:
51
+ rubygems_mfa_required: 'true'
67
52
  rdoc_options: []
68
53
  require_paths:
69
54
  - lib
@@ -71,15 +56,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
71
56
  requirements:
72
57
  - - ">="
73
58
  - !ruby/object:Gem::Version
74
- version: '0'
59
+ version: '3.2'
75
60
  required_rubygems_version: !ruby/object:Gem::Requirement
76
61
  requirements:
77
62
  - - ">="
78
63
  - !ruby/object:Gem::Version
79
64
  version: '0'
80
65
  requirements: []
81
- rubygems_version: 3.2.3
82
- signing_key:
66
+ rubygems_version: 3.6.9
83
67
  specification_version: 4
84
68
  summary: Snapshot form_ui using saycheese.js
85
69
  test_files: []
data/README.textile DELETED
@@ -1,60 +0,0 @@
1
- h1. Camera for ActiveScaffold
2
-
3
- Currently, this plugin is compatible with ActiveScaffold >= 3.4 gem.
4
-
5
- h2. Overview
6
-
7
- This plugin adds a camera viewer to take a snapshot from camera on ActiveScaffold forms. It uses "SayCheese.js":https://github.com/leemachin/say-cheese
8
-
9
- h2. Installation
10
-
11
- You'll need at least ActiveScaffold 3.4 to use this
12
-
13
- <pre>
14
- gem install active_scaffold_camera
15
- </pre>
16
-
17
- h2. Usage
18
-
19
- h4. Step 1
20
-
21
- Set column to use :snapshot form_ui
22
-
23
- h4. Step 2
24
-
25
- Setup snapshot using options method on column
26
-
27
- <pre>
28
- # app/controllers/visitors_controller.rb
29
-
30
- class VisitorsController < ApplicationController
31
- active_scaffold :visitor do |config|
32
- config.columns[:signature].form_ui = :signaturepad
33
- config.columns[:signature].options = {:source => -1}
34
- end
35
- end
36
- </pre>
37
-
38
- Available options: video_not_supported, audio_not_supported, media_forbidden, source
39
-
40
- Source can be set to use a specific source instead of displaying source selector. It must be the source index, and it can be negative index to start from end. If no device on that index, first device will be used.
41
-
42
- If video_not_supported, audio_not_supported or media_forbidden are symbols they will be translated (using as_ method, so it must be on active_scaffold namespace). They have a default translation.
43
-
44
- h4. Save image
45
-
46
- Snapshot form_ui sends a data url, encoded on base64, of the image, so it should be saved on column of blob type.
47
-
48
- h2. Support
49
-
50
- If you have issues installing the plugin, search / post to the "Active Scaffold":http://groups.google.com/group/activescaffold forum or "Create an issue":http://github.com/activescaffold/active_scaffold_camera/issues
51
-
52
- h2. Contributing
53
-
54
- Fork, hack, push, and request a pull:
55
-
56
- http://github.com/activescaffold/active_scaffold_camera/
57
-
58
- h2. License
59
-
60
- Released under the MIT license (included).