react_editable_content 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (70) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +18 -0
  3. data/.travis.yml +7 -0
  4. data/Gemfile +20 -0
  5. data/Gemfile.lock +129 -0
  6. data/Rakefile +20 -0
  7. data/Readme.md +44 -0
  8. data/app/assets/images/icon_edit.png +0 -0
  9. data/app/assets/javascripts/mercury.js +463 -0
  10. data/app/assets/javascripts/mercury/uploader.js.coffee +248 -0
  11. data/app/assets/stylesheets/mercury.css +23 -0
  12. data/app/controller/editable_content/editables_controller.rb +21 -0
  13. data/app/controller/mercury/images_controller.rb +24 -0
  14. data/app/helpers/editable_content/application_helper.rb +39 -0
  15. data/app/models/editable_content/editable.rb +6 -0
  16. data/app/models/mercury/image.rb +21 -0
  17. data/app/views/editable_content/_edit_link.html.erb +3 -0
  18. data/app/views/layouts/mercury.html.erb +23 -0
  19. data/config/routes.rb +9 -0
  20. data/lib/editable_content/engine.rb +17 -0
  21. data/lib/editable_content/railtie.rb +10 -0
  22. data/lib/generators/editable_content/install/install_generator.rb +22 -0
  23. data/lib/generators/editable_content/install/templates/create_editable_content_editables.rb +11 -0
  24. data/lib/generators/editable_content/install/templates/create_mercury_images.rb +8 -0
  25. data/lib/generators/editable_content/install/templates/lib/mercury/authentication.rb +9 -0
  26. data/lib/mercury/authentication.rb +9 -0
  27. data/lib/react_editable_content.rb +6 -0
  28. data/react_editable_content.gemspec +25 -0
  29. data/test/controllers/editable_content/editable_controller_test.rb +37 -0
  30. data/test/dummy/Rakefile +7 -0
  31. data/test/dummy/app/controllers/application_controller.rb +3 -0
  32. data/test/dummy/app/helpers/application_helper.rb +2 -0
  33. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  34. data/test/dummy/config.ru +4 -0
  35. data/test/dummy/config/application.rb +44 -0
  36. data/test/dummy/config/boot.rb +10 -0
  37. data/test/dummy/config/database.yml +22 -0
  38. data/test/dummy/config/environment.rb +5 -0
  39. data/test/dummy/config/environments/development.rb +25 -0
  40. data/test/dummy/config/environments/production.rb +49 -0
  41. data/test/dummy/config/environments/test.rb +35 -0
  42. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  43. data/test/dummy/config/initializers/inflections.rb +10 -0
  44. data/test/dummy/config/initializers/mime_types.rb +5 -0
  45. data/test/dummy/config/initializers/secret_token.rb +7 -0
  46. data/test/dummy/config/initializers/session_store.rb +8 -0
  47. data/test/dummy/config/locales/en.yml +5 -0
  48. data/test/dummy/config/routes.rb +58 -0
  49. data/test/dummy/db/migrate/20140908144759_create_editable_content_editables.rb +11 -0
  50. data/test/dummy/db/migrate/20140908144760_create_mercury_images.rb +8 -0
  51. data/test/dummy/db/schema.rb +33 -0
  52. data/test/dummy/db/test.sqlite3 +0 -0
  53. data/test/dummy/lib/mercury/authentication.rb +9 -0
  54. data/test/dummy/public/404.html +26 -0
  55. data/test/dummy/public/422.html +26 -0
  56. data/test/dummy/public/500.html +26 -0
  57. data/test/dummy/public/favicon.ico +0 -0
  58. data/test/dummy/public/javascripts/application.js +2 -0
  59. data/test/dummy/public/javascripts/controls.js +965 -0
  60. data/test/dummy/public/javascripts/dragdrop.js +974 -0
  61. data/test/dummy/public/javascripts/effects.js +1123 -0
  62. data/test/dummy/public/javascripts/prototype.js +6001 -0
  63. data/test/dummy/public/javascripts/rails.js +202 -0
  64. data/test/dummy/public/stylesheets/.gitkeep +0 -0
  65. data/test/dummy/script/rails +6 -0
  66. data/test/dummy/test/fixtures/editables.yml +9 -0
  67. data/test/dummy/test/fixtures/rails.png +0 -0
  68. data/test/helpers/editables_helper_test.rb +78 -0
  69. data/test/test_helper.rb +27 -0
  70. metadata +167 -0
