emerald-rails 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in emerald-rails.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Alexandre de Oliveira
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Emerald::Rails
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'emerald-rails'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install emerald-rails
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ desc "Downloads the latest stable Javascript file"
4
+ task :stable do
5
+ stable_url = "https://raw.github.com/kurko/emerald.js/master/build/stable.js"
6
+ assets_dir = "app/assets/javascript"
7
+ cmd = "( cd #{assets_dir} > /dev/null ; curl -O #{stable_url} ; )"
8
+ exec cmd
9
+ end
@@ -0,0 +1,292 @@
1
+ var Emerald = {
2
+ Core: {},
3
+ ActionView: {
4
+ Elements: {},
5
+ Events: {}
6
+ }
7
+ };
8
+
9
+
10
+ Emerald.ActionView.extend = function(actions){
11
+ // instance object to be returned
12
+ var instance = new Object;
13
+ var _this = this;
14
+ _this.actions = actions;
15
+
16
+ if (_this.actions.initialize) {
17
+ $(document).ready(function(){
18
+ _this.actions.initialize();
19
+ });
20
+ }
21
+
22
+ // initializes this View's elements
23
+ $(document).ready(function() {
24
+ Emerald.ActionView.Elements.bindToView();
25
+ Emerald.ActionView.Events.bindElementToViewAction(_this.actions);
26
+ });
27
+
28
+ // defines the method of the new view instance
29
+ for (action in actions) {
30
+ instance[action] = actions[action];
31
+ }
32
+
33
+ return instance;
34
+ }
35
+
36
+ Emerald.ActionView.Elements.bindToView = function() {
37
+ $("[data-view-action]").each(function(index, viewElement){
38
+ var elementsView = $(this).closest("[data-view]").data("view");
39
+ viewElement.setAttribute('extendsView', elementsView);
40
+ });
41
+ }
42
+
43
+ Emerald.ActionView.Elements.dataFieldsSelector = function(){
44
+ var selector = "";
45
+ selector+= "input[type='text'], ";
46
+ selector+= "input[type='radio']:checked";
47
+ return selector;
48
+ }
49
+
50
+ Emerald.ActionView.Elements.dataFields = function(){
51
+ var selectors = this.dataFieldsSelector();
52
+ return $(selectors);
53
+ }
54
+
55
+ Emerald.ActionView.Events.bindElementToViewAction = function(viewActions) {
56
+ $('[data-view-action]').each(function(index, viewElement){
57
+ var _event = viewElement.getAttribute('data-event');
58
+ var _viewAction = viewElement.getAttribute('data-view-action');
59
+
60
+ _event = Emerald.ActionView.Events.defineDefaultEvent(viewElement, _event);
61
+
62
+ var dataFieldSelector = "";
63
+ dataFieldSelector+= "input[type='text'][data-view-action='"+_viewAction+"'], ";
64
+ dataFieldSelector+= "input[type='radio'][data-view-action='"+_viewAction+"']:checked";
65
+
66
+ $(this).on(_event, function(e){
67
+ var dataFields = $(dataFieldSelector);
68
+ viewActions[_viewAction](e, dataFields);
69
+ });
70
+ });
71
+ }
72
+
73
+ Emerald.ActionView.Events.defineDefaultEvent = function(domElement, _event) {
74
+ if (_event)
75
+ return _event;
76
+ else
77
+ return 'keyup';
78
+ }
79
+
80
+ function emeraldActionController(){
81
+ this.extend = function(properties){
82
+ var _this = this;
83
+ _this.properties = properties;
84
+
85
+ return new emeraldActionControllerInstance(properties);
86
+ }
87
+ }
88
+
89
+ function emeraldActionControllerInstance(properties){
90
+ var singleton = function() {
91
+ this.self = function(properties){
92
+ this.properties = properties;
93
+
94
+ for (propertyName in properties) {
95
+ if (!this[propertyName])
96
+ this[propertyName] = properties[propertyName];
97
+ }
98
+
99
+ return this;
100
+ }
101
+
102
+ this.params = function(domElements){
103
+ var params = new Object;
104
+
105
+ for (i = 0; i < domElements.length; i++) {
106
+ var element = domElements[i];
107
+ params[element.name] = element.value;
108
+ }
109
+
110
+ return params;
111
+ }
112
+
113
+ this.persistView = true;
114
+
115
+ this.persistViewCallback = function(JSON) {
116
+ debugger;
117
+ if (this.persistView)
118
+ Emerald.modelObserver.update(JSON);
119
+ return true;
120
+ }
121
+ }
122
+
123
+ var instance = new singleton().self(properties);
124
+ return instance;
125
+ }
126
+
127
+ function emeraldActiveModel(){
128
+ this.extend = function(properties){
129
+ var _this = this;
130
+ _this.properties = properties;
131
+
132
+ return new emeraldActiveModelInstance(properties);
133
+ }
134
+ }
135
+
136
+ function emeraldActiveModelInstance(properties){
137
+ var singleton = function() {
138
+ this.self = function(properties){
139
+ this.properties = properties;
140
+ return this;
141
+ }
142
+
143
+ this.attributes = function() {
144
+ attributesValues = {};
145
+ if (this.properties.attrAccessible) {
146
+ var attributes = this.properties.attrAccessible;
147
+ for (attribute in attributes) {
148
+ var attributeName = attributes[attribute];
149
+ if (this[attributeName])
150
+ attributesValues[attributeName] = this[attributeName];
151
+ }
152
+ }
153
+ return attributesValues;
154
+ }
155
+
156
+ this.route = function() { return this.properties.route; }
157
+
158
+ this.get = function(attribute) {
159
+ if (this.properties[attribute])
160
+ return this.properties[attribute];
161
+ else
162
+ return "none";
163
+ }
164
+
165
+ this.save = function(data, andCallbackController) {
166
+ // TODO verify that `data` fields are the ones listed in this.attrAccessible
167
+ debugger;
168
+ var persistence = new emeraldPersistence(this).save(data, andCallbackController);
169
+ return persistence;
170
+ }
171
+ }
172
+
173
+ function createAccessibleAttributes(singleton, attributes) {
174
+ for (attribute in attributes) {
175
+ singleton.prototype[attributes[attribute]] = null;
176
+ }
177
+ }
178
+
179
+ var instance = new singleton().self(properties);
180
+ createAccessibleAttributes(singleton, properties.attrAccessible);
181
+ return instance;
182
+ }
183
+
184
+ // Observes the whole document for [data-observe] elements. Whenever an Ajax
185
+ // call is made, the JSON response should be passed in to
186
+ //
187
+ // observer.update(response)
188
+ //
189
+ // It will update all the elements in the document with the JSON data.
190
+ //
191
+ // Example:
192
+ //
193
+ // JSON: {"item":{"name":"My item", "price":"US$40,00"}}
194
+ //
195
+ // Document:
196
+ //
197
+ // <span data-observe="item.name"></span>
198
+ // <span data-observe="item.price"></span>
199
+ //
200
+ // The first HTML element will be updated with "My item" automatically and
201
+ // the second, price, with "US$40,00".
202
+ //
203
+ function modelObserver(){}
204
+
205
+ Emerald.modelObserver = new modelObserver();
206
+
207
+ modelObserver.prototype.update = function(jsonData){
208
+ $("[data-observe]").each(function(index){
209
+ var observing = $(this).data("observe");
210
+ var observedResources = observing.split(".");
211
+
212
+ var currentValue = jsonData;
213
+ $.each(observedResources, function(index, value){
214
+ if (currentValue[value] || typeof currentValue[value] == "string")
215
+ currentValue = currentValue[value];
216
+ else
217
+ return false;
218
+ });
219
+
220
+ switch (typeof currentValue) {
221
+ case "number":
222
+ case "bool":
223
+ case "string":
224
+ $(this).html(currentValue);
225
+ return true;
226
+ }
227
+
228
+ });
229
+ };
230
+
231
+ function emeraldPersistence(model){
232
+ var singleton = function() {
233
+ this.initialize = function(model, _class) {
234
+ this.model = model;
235
+ this._class = _class;
236
+ return this;
237
+ }
238
+
239
+ this.save = function(data, callbackController){
240
+ return new this._class.PersistenceSave().save(data, callbackController);
241
+ }
242
+ }
243
+
244
+ this.PersistenceSave = function() {
245
+ var singleton = function() {
246
+ this.initialize = function(model, _class) {
247
+ this.model = model;
248
+ this._class = _class;
249
+ return this;
250
+ }
251
+
252
+ this.save = function(data, controller) {
253
+ //var attributes = this.model.attributes();
254
+ var attributes = data;
255
+ var _controller = controller;
256
+
257
+ var requestSpecs = {
258
+ url: this.model.route(),
259
+ data: attributes,
260
+ type: this.requestType(attributes),
261
+ dataType: "json"
262
+ };
263
+
264
+ $.ajax(requestSpecs).done(function(JSON) {
265
+ debugger;
266
+ if (_controller)
267
+ _controller.persistViewCallback(JSON);
268
+ }).fail(function(response) {
269
+ // TODO handle errors
270
+ if (_controller)
271
+ _controller.persistViewCallback(JSON);
272
+ });
273
+
274
+ return requestSpecs;
275
+ }
276
+
277
+ this.requestType = function(attributes) {
278
+ if (attributes["id"])
279
+ return "PUT";
280
+ else
281
+ return "POST";
282
+ }
283
+ }
284
+
285
+ var instance = new singleton().initialize(model, this);
286
+ return instance;
287
+ }
288
+
289
+ var instance = new singleton().initialize(model, this);
290
+ return instance;
291
+ }
292
+
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'emerald-rails/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "emerald-rails"
8
+ gem.version = Emerald::Rails::VERSION
9
+ gem.authors = ["Alexandre de Oliveira"]
10
+ gem.email = ["chavedomundo@gmail.com"]
11
+ gem.description = %q{Brings Emerald.js into your Rails app automagically}
12
+ gem.summary = %q{This gem embed the latest Emerald.js stable version into a Rails app.}
13
+ gem.homepage = "http://github.com/kurko/emerald-rails"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib", "app"]
19
+ gem.add_dependency "railties", "~> 3.1"
20
+ end
@@ -0,0 +1,5 @@
1
+ module Emerald
2
+ module Rails
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,8 @@
1
+ require "emerald-rails/version"
2
+
3
+ module Emerald
4
+ module Rails
5
+ class Engine < ::Rails::Engine
6
+ end
7
+ end
8
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: emerald-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Alexandre de Oliveira
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-21 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: railties
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '3.1'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '3.1'
30
+ description: Brings Emerald.js into your Rails app automagically
31
+ email:
32
+ - chavedomundo@gmail.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - .gitignore
38
+ - Gemfile
39
+ - LICENSE.txt
40
+ - README.md
41
+ - Rakefile
42
+ - app/assets/javascript/stable.js
43
+ - emerald-rails.gemspec
44
+ - lib/emerald-rails.rb
45
+ - lib/emerald-rails/version.rb
46
+ homepage: http://github.com/kurko/emerald-rails
47
+ licenses: []
48
+ post_install_message:
49
+ rdoc_options: []
50
+ require_paths:
51
+ - lib
52
+ - app
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ! '>='
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ! '>='
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ requirements: []
66
+ rubyforge_project:
67
+ rubygems_version: 1.8.23
68
+ signing_key:
69
+ specification_version: 3
70
+ summary: This gem embed the latest Emerald.js stable version into a Rails app.
71
+ test_files: []