railj 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,3 @@
1
+ coverage
2
+ rdoc
3
+ pkg
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 [name of plugin creator]
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,13 @@
1
+ Rails.js
2
+ ========
3
+
4
+ Introduction goes here.
5
+
6
+
7
+ Example
8
+ =======
9
+
10
+ Example goes here.
11
+
12
+
13
+ Copyright (c) 2009 [name of plugin creator], released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,54 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = 'railj'
8
+ gem.summary = %Q{Railj}
9
+ gem.description = %Q{Porno with Rails and JavaScript in leading roles}
10
+ gem.email = "kossnocorp@gmail.ru"
11
+ gem.homepage = "http://github.com/kossnocorp/railj"
12
+ gem.authors = ["Aleksandr Koss"]
13
+ gem.version = '0.0.1'
14
+
15
+ Jeweler::GemcutterTasks.new
16
+
17
+ end
18
+ rescue LoadError
19
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
20
+ end
21
+
22
+ require 'rake/testtask'
23
+ Rake::TestTask.new(:test) do |test|
24
+ test.libs << 'lib' << 'test'
25
+ test.pattern = 'test/**/test_*.rb'
26
+ test.verbose = true
27
+ end
28
+
29
+ begin
30
+ require 'rcov/rcovtask'
31
+ Rcov::RcovTask.new do |test|
32
+ test.libs << 'test'
33
+ test.pattern = 'test/**/test_*.rb'
34
+ test.verbose = true
35
+ end
36
+ rescue LoadError
37
+ task :rcov do
38
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
39
+ end
40
+ end
41
+
42
+ task :test => :check_dependencies
43
+
44
+ task :default => :test
45
+
46
+ require 'rake/rdoctask'
47
+ Rake::RDocTask.new do |rdoc|
48
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
49
+
50
+ rdoc.rdoc_dir = 'rdoc'
51
+ rdoc.title = "raijs #{version}"
52
+ rdoc.rdoc_files.include('README*')
53
+ rdoc.rdoc_files.include('lib/**/*.rb')
54
+ end
@@ -0,0 +1,10 @@
1
+ class RailjDefaultedGenerator < Rails::Generator::Base
2
+ def manifest
3
+ record do |m|
4
+ logger.info 'Copying jquery.defaulted.js to javascripts folder...'
5
+
6
+ m.file 'jquery.defaulted.js',
7
+ 'public/javascripts/jquery.defaulted.js'
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,59 @@
1
+ $(function() {
2
+ $('[data-default-value]').each(function() {
3
+ var default_value = $(this).attr('data-default-value');
4
+
5
+ if(default_value != '') {
6
+
7
+ var start_up_value = $(this).val(),
8
+ is_password = $(this).attr('type') == 'password',
9
+ object = $(this),
10
+ default_object = null,
11
+ default_object_html =
12
+ '<input class="' +
13
+ (is_password ? 'default-value-password ' : '' ) +
14
+ 'default-value" value="' + default_value + '" ' +
15
+ 'style="border:0;margin:3px;position:absolute;top:0;left:0;"' +
16
+ '/>';
17
+
18
+ object.wrap('<div style="position: relative;"></div>');
19
+
20
+ function destroy_default_object() {
21
+ if(default_object == null) return;
22
+
23
+ default_object.remove();
24
+ default_object = null;
25
+ }
26
+
27
+ function refocus_default_object() {
28
+ object.focus();
29
+ destroy_default_object();
30
+ }
31
+
32
+ function create_default_object() {
33
+ if(default_object) return;
34
+
35
+ default_object = $(default_object_html).insertAfter(object);
36
+
37
+ default_object
38
+ .focus(refocus_default_object)
39
+ .click(refocus_default_object);
40
+ }
41
+
42
+ // Start up cheking
43
+
44
+ if(start_up_value == '') create_default_object();
45
+
46
+ // Triggers
47
+
48
+ function object_focus() {
49
+ if(default_object) destroy_default_object();
50
+ }
51
+
52
+ object.focus(object_focus).click(object_focus);
53
+
54
+ object.blur(function() {
55
+ if(object.val() == '') create_default_object();
56
+ });
57
+ }
58
+ });
59
+ });
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'railj'
data/install.rb ADDED
@@ -0,0 +1 @@
1
+ # Install hook code here
@@ -0,0 +1,64 @@
1
+ module Railj
2
+ module Defaulted
3
+ def self.included base
4
+ base.class_eval do
5
+
6
+ #
7
+
8
+ alias railsjs__defaulted_text_field text_field
9
+
10
+ def text_field object_name, method = nil, options = {}
11
+
12
+ method_copy = method ? method.clone : nil
13
+
14
+ if method_copy and method_copy.class == Hash
15
+
16
+ if default_value = method_copy[:default]
17
+ method_copy.delete :default
18
+ method_copy['data-default-value'] = default_value
19
+ end
20
+
21
+ railsjs__defaulted_text_field \
22
+ object_name, method_copy
23
+ elsif method
24
+ railsjs__defaulted_text_field \
25
+ object_name, method, options
26
+ else
27
+ railsjs__defaulted_text_field \
28
+ object_name, {}
29
+ end
30
+ end
31
+
32
+ #
33
+
34
+ alias railsjs__defaulted_password_field password_field
35
+
36
+ def password_field object_name, method = nil, options = {}
37
+
38
+ method_copy = method ? method.clone : nil
39
+
40
+ if method_copy and method_copy.class == Hash
41
+
42
+ if default_value = method_copy[:default]
43
+ method_copy.delete :default
44
+ method_copy['data-default-value'] = default_value
45
+ end
46
+
47
+ railsjs__defaulted_password_field \
48
+ object_name, method_copy
49
+ elsif method
50
+ debugger
51
+
52
+ railsjs__defaulted_password_field \
53
+ object_name, method, options
54
+ else
55
+ railsjs__defaulted_password_field \
56
+ object_name, {}
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
63
+
64
+ ActionView::Helpers::FormBuilder.send :include, Railj::Defaulted
data/lib/railj.rb ADDED
@@ -0,0 +1,4 @@
1
+ require 'helpers/defaulted'
2
+
3
+ require 'validations/validations'
4
+ require 'validations/form_helper'
@@ -0,0 +1,36 @@
1
+ module Railj
2
+ module FormHelper
3
+ def self.included base
4
+ base.class_eval do
5
+
6
+ alias origin_text_field text_field
7
+
8
+ def text_field object_name, method = nil, options = {}
9
+ model = eval(@object_name.to_s.classify)
10
+
11
+ validation_rules = {}
12
+
13
+ if defined? model.get_validation_rules
14
+ validation_rules = \
15
+ model.get_validation_rules object_name
16
+ end
17
+
18
+ if method and method.class == Hash
19
+ origin_text_field object_name, method.merge(validation_rules || {})
20
+ elsif method
21
+ origin_text_field object_name, method, options.merge(validation_rules || {})
22
+ else
23
+ origin_text_field object_name, (validation_rules || {})
24
+ end
25
+ end
26
+
27
+ #def password_field
28
+ # debugger
29
+ # qwe = 'asdasd'
30
+ #end
31
+ end
32
+ end
33
+ end
34
+ end
35
+
36
+ ActionView::Helpers::FormBuilder.send :include, Railj::FormHelper
@@ -0,0 +1,125 @@
1
+ module Railj
2
+ module Validations
3
+ def self.included base
4
+ base.extend ClassMethods
5
+ end
6
+
7
+ module ClassMethods
8
+
9
+ def validates_confirmation_of *attr_names
10
+ add_js_validation_value :validates_confirmation_of,
11
+ attr_names[0],
12
+ attr_names[1]
13
+
14
+ eval(get_return_value attr_names)
15
+ end
16
+
17
+ def validates_acceptance_of *attr_names
18
+ # TODO: Check attr_names[1] existance
19
+ add_js_validation_value :validates_acceptance_of,
20
+ attr_names[0],
21
+ attr_names[1]
22
+
23
+ eval(get_return_value attr_names)
24
+ end
25
+
26
+ def validates_presence_of *attr_names
27
+ add_js_validation_value :validates_presence_of,
28
+ attr_names[0],
29
+ attr_names[1]
30
+
31
+ eval(get_return_value attr_names)
32
+ end
33
+
34
+ def validates_length_of *attr_names
35
+ add_js_validation_value :validates_length_of,
36
+ attr_names[0],
37
+ attr_names[1]
38
+
39
+ eval(get_return_value attr_names)
40
+ end
41
+
42
+ def validates_uniqueness_of *attr_names
43
+ # TODO: Create uniqueness options
44
+
45
+ eval(get_return_value attr_names)
46
+ end
47
+
48
+ def validates_format_of *attr_names
49
+ # TODO: JavaScript regexp converter
50
+ add_js_validation_value :validates_format_of,
51
+ attr_names[0],
52
+ attr_names[1]
53
+
54
+ eval(get_return_value attr_names)
55
+ end
56
+
57
+ def validates_inclusion_of *attr_names
58
+ add_js_validation_value :validates_inclusion_of,
59
+ attr_names[0],
60
+ attr_names[1]
61
+
62
+ eval(get_return_value attr_names)
63
+ end
64
+
65
+ def validates_exclusion_of *attr_names
66
+ add_js_validation_value :validates_exclusion_of,
67
+ attr_names[0],
68
+ attr_names[1]
69
+
70
+ eval(get_return_value attr_names)
71
+ end
72
+
73
+ def validates_associated *attr_names
74
+ # TODO: How it wrap to JavaScript?!
75
+
76
+ eval(get_return_value attr_names)
77
+ end
78
+
79
+ def validates_numericality_of *attr_names
80
+ # TODO: How it wrap to JavaScript?!
81
+
82
+ eval(get_return_value attr_names)
83
+ end
84
+
85
+ def get_validation_rules field_name
86
+ @@validation_rules[field_name]
87
+ end
88
+
89
+ protected
90
+
91
+ def get_return_value attr_names
92
+ return_array = []
93
+ attr_names.each_index do |index|
94
+ return_array << "attr_names[#{index}]"
95
+ end
96
+ "super #{return_array.join ', '}"
97
+ end
98
+
99
+ def add_js_validation_value method_name, field_name, options
100
+ options_json = ''
101
+
102
+ if options
103
+ # Prepare options hash to JSON
104
+
105
+ copied_options = options.clone
106
+ copied_options.each_pair do |key, value|
107
+ copied_options[key] = value.to_s if value.class == Range
108
+ end
109
+
110
+ options_json = copied_options.to_json
111
+ end
112
+
113
+ # Create @@validation_rules structure if it needed
114
+
115
+ @@validation_rules = Hash.new unless defined? @@validation_rules
116
+ @@validation_rules[field_name] = Hash.new unless \
117
+ defined?(@@validation_rules[field_name]) == true
118
+
119
+ @@validation_rules[field_name]["data-#{method_name.to_s.gsub('_', '-')}"] = options_json
120
+ end
121
+ end
122
+ end
123
+ end
124
+
125
+ ActiveRecord::Base.class_eval { include Railj::Validations }
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :rails.js do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,8 @@
1
+ require 'test_helper'
2
+
3
+ class Rails.jsTest < ActiveSupport::TestCase
4
+ # Replace this with your real tests.
5
+ test "the truth" do
6
+ assert true
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ require 'rubygems'
2
+ require 'active_support'
3
+ require 'active_support/test_case'
data/uninstall.rb ADDED
@@ -0,0 +1 @@
1
+ # Uninstall hook code here
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: railj
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Aleksandr Koss
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-11-07 00:00:00 +06:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Porno with Rails and JavaScript in leading roles
17
+ email: kossnocorp@gmail.ru
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README
24
+ files:
25
+ - .gitignore
26
+ - MIT-LICENSE
27
+ - README
28
+ - Rakefile
29
+ - generators/railj_defaulted/railj_defaulted_generator.rb
30
+ - generators/railj_defaulted/templates/jquery.defaulted.js
31
+ - init.rb
32
+ - install.rb
33
+ - lib/helpers/defaulted.rb
34
+ - lib/railj.rb
35
+ - lib/validations/form_helper.rb
36
+ - lib/validations/validations.rb
37
+ - tasks/rails.js_tasks.rake
38
+ - test/rails.js_test.rb
39
+ - test/test_helper.rb
40
+ - uninstall.rb
41
+ has_rdoc: true
42
+ homepage: http://github.com/kossnocorp/railj
43
+ licenses: []
44
+
45
+ post_install_message:
46
+ rdoc_options:
47
+ - --charset=UTF-8
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: "0"
55
+ version:
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: "0"
61
+ version:
62
+ requirements: []
63
+
64
+ rubyforge_project:
65
+ rubygems_version: 1.3.5
66
+ signing_key:
67
+ specification_version: 3
68
+ summary: Railj
69
+ test_files:
70
+ - test/test_helper.rb
71
+ - test/rails.js_test.rb