@@ -0,0 +1,202 @@
1
+ (function() {
2
+ Ajax.Responders.register({
3
+ onCreate: function(request) {
4
+ var token = $$('meta[name=csrf-token]')[0];
5
+ if (token) {
6
+ if (!request.options.requestHeaders) request.options.requestHeaders = {};
7
+ request.options.requestHeaders['X-CSRF-Token'] = token.readAttribute('content');
8
+ }
9
+ }
10
+ });
11
+
12
+ // Technique from Juriy Zaytsev
13
+ // http://thinkweb2.com/projects/prototype/detecting-event-support-without-browser-sniffing/
14
+ function isEventSupported(eventName) {
15
+ var el = document.createElement('div');
16
+ eventName = 'on' + eventName;
17
+ var isSupported = (eventName in el);
18
+ if (!isSupported) {
19
+ el.setAttribute(eventName, 'return;');
20
+ isSupported = typeof el[eventName] == 'function';
21
+ }
22
+ el = null;
23
+ return isSupported;
24
+ }
25
+
26
+ function isForm(element) {
27
+ return Object.isElement(element) && element.nodeName.toUpperCase() == 'FORM';
28
+ }
29
+
30
+ function isInput(element) {
31
+ if (Object.isElement(element)) {
32
+ var name = element.nodeName.toUpperCase();
33
+ return name == 'INPUT' || name == 'SELECT' || name == 'TEXTAREA';
34
+ }
35
+ else return false;
36
+ }
37
+
38
+ var submitBubbles = isEventSupported('submit'),
39
+ changeBubbles = isEventSupported('change');
40
+
41
+ if (!submitBubbles || !changeBubbles) {
42
+ // augment the Event.Handler class to observe custom events when needed
43
+ Event.Handler.prototype.initialize = Event.Handler.prototype.initialize.wrap(
44
+ function(init, element, eventName, selector, callback) {
45
+ init(element, eventName, selector, callback);
46
+ // is the handler being attached to an element that doesn't support this event?
47
+ if ( (!submitBubbles && this.eventName == 'submit' && !isForm(this.element)) ||
48
+ (!changeBubbles && this.eventName == 'change' && !isInput(this.element)) ) {
49
+ // "submit" => "emulated:submit"
50
+ this.eventName = 'emulated:' + this.eventName;
51
+ }
52
+ }
53
+ );
54
+ }
55
+
56
+ if (!submitBubbles) {
57
+ // discover forms on the page by observing focus events which always bubble
58
+ document.on('focusin', 'form', function(focusEvent, form) {
59
+ // special handler for the real "submit" event (one-time operation)
60
+ if (!form.retrieve('emulated:submit')) {
61
+ form.on('submit', function(submitEvent) {
62
+ var emulated = form.fire('emulated:submit', submitEvent, true);
63
+ // if custom event received preventDefault, cancel the real one too
64
+ if (emulated.returnValue === false) submitEvent.preventDefault();
65
+ });
66
+ form.store('emulated:submit', true);
67
+ }
68
+ });
69
+ }
70
+
71
+ if (!changeBubbles) {
72
+ // discover form inputs on the page
73
+ document.on('focusin', 'input, select, textarea', function(focusEvent, input) {
74
+ // special handler for real "change" events
75
+ if (!input.retrieve('emulated:change')) {
76
+ input.on('change', function(changeEvent) {
77
+ input.fire('emulated:change', changeEvent, true);
78
+ });
79
+ input.store('emulated:change', true);
80
+ }
81
+ });
82
+ }
83
+
84
+ function handleRemote(element) {
85
+ var method, url, params;
86
+
87
+ var event = element.fire("ajax:before");
88
+ if (event.stopped) return false;
89
+
90
+ if (element.tagName.toLowerCase() === 'form') {
91
+ method = element.readAttribute('method') || 'post';
92
+ url = element.readAttribute('action');
93
+ // serialize the form with respect to the submit button that was pressed
94
+ params = element.serialize({ submit: element.retrieve('rails:submit-button') });
95
+ // clear the pressed submit button information
96
+ element.store('rails:submit-button', null);
97
+ } else {
98
+ method = element.readAttribute('data-method') || 'get';
99
+ url = element.readAttribute('href');
100
+ params = {};
101
+ }
102
+
103
+ new Ajax.Request(url, {
104
+ method: method,
105
+ parameters: params,
106
+ evalScripts: true,
107
+
108
+ onCreate: function(response) { element.fire("ajax:create", response); },
109
+ onComplete: function(response) { element.fire("ajax:complete", response); },
110
+ onSuccess: function(response) { element.fire("ajax:success", response); },
111
+ onFailure: function(response) { element.fire("ajax:failure", response); }
112
+ });
113
+
114
+ element.fire("ajax:after");
115
+ }
116
+
117
+ function insertHiddenField(form, name, value) {
118
+ form.insert(new Element('input', { type: 'hidden', name: name, value: value }));
119
+ }
120
+
121
+ function handleMethod(element) {
122
+ var method = element.readAttribute('data-method'),
123
+ url = element.readAttribute('href'),
124
+ csrf_param = $$('meta[name=csrf-param]')[0],
125
+ csrf_token = $$('meta[name=csrf-token]')[0];
126
+
127
+ var form = new Element('form', { method: "POST", action: url, style: "display: none;" });
128
+ $(element.parentNode).insert(form);
129
+
130
+ if (method !== 'post') {
131
+ insertHiddenField(form, '_method', method);
132
+ }
133
+
134
+ if (csrf_param) {
135
+ insertHiddenField(form, csrf_param.readAttribute('content'), csrf_token.readAttribute('content'));
136
+ }
137
+
138
+ form.submit();
139
+ }
140
+
141
+ function disableFormElements(form) {
142
+ form.select('input[type=submit][data-disable-with]').each(function(input) {
143
+ input.store('rails:original-value', input.getValue());
144
+ input.setValue(input.readAttribute('data-disable-with')).disable();
145
+ });
146
+ }
147
+
148
+ function enableFormElements(form) {
149
+ form.select('input[type=submit][data-disable-with]').each(function(input) {
150
+ input.setValue(input.retrieve('rails:original-value')).enable();
151
+ });
152
+ }
153
+
154
+ function allowAction(element) {
155
+ var message = element.readAttribute('data-confirm');
156
+ return !message || confirm(message);
157
+ }
158
+
159
+ document.on('click', 'a[data-confirm], a[data-remote], a[data-method]', function(event, link) {
160
+ if (!allowAction(link)) {
161
+ event.stop();
162
+ return false;
163
+ }
164
+
165
+ if (link.readAttribute('data-remote')) {
166
+ handleRemote(link);
167
+ event.stop();
168
+ } else if (link.readAttribute('data-method')) {
169
+ handleMethod(link);
170
+ event.stop();
171
+ }
172
+ });
173
+
174
+ document.on("click", "form input[type=submit], form button[type=submit], form button:not([type])", function(event, button) {
175
+ // register the pressed submit button
176
+ event.findElement('form').store('rails:submit-button', button.name || false);
177
+ });
178
+
179
+ document.on("submit", function(event) {
180
+ var form = event.findElement();
181
+
182
+ if (!allowAction(form)) {
183
+ event.stop();
184
+ return false;
185
+ }
186
+
187
+ if (form.readAttribute('data-remote')) {
188
+ handleRemote(form);
189
+ event.stop();
190
+ } else {
191
+ disableFormElements(form);
192
+ }
193
+ });
194
+
195
+ document.on('ajax:create', 'form', function(event, form) {
196
+ if (form == event.findElement()) disableFormElements(form);
197
+ });
198
+
199
+ document.on('ajax:complete', 'form', function(event, form) {
200
+ if (form == event.findElement()) enableFormElements(form);
201
+ });
202
+ })();
File without changes
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ # This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
3
+
4
+ APP_PATH = File.expand_path('../../config/application', __FILE__)
5
+ require File.expand_path('../../config/boot', __FILE__)
6
+ require 'rails/commands'
@@ -0,0 +1,9 @@
1
+ # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html
2
+
3
+ one:
4
+ key: MyString
5
+ text: MyText
6
+
7
+ two:
8
+ key: MyString
9
+ text: MyText
@@ -0,0 +1,78 @@
1
+ # encoding: utf-8
2
+ require 'test_helper'
3
+
4
+ class EditablesHelperTest < ActionView::TestCase
5
+ include EditableContent::ApplicationHelper
6
+
7
+ fixtures :editables
8
+ set_fixture_class :editables => EditableContent::Editable
9
+
10
+ setup do
11
+ self.stubs(:can_edit?).returns(true)
12
+ request = mock('request')
13
+ request.stubs(:url).returns('/test')
14
+ self.stubs(:request).returns(request)
15
+ end
16
+
17
+ # Editable Text
18
+ test "should create new editable for new key, and use the existent for existent ones" do
19
+ assert_difference('EditableContent::Editable.count') do
20
+ a = editable_content do
21
+ "test 1"
22
+ end
23
+ end
24
+ assert_difference('EditableContent::Editable.count', 0) do
25
+ editable_content do
26
+ "test 1"
27
+ end
28
+ end
29
+ assert_difference('EditableContent::Editable.count') do
30
+ editable_content('test 2') do
31
+ "test 2"
32
+ end
33
+ end
34
+ assert_difference('EditableContent::Editable.count', 0) do
35
+ editable_content('test 2') do
36
+ "test 2"
37
+ end
38
+ end
39
+ end
40
+
41
+ test "should render only text for not allowed users" do
42
+ self.stubs(:can_edit?).returns(false)
43
+ editable = editable_content do
44
+ "text test"
45
+ end
46
+ assert_equal "text test", editable
47
+ end
48
+
49
+ test "should render editable content to alowed logged users" do
50
+ editable = editable_content do
51
+ "text test"
52
+ end
53
+ assert_match /data-mercury/, editable
54
+ end
55
+
56
+ # Editable Images
57
+ test "editable_image_tag without size should raise an error" do
58
+ assert_raises(RuntimeError) { editable_image_tag "rails.png" }
59
+ end
60
+
61
+ test "should create new editable image for new key, and use the existent for existent ones" do
62
+ assert_difference('EditableContent::Editable.count') do
63
+ editable_image_tag "test1.png", :size => "50x50"
64
+ end
65
+ assert_difference('EditableContent::Editable.count', 0) do
66
+ editable_image_tag "test1.png", :size => "50x50"
67
+ end
68
+ end
69
+
70
+ test "should render only img for not allowed users" do
71
+ self.stubs(:can_edit?).returns(false)
72
+ assert_equal "<img alt=\"Rails\" height=\"50\" src=\"/images/rails.png\" width=\"50\" />", editable_image_tag("rails.png", :size => "50x50")
73
+ end
74
+
75
+ test "should render editable image to allowed users" do
76
+ assert_match /data-mercury="image"/, editable_image_tag("rails.png", :size => "50x50")
77
+ end
78
+ end
@@ -0,0 +1,27 @@
1
+ # Configure Rails Envinronment
2
+ ENV["RAILS_ENV"] = "test"
3
+
4
+ require 'coveralls'
5
+ Coveralls.wear!
6
+
7
+ require File.expand_path("../dummy/config/environment.rb", __FILE__)
8
+ require "rails/test_help"
9
+
10
+ Rails.backtrace_cleaner.remove_silencers!
11
+
12
+ # Run any available migration
13
+ ActiveRecord::Migrator.migrate File.expand_path("../dummy/db/migrate/", __FILE__)
14
+
15
+ # Load support files
16
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
17
+
18
+ class ActiveSupport::TestCase
19
+ # Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order.
20
+ #
21
+ # Note: You'll currently still have to declare fixtures explicitly in integration tests
22
+ # -- they do not yet inherit this setting
23
+ fixtures :all
24
+ def sample_file(filename = "rails.png")
25
+ File.new("../fixtures/#{filename}")
26
+ end
27
+ end
metadata ADDED
@@ -0,0 +1,167 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: react_editable_content
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Rogério Chaves
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-09-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: paperclip
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 4.1.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 4.1.1
27
+ description: Change text and images from the browser
28
+ email:
29
+ - rogerio@react.ag
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".gitignore"
35
+ - ".travis.yml"
36
+ - Gemfile
37
+ - Gemfile.lock
38
+ - Rakefile
39
+ - Readme.md
40
+ - app/assets/images/icon_edit.png
41
+ - app/assets/javascripts/mercury.js
42
+ - app/assets/javascripts/mercury/uploader.js.coffee
43
+ - app/assets/stylesheets/mercury.css
44
+ - app/controller/editable_content/editables_controller.rb
45
+ - app/controller/mercury/images_controller.rb
46
+ - app/helpers/editable_content/application_helper.rb
47
+ - app/models/editable_content/editable.rb
48
+ - app/models/mercury/image.rb
49
+ - app/views/editable_content/_edit_link.html.erb
50
+ - app/views/layouts/mercury.html.erb
51
+ - config/routes.rb
52
+ - lib/editable_content/engine.rb
53
+ - lib/editable_content/railtie.rb
54
+ - lib/generators/editable_content/install/install_generator.rb
55
+ - lib/generators/editable_content/install/templates/create_editable_content_editables.rb
56
+ - lib/generators/editable_content/install/templates/create_mercury_images.rb
57
+ - lib/generators/editable_content/install/templates/lib/mercury/authentication.rb
58
+ - lib/mercury/authentication.rb
59
+ - lib/react_editable_content.rb
60
+ - react_editable_content.gemspec
61
+ - test/controllers/editable_content/editable_controller_test.rb
62
+ - test/dummy/Rakefile
63
+ - test/dummy/app/controllers/application_controller.rb
64
+ - test/dummy/app/helpers/application_helper.rb
65
+ - test/dummy/app/views/layouts/application.html.erb
66
+ - test/dummy/config.ru
67
+ - test/dummy/config/application.rb
68
+ - test/dummy/config/boot.rb
69
+ - test/dummy/config/database.yml
70
+ - test/dummy/config/environment.rb
71
+ - test/dummy/config/environments/development.rb
72
+ - test/dummy/config/environments/production.rb
73
+ - test/dummy/config/environments/test.rb
74
+ - test/dummy/config/initializers/backtrace_silencers.rb
75
+ - test/dummy/config/initializers/inflections.rb
76
+ - test/dummy/config/initializers/mime_types.rb
77
+ - test/dummy/config/initializers/secret_token.rb
78
+ - test/dummy/config/initializers/session_store.rb
79
+ - test/dummy/config/locales/en.yml
80
+ - test/dummy/config/routes.rb
81
+ - test/dummy/db/migrate/20140908144759_create_editable_content_editables.rb
82
+ - test/dummy/db/migrate/20140908144760_create_mercury_images.rb
83
+ - test/dummy/db/schema.rb
84
+ - test/dummy/db/test.sqlite3
85
+ - test/dummy/lib/mercury/authentication.rb
86
+ - test/dummy/public/404.html
87
+ - test/dummy/public/422.html
88
+ - test/dummy/public/500.html
89
+ - test/dummy/public/favicon.ico
90
+ - test/dummy/public/javascripts/application.js
91
+ - test/dummy/public/javascripts/controls.js
92
+ - test/dummy/public/javascripts/dragdrop.js
93
+ - test/dummy/public/javascripts/effects.js
94
+ - test/dummy/public/javascripts/prototype.js
95
+ - test/dummy/public/javascripts/rails.js
96
+ - test/dummy/public/stylesheets/.gitkeep
97
+ - test/dummy/script/rails
98
+ - test/dummy/test/fixtures/editables.yml
99
+ - test/dummy/test/fixtures/rails.png
100
+ - test/helpers/editables_helper_test.rb
101
+ - test/test_helper.rb
102
+ homepage: https://github.com/rogeriochaves/react_editable_content
103
+ licenses:
104
+ - MIT
105
+ metadata: {}
106
+ post_install_message:
107
+ rdoc_options: []
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: 1.9.3
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ requirements: []
121
+ rubyforge_project:
122
+ rubygems_version: 2.4.1
123
+ signing_key:
124
+ specification_version: 4
125
+ summary: Easily edit static content from the front-end
126
+ test_files:
127
+ - test/controllers/editable_content/editable_controller_test.rb
128
+ - test/dummy/Rakefile
129
+ - test/dummy/app/controllers/application_controller.rb
130
+ - test/dummy/app/helpers/application_helper.rb
131
+ - test/dummy/app/views/layouts/application.html.erb
132
+ - test/dummy/config.ru
133
+ - test/dummy/config/application.rb
134
+ - test/dummy/config/boot.rb
135
+ - test/dummy/config/database.yml
136
+ - test/dummy/config/environment.rb
137
+ - test/dummy/config/environments/development.rb
138
+ - test/dummy/config/environments/production.rb
139
+ - test/dummy/config/environments/test.rb
140
+ - test/dummy/config/initializers/backtrace_silencers.rb
141
+ - test/dummy/config/initializers/inflections.rb
142
+ - test/dummy/config/initializers/mime_types.rb
143
+ - test/dummy/config/initializers/secret_token.rb
144
+ - test/dummy/config/initializers/session_store.rb
145
+ - test/dummy/config/locales/en.yml
146
+ - test/dummy/config/routes.rb
147
+ - test/dummy/db/migrate/20140908144759_create_editable_content_editables.rb
148
+ - test/dummy/db/migrate/20140908144760_create_mercury_images.rb
149
+ - test/dummy/db/schema.rb
150
+ - test/dummy/db/test.sqlite3
151
+ - test/dummy/lib/mercury/authentication.rb
152
+ - test/dummy/public/404.html
153
+ - test/dummy/public/422.html
154
+ - test/dummy/public/500.html
155
+ - test/dummy/public/favicon.ico
156
+ - test/dummy/public/javascripts/application.js
157
+ - test/dummy/public/javascripts/controls.js
158
+ - test/dummy/public/javascripts/dragdrop.js
159
+ - test/dummy/public/javascripts/effects.js
160
+ - test/dummy/public/javascripts/prototype.js
161
+ - test/dummy/public/javascripts/rails.js
162
+ - test/dummy/public/stylesheets/.gitkeep
163
+ - test/dummy/script/rails
164
+ - test/dummy/test/fixtures/editables.yml
165
+ - test/dummy/test/fixtures/rails.png
166
+ - test/helpers/editables_helper_test.rb
167
+ - test/test_helper.rb