emerald-rails 0.0.2 → 0.0.3
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.
- data/README.md +13 -5
- data/Rakefile +5 -1
- data/app/assets/javascript/{stable.js → emerald.js} +0 -0
- data/emerald-rails.gemspec +1 -1
- data/lib/emerald-rails/version.rb +2 -4
- data/lib/emerald-rails.rb +2 -4
- data/lib/generators/emerald/install/USAGE +9 -0
- data/lib/generators/emerald/install/install_generator.rb +11 -0
- data/lib/generators/emerald/install/templates/emerald.js +308 -0
- metadata +5 -2
data/README.md
CHANGED
@@ -1,6 +1,8 @@
|
|
1
|
-
#
|
1
|
+
# emerald-rails gem
|
2
2
|
|
3
|
-
|
3
|
+
This gem integrates
|
4
|
+
[Emerald.js](https://github.com/kurko/emerald.js)
|
5
|
+
with your Rails application automagically.
|
4
6
|
|
5
7
|
## Installation
|
6
8
|
|
@@ -12,13 +14,15 @@ And then execute:
|
|
12
14
|
|
13
15
|
$ bundle
|
14
16
|
|
15
|
-
|
17
|
+
Then add this line to your Javascript manifest file (`application.js`)
|
18
|
+
before your own scripts:
|
16
19
|
|
17
|
-
|
20
|
+
//= require emerald
|
18
21
|
|
19
22
|
## Usage
|
20
23
|
|
21
|
-
|
24
|
+
For instructions on how to use Emerald.js, visit its page,
|
25
|
+
[https://github.com/kurko/emerald.js](https://github.com/kurko/emerald.js).
|
22
26
|
|
23
27
|
## Contributing
|
24
28
|
|
@@ -27,3 +31,7 @@ TODO: Write usage instructions here
|
|
27
31
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
32
|
4. Push to the branch (`git push origin my-new-feature`)
|
29
33
|
5. Create new Pull Request
|
34
|
+
|
35
|
+
##License
|
36
|
+
|
37
|
+
This software is released under the MIT License.
|
data/Rakefile
CHANGED
@@ -4,6 +4,10 @@ desc "Downloads the latest stable Javascript file"
|
|
4
4
|
task :stable do
|
5
5
|
stable_url = "https://raw.github.com/kurko/emerald.js/master/build/stable.js"
|
6
6
|
assets_dir = "app/assets/javascript"
|
7
|
-
|
7
|
+
|
8
|
+
cmd = "cd #{assets_dir}"
|
9
|
+
cmd << " && curl -O #{stable_url}"
|
10
|
+
cmd << " && mv stable.js emerald.js"
|
11
|
+
cmd << " && cp emerald.js ../../../lib/generators/emerald/install/templates/emerald.js"
|
8
12
|
exec cmd
|
9
13
|
end
|
File without changes
|
data/emerald-rails.gemspec
CHANGED
@@ -5,7 +5,7 @@ require 'emerald-rails/version'
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |gem|
|
7
7
|
gem.name = "emerald-rails"
|
8
|
-
gem.version =
|
8
|
+
gem.version = EmeraldRails::VERSION
|
9
9
|
gem.authors = ["Alexandre de Oliveira"]
|
10
10
|
gem.email = ["chavedomundo@gmail.com"]
|
11
11
|
gem.description = %q{Brings Emerald.js into your Rails app automagically}
|
data/lib/emerald-rails.rb
CHANGED
@@ -0,0 +1,308 @@
|
|
1
|
+
var Emerald = {
|
2
|
+
Version: "0.0.1",
|
3
|
+
Core: {},
|
4
|
+
Controller: {},
|
5
|
+
ActionView: {
|
6
|
+
Elements: {},
|
7
|
+
Events: {}
|
8
|
+
},
|
9
|
+
Model: {}
|
10
|
+
};
|
11
|
+
|
12
|
+
Emerald.ActionView.extend = function(actions){
|
13
|
+
// instance object to be returned
|
14
|
+
var instance = new Object;
|
15
|
+
var _this = this;
|
16
|
+
_this.actions = actions;
|
17
|
+
|
18
|
+
if (_this.actions.initialize) {
|
19
|
+
$(document).ready(function(){
|
20
|
+
_this.actions.initialize();
|
21
|
+
});
|
22
|
+
}
|
23
|
+
|
24
|
+
// initializes this View's elements
|
25
|
+
$(document).ready(function() {
|
26
|
+
Emerald.ActionView.Elements.bindToView();
|
27
|
+
Emerald.ActionView.Events.bindElementToViewAction(_this.actions);
|
28
|
+
});
|
29
|
+
|
30
|
+
// defines the method of the new view instance
|
31
|
+
for (action in actions) {
|
32
|
+
instance[action] = actions[action];
|
33
|
+
}
|
34
|
+
|
35
|
+
return instance;
|
36
|
+
}
|
37
|
+
|
38
|
+
Emerald.ActionView.Elements.bindToView = function() {
|
39
|
+
$("[data-view-action]").each(function(index, viewElement){
|
40
|
+
var elementsView = $(this).closest("[data-view]").data("view");
|
41
|
+
viewElement.setAttribute('extendsView', elementsView);
|
42
|
+
});
|
43
|
+
}
|
44
|
+
|
45
|
+
Emerald.ActionView.Elements.dataFieldsSelector = function(){
|
46
|
+
var selector = "";
|
47
|
+
selector+= "input[type='text'], ";
|
48
|
+
selector+= "input[type='radio']:checked";
|
49
|
+
return selector;
|
50
|
+
}
|
51
|
+
|
52
|
+
Emerald.ActionView.Elements.dataFields = function(){
|
53
|
+
var selectors = this.dataFieldsSelector();
|
54
|
+
return $(selectors);
|
55
|
+
}
|
56
|
+
|
57
|
+
Emerald.ActionView.Events.bindElementToViewAction = function(viewActions) {
|
58
|
+
$('[data-view-action]').each(function(index, viewElement){
|
59
|
+
var _event = viewElement.getAttribute('data-event');
|
60
|
+
var _viewAction = viewElement.getAttribute('data-view-action');
|
61
|
+
|
62
|
+
_event = Emerald.ActionView.Events.defineDefaultEvent(viewElement, _event);
|
63
|
+
|
64
|
+
var dataFieldSelector = "";
|
65
|
+
dataFieldSelector+= "input[type='text'][data-view-action='"+_viewAction+"'], ";
|
66
|
+
dataFieldSelector+= "input[type='radio'][data-view-action='"+_viewAction+"']:checked";
|
67
|
+
|
68
|
+
$(this).on(_event, function(e){
|
69
|
+
var dataFields = $(dataFieldSelector);
|
70
|
+
viewActions[_viewAction](e, dataFields);
|
71
|
+
});
|
72
|
+
});
|
73
|
+
}
|
74
|
+
|
75
|
+
Emerald.ActionView.Events.defineDefaultEvent = function(domElement, _event) {
|
76
|
+
if (_event)
|
77
|
+
return _event;
|
78
|
+
else
|
79
|
+
return 'keyup';
|
80
|
+
}
|
81
|
+
|
82
|
+
Emerald.Controller.extend = function(actions){
|
83
|
+
var instance = new Object;
|
84
|
+
var _this = this;
|
85
|
+
_this.actions = actions;
|
86
|
+
|
87
|
+
// defines the method of the new controller instance
|
88
|
+
for (action in actions) {
|
89
|
+
instance[action] = actions[action];
|
90
|
+
}
|
91
|
+
|
92
|
+
instance.params = function(domElements){
|
93
|
+
var params = new Object;
|
94
|
+
|
95
|
+
for (i = 0; i < domElements.length; i++) {
|
96
|
+
var element = domElements[i];
|
97
|
+
params[element.name] = element.value;
|
98
|
+
}
|
99
|
+
|
100
|
+
return params;
|
101
|
+
}
|
102
|
+
|
103
|
+
instance.persistView = true;
|
104
|
+
|
105
|
+
instance.persistViewCallback = function(JSON, observerObject) {
|
106
|
+
if (!observerObject)
|
107
|
+
observerObject = Emerald.Model.Observer;
|
108
|
+
|
109
|
+
if (this.persistView)
|
110
|
+
observerObject.update(JSON);
|
111
|
+
|
112
|
+
return true;
|
113
|
+
}
|
114
|
+
|
115
|
+
return instance;
|
116
|
+
}
|
117
|
+
|
118
|
+
Emerald.Model.extend = function(properties) {
|
119
|
+
var instance = new Object;
|
120
|
+
var _this = this;
|
121
|
+
this.properties = properties;
|
122
|
+
|
123
|
+
// defines the method of the new view instance
|
124
|
+
for (property in properties) {
|
125
|
+
instance[property] = properties[property];
|
126
|
+
}
|
127
|
+
|
128
|
+
// runs initialization on startup
|
129
|
+
if (this.properties.initialize) {
|
130
|
+
$(document).ready(function(){ _this.properties.initialize(); });
|
131
|
+
}
|
132
|
+
|
133
|
+
// defines attrAccessible fields
|
134
|
+
Emerald.Model.Attributes.initAttrAccessible(instance, properties.attrAccessible);
|
135
|
+
|
136
|
+
instance.get = function(attribute) {
|
137
|
+
if (_this.properties[attribute])
|
138
|
+
return _this.properties[attribute];
|
139
|
+
else
|
140
|
+
return null;
|
141
|
+
}
|
142
|
+
|
143
|
+
instance.save = function(data, andCallbackController, persistenceObject) {
|
144
|
+
if (!persistenceObject)
|
145
|
+
persistenceObject = Emerald.Persistence;
|
146
|
+
|
147
|
+
// TODO verify that `data` fields are the ones listed in this.attrAccessible
|
148
|
+
return new persistenceObject(this).save(data, andCallbackController);
|
149
|
+
}
|
150
|
+
|
151
|
+
return instance;
|
152
|
+
}
|
153
|
+
|
154
|
+
Emerald.Model.Attributes = {}
|
155
|
+
Emerald.Model.Attributes.initAttrAccessible = function(model, attrAccessible) {
|
156
|
+
model.attributes = function() { return false; }
|
157
|
+
if (!attrAccessible)
|
158
|
+
return false;
|
159
|
+
|
160
|
+
var attributes = attrAccessible;
|
161
|
+
for (attribute in attributes) {
|
162
|
+
var attributeName = attributes[attribute];
|
163
|
+
|
164
|
+
// model method
|
165
|
+
if (!model[attributeName])
|
166
|
+
model[attributeName] = "";
|
167
|
+
|
168
|
+
// attributes hash
|
169
|
+
model.attributes = function(attributeName) {
|
170
|
+
var attributes = Emerald.Model.Attributes.getAttributes(model);
|
171
|
+
|
172
|
+
if (attributeName)
|
173
|
+
return attributes[attributeName];
|
174
|
+
else
|
175
|
+
return attributes;
|
176
|
+
}
|
177
|
+
}
|
178
|
+
|
179
|
+
return true;
|
180
|
+
}
|
181
|
+
|
182
|
+
Emerald.Model.Attributes.getAttributes = function(model) {
|
183
|
+
var attributesValues = {}
|
184
|
+
if (!model.attrAccessible)
|
185
|
+
return attributesValues;
|
186
|
+
|
187
|
+
var attributes = model.attrAccessible;
|
188
|
+
for (attribute in attributes) {
|
189
|
+
var attributeName = attributes[attribute];
|
190
|
+
|
191
|
+
// model method
|
192
|
+
attributesValues[attributeName] = model[attributeName];
|
193
|
+
}
|
194
|
+
return attributesValues;
|
195
|
+
}
|
196
|
+
|
197
|
+
// Observes the whole document for [data-observe] elements. Whenever an Ajax
|
198
|
+
// call is made, the JSON response should be passed in to
|
199
|
+
//
|
200
|
+
// observer.update(response)
|
201
|
+
//
|
202
|
+
// It will update all the elements in the document with the JSON data.
|
203
|
+
//
|
204
|
+
// Example:
|
205
|
+
//
|
206
|
+
// JSON: {"item":{"name":"My item", "price":"US$40,00"}}
|
207
|
+
//
|
208
|
+
// Document:
|
209
|
+
//
|
210
|
+
// <span data-observe="item.name"></span>
|
211
|
+
// <span data-observe="item.price"></span>
|
212
|
+
//
|
213
|
+
// The first HTML element will be updated with "My item" automatically and
|
214
|
+
// the second, price, with "US$40,00".
|
215
|
+
//
|
216
|
+
Emerald.Model.Observer = {
|
217
|
+
update: function(jsonData){
|
218
|
+
$("[data-observe]").each(function(index){
|
219
|
+
var observing = $(this).data("observe");
|
220
|
+
var observedResources = observing.split(".");
|
221
|
+
|
222
|
+
var currentValue = jsonData;
|
223
|
+
$.each(observedResources, function(index, value){
|
224
|
+
if (currentValue[value] || typeof currentValue[value] == "string")
|
225
|
+
currentValue = currentValue[value];
|
226
|
+
else
|
227
|
+
return false;
|
228
|
+
});
|
229
|
+
|
230
|
+
switch (typeof currentValue) {
|
231
|
+
case "number":
|
232
|
+
case "bool":
|
233
|
+
case "string":
|
234
|
+
$(this).html(currentValue);
|
235
|
+
return true;
|
236
|
+
}
|
237
|
+
|
238
|
+
});
|
239
|
+
|
240
|
+
return true;
|
241
|
+
}
|
242
|
+
}
|
243
|
+
|
244
|
+
Emerald.Persistence = function(model, persistenceObject) {
|
245
|
+
var instance = new Object;
|
246
|
+
|
247
|
+
if (!persistenceObject)
|
248
|
+
persistenceObject = Emerald.Persistence;
|
249
|
+
|
250
|
+
instance.save = function(model, callbackController){
|
251
|
+
return persistenceObject.save(model, callbackController);
|
252
|
+
}
|
253
|
+
|
254
|
+
return instance;
|
255
|
+
}
|
256
|
+
|
257
|
+
|
258
|
+
Emerald.Persistence.save = function(model, callbackController) {
|
259
|
+
var instance = function() {
|
260
|
+
this.initialize = function(model) {
|
261
|
+
this.model = model;
|
262
|
+
return this;
|
263
|
+
}
|
264
|
+
|
265
|
+
this.save = function(model, controller) {
|
266
|
+
var attributes = this.model.attributes();
|
267
|
+
|
268
|
+
if (!attributes)
|
269
|
+
return false;
|
270
|
+
|
271
|
+
var _controller = controller;
|
272
|
+
var requestSpecs = Emerald.Persistence.saveRequest(model);
|
273
|
+
|
274
|
+
$.ajax(requestSpecs).done(function(jsonResponse) {
|
275
|
+
if (_controller)
|
276
|
+
_controller.persistViewCallback(jsonResponse);
|
277
|
+
}).fail(function(jsonResponse) {
|
278
|
+
if (_controller)
|
279
|
+
_controller.failedAjaxResponseCallback(jsonResponse);
|
280
|
+
});
|
281
|
+
|
282
|
+
return requestSpecs;
|
283
|
+
}
|
284
|
+
|
285
|
+
return this;
|
286
|
+
}
|
287
|
+
|
288
|
+
instance().initialize(model);
|
289
|
+
return instance().save(model, callbackController);
|
290
|
+
}
|
291
|
+
|
292
|
+
Emerald.Persistence.saveRequest = function(model) {
|
293
|
+
var attributes = model.attributes();
|
294
|
+
|
295
|
+
return {
|
296
|
+
url: model.route,
|
297
|
+
data: attributes,
|
298
|
+
type: Emerald.Persistence.saveRequestType(attributes),
|
299
|
+
dataType: "json"
|
300
|
+
}
|
301
|
+
}
|
302
|
+
|
303
|
+
Emerald.Persistence.saveRequestType = function(attributes) {
|
304
|
+
if (attributes["id"])
|
305
|
+
return "PUT";
|
306
|
+
else
|
307
|
+
return "POST";
|
308
|
+
}
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: emerald-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -39,10 +39,13 @@ files:
|
|
39
39
|
- LICENSE.txt
|
40
40
|
- README.md
|
41
41
|
- Rakefile
|
42
|
-
- app/assets/javascript/
|
42
|
+
- app/assets/javascript/emerald.js
|
43
43
|
- emerald-rails.gemspec
|
44
44
|
- lib/emerald-rails.rb
|
45
45
|
- lib/emerald-rails/version.rb
|
46
|
+
- lib/generators/emerald/install/USAGE
|
47
|
+
- lib/generators/emerald/install/install_generator.rb
|
48
|
+
- lib/generators/emerald/install/templates/emerald.js
|
46
49
|
homepage: http://github.com/kurko/emerald-rails
|
47
50
|
licenses: []
|
48
51
|
post_install_message:
|