rails_ember_validations 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: df12d9676acf553fcac1f09b51763133eb3dae99
4
+ data.tar.gz: 639d9859de2d9b49191951186f5a9c7ce0a8e7bf
5
+ SHA512:
6
+ metadata.gz: b3eb4dc1d3d2c26eb69346d7b07c2b32097397513d0ca2026c7db292197802ce9793063a2dc5ea94497a8544e0f65600b89d7b1737ff12ec964fa9a1bd959078
7
+ data.tar.gz: d56f739e6ecbd3ff90bc6ed7cec7fba4b0df5c15ccdbf324a4d4a321f51f0f235d38408f035142f81d4c9975ed602c1263a1190b1678d84d8db612e7fb3696ff
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 rails_ember_validations.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Alexandros M
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
+ # RailsEmberValidations
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'rails_ember_validations'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install rails_ember_validations
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 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,92 @@
1
+ App.ValidationView = Ember.View.extend(Ember.TargetActionSupport, {
2
+ previousErrors: [],
3
+ resetErrors: function() {
4
+ return this.get('previousErrors').forEach((function(_this) {
5
+ return function(error) {
6
+ _this.set(error + 'Validation', null);
7
+ return $('#' + error + 'Input').removeClass('has-error');
8
+ };
9
+ })(this));
10
+ },
11
+ actions: {
12
+ cancel: function(object) {
13
+ if (object.get('isNew')) {
14
+ object.deleteRecord();
15
+ } else {
16
+ object.rollback();
17
+ }
18
+ return this.resetErrors();
19
+ },
20
+ validate: function() {
21
+ var attrs, controller, object, relationships;
22
+ if (!this.get('validationObject')) {
23
+ this.set('validationObject', this.get('controller.content'));
24
+ }
25
+ this.resetErrors();
26
+ controller = this;
27
+ relationships = Ember.get(this.get('validationObject.constructor'), 'relationshipNames');
28
+ object = String(this.get('validationObject.constructor'));
29
+ object = object.split('.');
30
+ object = Ember.String.capitalize(object.pop()).underscore();
31
+ attrs = {};
32
+ attrs[object] = this.get('validationObject._attributes');
33
+ this.get('validationObject').eachAttribute((function(_this) {
34
+ return function(att) {
35
+ if (!_this.get('validationObject.excludedProperties').contains(att)) {
36
+ return attrs[object][att] = _this.get('validationObject').get(att);
37
+ }
38
+ };
39
+ })(this));
40
+ relationships.belongsTo.forEach((function(_this) {
41
+ return function(relationship) {
42
+ if (_this.get('validationObject').get(relationship)) {
43
+ return attrs[object][relationship + '_id'] = _this.get('validationObject').get(relationship).get('id');
44
+ }
45
+ };
46
+ })(this));
47
+ relationships.hasMany.forEach((function(_this) {
48
+ return function(relationship) {
49
+ var relationshipArray;
50
+ relationshipArray = [];
51
+ _this.get('validationObject').get(relationship).forEach(function(related) {
52
+ return relationshipArray.push(related.get('id'));
53
+ });
54
+ return attrs[object][relationship.singularize() + '_ids'] = relationshipArray;
55
+ };
56
+ })(this));
57
+ if (attrs[object][Ember.keys(attrs[object]).get('firstObject')] === void 0) {
58
+ attrs[object][Ember.keys(attrs[object]).get('firstObject')] = null;
59
+ }
60
+ if (this.get('validationObject.id') !== null) {
61
+ attrs[object]['id'] = this.get('validationObject').get('id');
62
+ }
63
+ return Ember.$.post('/validate.json', attrs).then(function(data) {
64
+ controller.set('content', attrs);
65
+ return controller.triggerAction({
66
+ action: "save",
67
+ target: controller.get("controller")
68
+ });
69
+ }, function(error) {
70
+ var errorsHash;
71
+ controller.set('previousErrors', []);
72
+ errorsHash = JSON.parse(error.responseText);
73
+ if (_.difference(Ember.keys(errorsHash), controller.get('validationObject.excludedProperties')).length === 0) {
74
+ controller.set('content', attrs);
75
+ return controller.triggerAction({
76
+ action: "save",
77
+ target: controller.get("controller")
78
+ });
79
+ } else {
80
+ return Ember.keys(errorsHash).forEach(function(key) {
81
+ controller.get('previousErrors').push(key);
82
+ if ($('div[id^=' + key + 'Input')) {
83
+ $('div[id^=' + key + 'Input').addClass('has-error');
84
+ }
85
+ error = errorsHash[key].join(', ');
86
+ return controller.set(key + 'Validation', error);
87
+ });
88
+ }
89
+ });
90
+ }
91
+ }
92
+ });
@@ -0,0 +1,52 @@
1
+
2
+ class ValidationsController < ApplicationController
3
+
4
+ rescue_from CanCan::AccessDenied do |exception|
5
+ render json: {authorization: ["Your are not allowed to #{$action} this #{object_params.first[0]} with this content. Please make changes to your form "]}, status: 403
6
+ end
7
+ def validate
8
+ validation_object = object
9
+
10
+ if validation_object.valid?
11
+ render json: :null, status: 200
12
+ else
13
+
14
+ render json: validation_object.errors, status: 422
15
+ end
16
+ end
17
+
18
+ def authorize_new(action)
19
+ authorize_object = object
20
+ authorize! action.to_sym, authorize_object
21
+ end
22
+
23
+
24
+ def object
25
+ if object_params.first[1][:id] == nil
26
+ $action = 'create'
27
+ object = Object::const_get(object_params.first[0].capitalize).new(object_params.first[1])
28
+ else
29
+ $action = 'update'
30
+ object = Object::const_get(object_params.first[0].capitalize).find(object_params.first[1][:id])
31
+
32
+ object_params.first[1].keys.each do |key|
33
+
34
+ if object.attributes.has_key? key
35
+ object[key] = object_params.first[1][key]
36
+ end
37
+ end
38
+ end
39
+ cancan = !!Module.const_get('CanCan') rescue false
40
+
41
+ authorize! $action.to_sym, object if cancan
42
+ return object
43
+ end
44
+
45
+ private
46
+ def object_params
47
+ params.permit!
48
+
49
+
50
+ end
51
+
52
+ end
data/config/routes.rb ADDED
@@ -0,0 +1,3 @@
1
+ Rails.application.routes.draw do
2
+ post '/validate', to: 'validations#validate', as: 'validate'
3
+ end
@@ -0,0 +1,8 @@
1
+ Description:
2
+ Explain the generator
3
+
4
+ Example:
5
+ rails generate rails_ember_val Thing
6
+
7
+ This will create:
8
+ what/will/it/create
@@ -0,0 +1,41 @@
1
+ require 'rails/generators/base'
2
+ module RailsEmberValidations
3
+ class InstallGenerator < Rails::Generators::NamedBase
4
+
5
+
6
+
7
+
8
+
9
+ def add_js
10
+ mode = :coffee
11
+ if File.exists? "app/assets/javascripts/application.js.coffee"
12
+ path = "app/assets/javascripts/application.js.coffee"
13
+ elsif File.exists? "app/assets/javascripts/application.coffee"
14
+ path = "app/assets/javascripts/application.coffee"
15
+ elsif File.exists? "app/assets/javascripts/application.js"
16
+ mode = :js
17
+ path = "app/assets/javascripts/application.js"
18
+ end
19
+
20
+ original = File.binread(path)
21
+ if original.include?("require validation-view")
22
+ puts "Skipped validation-view.js inclusion. Allready included"
23
+ else
24
+ if mode == :coffee
25
+ insert_into_file path, :after => %r{#= ['"]?require_self['"]?\s*$} do
26
+ "\n#= require validation-view"
27
+ end
28
+ puts "Added validation-view.js file to #{path}"
29
+ else
30
+ insert_into_file path, :after => %r{//= ['"]?require_self['"]?\s*$} do
31
+ "\n//= require validation-view"
32
+ end
33
+ puts "Added validation-view.js file to #{path}"
34
+ end
35
+ end
36
+ end
37
+
38
+
39
+
40
+ end
41
+ end
@@ -0,0 +1,6 @@
1
+ require "rails_ember_validations/version"
2
+ require "rails_ember_validations/engine"
3
+ module RailsEmberValidations
4
+
5
+
6
+ end
@@ -0,0 +1,5 @@
1
+ module RailsEmberValidations
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace RailsEmberValidations
4
+ end
5
+ end
@@ -0,0 +1,3 @@
1
+ module RailsEmberValidations
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rails_ember_validations/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rails_ember_validations"
8
+ spec.version = RailsEmberValidations::VERSION
9
+ spec.authors = ["Alexandros M"]
10
+ spec.email = ["alexandros.mastas@gmail.com"]
11
+ spec.description = "Server side validations for ember.js framework, model agnostic"
12
+ spec.summary = "Server side validations"
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/) + Dir["{app,config,lib}/**/*"]
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails_ember_validations
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Alexandros M
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Server side validations for ember.js framework, model agnostic
42
+ email:
43
+ - alexandros.mastas@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - .gitignore
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - lib/rails_ember_validations.rb
54
+ - lib/rails_ember_validations/version.rb
55
+ - rails_ember_validations.gemspec
56
+ - app/controllers/rails_ember_validations/validations_controller.rb
57
+ - app/assets/javascripts/validation-view.js
58
+ - config/routes.rb
59
+ - lib/rails_ember_validations/engine.rb
60
+ - lib/generators/rails_ember_validations/USAGE
61
+ - lib/generators/rails_ember_validations/install_generator.rb
62
+ homepage: ''
63
+ licenses:
64
+ - MIT
65
+ metadata: {}
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - '>='
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ requirements: []
81
+ rubyforge_project:
82
+ rubygems_version: 2.1.11
83
+ signing_key:
84
+ specification_version: 4
85
+ summary: Server side validations
86
+ test_files: []
87
+ has_rdoc: