codus 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +138 -0
- data/lib/README.md +122 -0
- data/lib/codus.rb +11 -0
- data/lib/codus/version.rb +3 -0
- data/lib/codus/view_helpers.rb +12 -0
- data/lib/codus/view_helpers/yojs_helper.rb +12 -0
- data/lib/codus/view_helpers/yojs_helper/yojs_call_generator.rb +70 -0
- metadata +86 -0
data/README.md
ADDED
@@ -0,0 +1,138 @@
|
|
1
|
+
Codus
|
2
|
+
=====
|
3
|
+
|
4
|
+
Tools to improve your life as Rails developer.
|
5
|
+
|
6
|
+
|
7
|
+
|
8
|
+
Javascript Organizer
|
9
|
+
--------------------
|
10
|
+
|
11
|
+
You can write your javascript functions using [yojs namespaces declaration](https://github.com/codus/yojs). Namespaces that matches your application name, current controller and current action will automatic be fired.
|
12
|
+
|
13
|
+
### Setup
|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
- Put in your Gemfile:
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
gem 'codus'
|
21
|
+
```
|
22
|
+
|
23
|
+
- Install the gem with *bundle install*
|
24
|
+
|
25
|
+
- Put in your layout:
|
26
|
+
|
27
|
+
`<%= load_yojs(:app_name => 'yourappname') %>`
|
28
|
+
|
29
|
+
- Put in your application.js
|
30
|
+
|
31
|
+
```javascript
|
32
|
+
//= require jquery
|
33
|
+
//= require yojs
|
34
|
+
```
|
35
|
+
|
36
|
+
- Declare your functions using yojs and enjoy your life
|
37
|
+
|
38
|
+
|
39
|
+
|
40
|
+
**Options**
|
41
|
+
|
42
|
+
|
43
|
+
|
44
|
+
***app_name***
|
45
|
+
|
46
|
+
The *load_yojs* helper method will print a javascript code, using query and yojs, that calls namespaces matching the app_name option, current controller name and current action name.
|
47
|
+
|
48
|
+
|
49
|
+
|
50
|
+
For instance, if you're in /posts/new it will call:
|
51
|
+
|
52
|
+
|
53
|
+
|
54
|
+
1. yourappname
|
55
|
+
|
56
|
+
2. yourappname.posts
|
57
|
+
|
58
|
+
3. yourappname.posts.new
|
59
|
+
|
60
|
+
|
61
|
+
|
62
|
+
***onload_method_name***
|
63
|
+
|
64
|
+
You can set the name of the last namespace to be called on the unload event with the *onload_method_name* option. For instance:
|
65
|
+
|
66
|
+
|
67
|
+
```erb
|
68
|
+
<%= load_yojs(:app_name => 'yourappname', :onload_method_name => "myonloadmethod") %>
|
69
|
+
```
|
70
|
+
|
71
|
+
If you're in /posts/new it will call:
|
72
|
+
|
73
|
+
1. yourappname.<myonloadmethod>
|
74
|
+
|
75
|
+
2. yourappname.posts.<myonloadmethod>
|
76
|
+
|
77
|
+
3. yourappname.posts.new.<myonloadmethod>
|
78
|
+
|
79
|
+
|
80
|
+
***method_names_mapper***
|
81
|
+
|
82
|
+
You can also configure equivalent namespaces using :<method_names_mapper. For instance:>
|
83
|
+
|
84
|
+
|
85
|
+
|
86
|
+
```erb
|
87
|
+
<%= load_yojs(:app_name => 'yourappname',
|
88
|
+
|
89
|
+
:method_names_mapper => {
|
90
|
+
|
91
|
+
:create => :new,
|
92
|
+
|
93
|
+
:update => :edit
|
94
|
+
|
95
|
+
}
|
96
|
+
|
97
|
+
)%>
|
98
|
+
```
|
99
|
+
|
100
|
+
If you're in /posts/create it will call:
|
101
|
+
|
102
|
+
1. yourappname
|
103
|
+
|
104
|
+
2. yourappname.posts
|
105
|
+
|
106
|
+
3. yourappname.posts.create
|
107
|
+
|
108
|
+
4. yourappname.posts.new
|
109
|
+
|
110
|
+
|
111
|
+
File structure
|
112
|
+
--------------------
|
113
|
+
|
114
|
+
We recomend you to structure your js files according your namespace hierarchy. Example
|
115
|
+
|
116
|
+
```
|
117
|
+
- assets
|
118
|
+
|- javascripts
|
119
|
+
|- application.js
|
120
|
+
|- components (jquery, plugins, etc)
|
121
|
+
|- myapp
|
122
|
+
| |- myapp.js (global javascripts in "myapp" namespace)
|
123
|
+
| |- controller1
|
124
|
+
| | |- controller1.js (javascripts shared between "myapp.controller1" namespace)
|
125
|
+
| | |- action1.js (javascripts for "myapp.controller1.action1" namespace)
|
126
|
+
| | |- action2.js (javascripts for "myapp.controller1.action2" namespace)
|
127
|
+
| | |- actionN.js (javascripts for "myapp.controller1.actionN" namespace)
|
128
|
+
| |
|
129
|
+
| |- controller2
|
130
|
+
| | |- controller2.js (javascripts shared between "myapp.controller2" namespace)
|
131
|
+
| | |- action1.js (javascripts for "myapp.controller2.action1" namespace)
|
132
|
+
| | |- action2.js (javascripts for "myapp.controller2.action2" namespace)
|
133
|
+
| | |- actionN.js (javascripts for "myapp.controller2.actionN" namespace)
|
134
|
+
| |
|
135
|
+
| |- controllerN
|
136
|
+
| | |- controllerN.js (javascripts shared between "myapp.controllerN" namespace)
|
137
|
+
| | |- ...
|
138
|
+
```
|
data/lib/README.md
ADDED
@@ -0,0 +1,122 @@
|
|
1
|
+
Codus
|
2
|
+
=====
|
3
|
+
|
4
|
+
Tools to improve your life as Rails developer.
|
5
|
+
|
6
|
+
|
7
|
+
|
8
|
+
Javascript Organizer
|
9
|
+
--------------------
|
10
|
+
|
11
|
+
You can organize your javascript functions using yojs namespaces declaration and
|
12
|
+
the ones that matches your application name, current controller and current
|
13
|
+
action will automatic be fired.
|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
### Setup
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
1. Put in your Gemfile:
|
22
|
+
|
23
|
+
gem 'codus'
|
24
|
+
|
25
|
+
2. Install the gem with *bundle install*
|
26
|
+
|
27
|
+
3. Put in your layout:
|
28
|
+
|
29
|
+
`<%= load_yojs(:app_name => 'yourappname') %>`
|
30
|
+
|
31
|
+
4. Put in your application.js
|
32
|
+
|
33
|
+
`//= require jquery`
|
34
|
+
|
35
|
+
`//= require yojs`
|
36
|
+
|
37
|
+
5. Declare your functions using yojs and enjoy your life
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
**Options**
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
**app_name**
|
46
|
+
|
47
|
+
The *load_yojs* helper method will print a javascript code, using query and
|
48
|
+
yojs, that calls namespaces matching the app_name option, current controller name
|
49
|
+
and current action name.
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
For instance, if you're in /posts/new it will call:
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
1. yourappname
|
58
|
+
|
59
|
+
2. yourappname.posts
|
60
|
+
|
61
|
+
3. yourappname.posts.new
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
**onload_method_name**
|
66
|
+
|
67
|
+
You can set the name of the last namespace to be called on the unload event with
|
68
|
+
the
|
69
|
+
|
70
|
+
<onload_method_name option. >For instance:
|
71
|
+
|
72
|
+
|
73
|
+
|
74
|
+
`<%= load_yojs(:app_name => 'yourappname', :onload_method_name =>
|
75
|
+
"myonloadmethod") %>`
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
If you're in /posts/new it will call:
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
1. yourappname.<myonloadmethod>
|
84
|
+
|
85
|
+
2. yourappname.posts.<myonloadmethod>
|
86
|
+
|
87
|
+
3. yourappname.posts.new.<myonloadmethod>
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
**method_names_mapper**
|
92
|
+
|
93
|
+
You can also configure equivalent namespaces using :<method_names_mapper. For
|
94
|
+
instance:>
|
95
|
+
|
96
|
+
|
97
|
+
|
98
|
+
`<%= load_yojs(:app_name => 'yourappname', `
|
99
|
+
|
100
|
+
` :method_names_mapper => {`
|
101
|
+
|
102
|
+
` :create => :new,`
|
103
|
+
|
104
|
+
` :update => :edit`
|
105
|
+
|
106
|
+
` }`
|
107
|
+
|
108
|
+
`)%>`
|
109
|
+
|
110
|
+
|
111
|
+
|
112
|
+
If you're in /posts/create it will call:
|
113
|
+
|
114
|
+
|
115
|
+
|
116
|
+
1. yourappname
|
117
|
+
|
118
|
+
2. yourappname.posts
|
119
|
+
|
120
|
+
3. yourappname.posts.create
|
121
|
+
|
122
|
+
4. yourappname.posts.new
|
data/lib/codus.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require "codus/view_helpers/yojs_helper/yojs_call_generator"
|
2
|
+
|
3
|
+
module Codus
|
4
|
+
module ViewHelpers
|
5
|
+
module YojsHelper
|
6
|
+
def load_yojs(options = {})
|
7
|
+
call_generator = YojsCallGenerator.new(params[:controller], params[:action], options)
|
8
|
+
javascript_ready { call_generator.generate_yojs_calls() }.html_safe
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
module Codus
|
2
|
+
module ViewHelpers
|
3
|
+
module YojsHelper
|
4
|
+
class YojsCallGenerator
|
5
|
+
def initialize(controller, action, options = {})
|
6
|
+
@options = merge_options_with_defaults(options)
|
7
|
+
@parent_namespace = "#{@options[:app_name]}.#{controller.parameterize('.')}"
|
8
|
+
@last_namespace = action.parameterize(".")
|
9
|
+
@fullnamespace = "#{@parent_namespace}.#{@last_namespace}"
|
10
|
+
|
11
|
+
end
|
12
|
+
|
13
|
+
def merge_options_with_defaults(options)
|
14
|
+
default_options = { :app_name => "",
|
15
|
+
:onload_method_name => "",
|
16
|
+
:method_names_mapper => {
|
17
|
+
:create => :new,
|
18
|
+
:update => :edit
|
19
|
+
}
|
20
|
+
}
|
21
|
+
mapping_options = options.delete(:method_names_mapper)
|
22
|
+
default_options[:method_names_mapper].merge!(mapping_options) unless mapping_options.nil?
|
23
|
+
default_options.merge(options)
|
24
|
+
end
|
25
|
+
|
26
|
+
def generate_yojs_calls
|
27
|
+
namespaces_to_call_onload = get_all_namespaces_from_full_namespace
|
28
|
+
onload_calls_definitions = append_onload_method_to_namespaces(namespaces_to_call_onload)
|
29
|
+
conditional_callings = generate_conditional_function_calling_for_namespaces(onload_calls_definitions)
|
30
|
+
conditional_callings.join("\n").html_safe
|
31
|
+
end
|
32
|
+
|
33
|
+
def get_all_namespaces_from_full_namespace
|
34
|
+
namespaces = []
|
35
|
+
@fullnamespace.split(".").each do |actual_namespace|
|
36
|
+
if namespaces.empty?
|
37
|
+
namespaces << actual_namespace
|
38
|
+
else
|
39
|
+
namespaces << ("#{namespaces.last}.#{actual_namespace}")
|
40
|
+
end
|
41
|
+
end
|
42
|
+
if @options[:method_names_mapper].has_key?(@fullnamespace.split(".").last.to_sym)
|
43
|
+
namespaces << ("#{@parent_namespace}.#{@options[:method_names_mapper][@last_namespace.to_sym]}")
|
44
|
+
end
|
45
|
+
namespaces
|
46
|
+
end
|
47
|
+
|
48
|
+
def append_onload_method_to_namespaces(namespaces = [])
|
49
|
+
return namespaces if @options[:onload_method_name].nil? || @options[:onload_method_name] == ""
|
50
|
+
namespaces.map do |namespace_to_call_onload|
|
51
|
+
"#{namespace_to_call_onload}.#{@options[:onload_method_name]}"
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def generate_conditional_function_calling_for_namespaces(namespaces = [])
|
56
|
+
namespaces.map do |call_definition|
|
57
|
+
"
|
58
|
+
if (yojs.isDefined(\"#{call_definition}\")) {
|
59
|
+
yojs.call(\"#{call_definition}\");
|
60
|
+
}
|
61
|
+
"
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
metadata
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: codus
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Vinícius Oyama
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-06-03 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.3'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.3'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: railties
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '3.1'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '3.1'
|
46
|
+
description: Organize your javascript across namespaces matching your controllers
|
47
|
+
and actions. Get automatic executing functions for current controller and action.
|
48
|
+
email: vinicius.oyama@codus.com.br
|
49
|
+
executables: []
|
50
|
+
extensions: []
|
51
|
+
extra_rdoc_files: []
|
52
|
+
files:
|
53
|
+
- lib/codus/version.rb
|
54
|
+
- lib/codus/view_helpers/yojs_helper/yojs_call_generator.rb
|
55
|
+
- lib/codus/view_helpers/yojs_helper.rb
|
56
|
+
- lib/codus/view_helpers.rb
|
57
|
+
- lib/codus.rb
|
58
|
+
- lib/README.md
|
59
|
+
- README.md
|
60
|
+
homepage: https://github.com/codus/codus
|
61
|
+
licenses:
|
62
|
+
- MIT
|
63
|
+
post_install_message:
|
64
|
+
rdoc_options: []
|
65
|
+
require_paths:
|
66
|
+
- lib
|
67
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
none: false
|
69
|
+
requirements:
|
70
|
+
- - ! '>='
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '0'
|
73
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
|
+
none: false
|
75
|
+
requirements:
|
76
|
+
- - ! '>='
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: '0'
|
79
|
+
requirements: []
|
80
|
+
rubyforge_project:
|
81
|
+
rubygems_version: 1.8.25
|
82
|
+
signing_key:
|
83
|
+
specification_version: 3
|
84
|
+
summary: Organize your javascript across namespaces matching your controllers and
|
85
|
+
actions. Get automatic executing functions for current controller and action.
|
86
|
+
test_files: []
|