page_specific_js 0.0.2
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/.gitignore +4 -0
- data/.rvmrc +1 -0
- data/Gemfile +4 -0
- data/README.md +85 -0
- data/Rakefile +10 -0
- data/lib/page_specific_js.rb +8 -0
- data/lib/page_specific_js/helper.rb +5 -0
- data/lib/page_specific_js/version.rb +3 -0
- data/page_specific_js.gemspec +31 -0
- data/spec/javascripts/fixtures/.gitkeep +0 -0
- data/spec/javascripts/helpers/jasmine-jquery-1.3.1.js +288 -0
- data/spec/javascripts/helpers/jquery.js +9266 -0
- data/spec/javascripts/page_specific_spec.js +160 -0
- data/spec/javascripts/support/jasmine.yml +76 -0
- data/spec/javascripts/support/jasmine_config.rb +23 -0
- data/spec/javascripts/support/jasmine_runner.rb +32 -0
- data/vendor/assets/javascripts/page_specific.js +72 -0
- metadata +94 -0
@@ -0,0 +1,160 @@
|
|
1
|
+
DummyAppWithoutPageSpecific = {};
|
2
|
+
|
3
|
+
DummyAppWithoutAllControllers = {
|
4
|
+
PageSpecific: {
|
5
|
+
}
|
6
|
+
};
|
7
|
+
|
8
|
+
DummyAppWithAllControllers = {
|
9
|
+
PageSpecific: {
|
10
|
+
AllControllers: {
|
11
|
+
}
|
12
|
+
}
|
13
|
+
};
|
14
|
+
|
15
|
+
DummyApplication = {
|
16
|
+
PageSpecific: {
|
17
|
+
AllControllers: {
|
18
|
+
allActions: function() {},
|
19
|
+
someactionAction: function() {}
|
20
|
+
},
|
21
|
+
TestOneController: {
|
22
|
+
// This controller-specific class has neither allActions nor an action-specifc method
|
23
|
+
},
|
24
|
+
TestTwoController: {
|
25
|
+
// This controller has both allActions and an action-specific method
|
26
|
+
allActions: function() {},
|
27
|
+
someactionAction: function() {
|
28
|
+
console.debug('someactionAction was called');
|
29
|
+
}
|
30
|
+
}
|
31
|
+
}
|
32
|
+
};
|
33
|
+
|
34
|
+
describe("PageSpecific", function() {
|
35
|
+
var $appClassToTest;
|
36
|
+
|
37
|
+
describe(".init", function() {
|
38
|
+
|
39
|
+
describe("for an app that doesn't have a PageSpecific class", function() {
|
40
|
+
beforeEach(function() {
|
41
|
+
appClassToTest = DummyAppWithoutPageSpecific;
|
42
|
+
});
|
43
|
+
|
44
|
+
it("should not throw an exception", function() {
|
45
|
+
expect(function() {
|
46
|
+
PageSpecific.init(appClassToTest);
|
47
|
+
}).not.toThrow();
|
48
|
+
});
|
49
|
+
});
|
50
|
+
|
51
|
+
describe("for an app that doesn't have AllControllers", function() {
|
52
|
+
beforeEach(function() {
|
53
|
+
appClassToTest = DummyAppWithoutAllControllers;
|
54
|
+
});
|
55
|
+
|
56
|
+
it("should not throw an exception", function() {
|
57
|
+
expect(function() {
|
58
|
+
PageSpecific.init(appClassToTest);
|
59
|
+
}).not.toThrow();
|
60
|
+
});
|
61
|
+
});
|
62
|
+
|
63
|
+
describe("for an app that does have AllControllers", function() {
|
64
|
+
beforeEach(function() {
|
65
|
+
appClassToTest = DummyAppWithAllControllers;
|
66
|
+
});
|
67
|
+
|
68
|
+
describe("for an app that doesn't have AllControllers.allActions or a AllControllers.<current action>Action method", function() {
|
69
|
+
beforeEach(function() {
|
70
|
+
$('body').attr('data-controller_name', 'irrelevant_controller');
|
71
|
+
$('body').attr('data-action_name', 'someaction');
|
72
|
+
});
|
73
|
+
|
74
|
+
it("should not throw an exception", function() {
|
75
|
+
expect(function() {
|
76
|
+
PageSpecific.init(appClassToTest);
|
77
|
+
}).not.toThrow();
|
78
|
+
});
|
79
|
+
});
|
80
|
+
|
81
|
+
describe("for an app that does have AllControllers.allActions and a AllControllers.<current action>Action method", function() {
|
82
|
+
beforeEach(function() {
|
83
|
+
appClassToTest = DummyApplication;
|
84
|
+
});
|
85
|
+
|
86
|
+
it("should invoke App.PageSpecific.AllControllers.allActions method", function() {
|
87
|
+
spyOn(DummyApplication.PageSpecific.AllControllers, 'allActions');
|
88
|
+
PageSpecific.init(appClassToTest);
|
89
|
+
expect(DummyApplication.PageSpecific.AllControllers.allActions).toHaveBeenCalled();
|
90
|
+
});
|
91
|
+
|
92
|
+
it("should invoke App.PageSpecific.AllControllers.<currentaction>Action", function() {
|
93
|
+
spyOn(DummyApplication.PageSpecific.AllControllers, 'someactionAction');
|
94
|
+
PageSpecific.init(appClassToTest);
|
95
|
+
expect(DummyApplication.PageSpecific.AllControllers.someactionAction).toHaveBeenCalled();
|
96
|
+
});
|
97
|
+
});
|
98
|
+
});
|
99
|
+
});
|
100
|
+
|
101
|
+
describe("for a page whose body doesn't have data-controller_name or data-action_name attributes", function() {
|
102
|
+
it("should not throw an exception", function() {
|
103
|
+
expect(function() {
|
104
|
+
PageSpecific.init(DummyApplication);
|
105
|
+
}).not.toThrow();
|
106
|
+
});
|
107
|
+
});
|
108
|
+
|
109
|
+
describe("if there is no controller-specific class for this controller", function() {
|
110
|
+
beforeEach(function() {
|
111
|
+
$('body').attr('data-controller_name', 'missing_controller');
|
112
|
+
$('body').attr('data-action_name', 'someaction');
|
113
|
+
});
|
114
|
+
|
115
|
+
it("should not throw an exception", function() {
|
116
|
+
expect(function() {
|
117
|
+
PageSpecific.init(DummyApplication);
|
118
|
+
}).not.toThrow();
|
119
|
+
});
|
120
|
+
});
|
121
|
+
|
122
|
+
describe("if the controller-specific class does not have an all-actions method or action-specifc method", function() {
|
123
|
+
beforeEach(function() {
|
124
|
+
$('body').attr('data-controller_name', 'test_one_controller');
|
125
|
+
$('body').attr('data-action_name', 'someaction');
|
126
|
+
});
|
127
|
+
|
128
|
+
it("should not throw an exception", function() {
|
129
|
+
expect(function() {
|
130
|
+
PageSpecific.init(DummyApplication);
|
131
|
+
}).not.toThrow();
|
132
|
+
});
|
133
|
+
});
|
134
|
+
|
135
|
+
describe("if the controller-specific class has an all-actions method", function() {
|
136
|
+
beforeEach(function() {
|
137
|
+
$('body').attr('data-controller_name', 'test_two_controller');
|
138
|
+
$('body').attr('data-action_name', 'someaction');
|
139
|
+
});
|
140
|
+
|
141
|
+
it("should call the controller-specific AllActions method", function() {
|
142
|
+
spyOn(DummyApplication.PageSpecific.TestTwoController, 'allActions');
|
143
|
+
PageSpecific.init(DummyApplication);
|
144
|
+
expect(DummyApplication.PageSpecific.TestTwoController.allActions).toHaveBeenCalled();
|
145
|
+
});
|
146
|
+
});
|
147
|
+
|
148
|
+
describe("if the controller-specific class has an action method for this action", function() {
|
149
|
+
beforeEach(function() {
|
150
|
+
$('body').attr('data-controller_name', 'test_two_controller');
|
151
|
+
$('body').attr('data-action_name', 'someaction');
|
152
|
+
});
|
153
|
+
|
154
|
+
it("should call the controller+action specific method", function() {
|
155
|
+
spyOn(DummyApplication.PageSpecific.TestTwoController, 'someactionAction');
|
156
|
+
PageSpecific.init(DummyApplication);
|
157
|
+
expect(DummyApplication.PageSpecific.TestTwoController.someactionAction).toHaveBeenCalled();
|
158
|
+
});
|
159
|
+
});
|
160
|
+
});
|
@@ -0,0 +1,76 @@
|
|
1
|
+
# src_files
|
2
|
+
#
|
3
|
+
# Return an array of filepaths relative to src_dir to include before jasmine specs.
|
4
|
+
# Default: []
|
5
|
+
#
|
6
|
+
# EXAMPLE:
|
7
|
+
#
|
8
|
+
# src_files:
|
9
|
+
# - lib/source1.js
|
10
|
+
# - lib/source2.js
|
11
|
+
# - dist/**/*.js
|
12
|
+
#
|
13
|
+
src_files:
|
14
|
+
- vendor/assets/javascripts/page_specific.js
|
15
|
+
|
16
|
+
# stylesheets
|
17
|
+
#
|
18
|
+
# Return an array of stylesheet filepaths relative to src_dir to include before jasmine specs.
|
19
|
+
# Default: []
|
20
|
+
#
|
21
|
+
# EXAMPLE:
|
22
|
+
#
|
23
|
+
# stylesheets:
|
24
|
+
# - css/style.css
|
25
|
+
# - stylesheets/*.css
|
26
|
+
#
|
27
|
+
stylesheets:
|
28
|
+
- stylesheets/**/*.css
|
29
|
+
|
30
|
+
# helpers
|
31
|
+
#
|
32
|
+
# Return an array of filepaths relative to spec_dir to include before jasmine specs.
|
33
|
+
# Default: ["helpers/**/*.js"]
|
34
|
+
#
|
35
|
+
# EXAMPLE:
|
36
|
+
#
|
37
|
+
# helpers:
|
38
|
+
# - helpers/**/*.js
|
39
|
+
#
|
40
|
+
helpers:
|
41
|
+
- helpers/**/*.js
|
42
|
+
|
43
|
+
# spec_files
|
44
|
+
#
|
45
|
+
# Return an array of filepaths relative to spec_dir to include.
|
46
|
+
# Default: ["**/*[sS]pec.js"]
|
47
|
+
#
|
48
|
+
# EXAMPLE:
|
49
|
+
#
|
50
|
+
# spec_files:
|
51
|
+
# - **/*[sS]pec.js
|
52
|
+
#
|
53
|
+
spec_files:
|
54
|
+
- '**/*[sS]pec.js'
|
55
|
+
|
56
|
+
# src_dir
|
57
|
+
#
|
58
|
+
# Source directory path. Your src_files must be returned relative to this path. Will use root if left blank.
|
59
|
+
# Default: project root
|
60
|
+
#
|
61
|
+
# EXAMPLE:
|
62
|
+
#
|
63
|
+
# src_dir: public
|
64
|
+
#
|
65
|
+
src_dir:
|
66
|
+
|
67
|
+
# spec_dir
|
68
|
+
#
|
69
|
+
# Spec directory path. Your spec_files must be returned relative to this path.
|
70
|
+
# Default: spec/javascripts
|
71
|
+
#
|
72
|
+
# EXAMPLE:
|
73
|
+
#
|
74
|
+
# spec_dir: spec/javascripts
|
75
|
+
#
|
76
|
+
spec_dir: spec/javascripts
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Jasmine
|
2
|
+
class Config
|
3
|
+
|
4
|
+
# Add your overrides or custom config code here
|
5
|
+
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
|
10
|
+
# Note - this is necessary for rspec2, which has removed the backtrace
|
11
|
+
module Jasmine
|
12
|
+
class SpecBuilder
|
13
|
+
def declare_spec(parent, spec)
|
14
|
+
me = self
|
15
|
+
example_name = spec["name"]
|
16
|
+
@spec_ids << spec["id"]
|
17
|
+
backtrace = @example_locations[parent.description + " " + example_name]
|
18
|
+
parent.it example_name, {} do
|
19
|
+
me.report_spec(spec["id"])
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
$:.unshift(ENV['JASMINE_GEM_PATH']) if ENV['JASMINE_GEM_PATH'] # for gem testing purposes
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'jasmine'
|
5
|
+
jasmine_config_overrides = File.expand_path(File.join(File.dirname(__FILE__), 'jasmine_config.rb'))
|
6
|
+
require jasmine_config_overrides if File.exist?(jasmine_config_overrides)
|
7
|
+
if Jasmine::Dependencies.rspec2?
|
8
|
+
require 'rspec'
|
9
|
+
else
|
10
|
+
require 'spec'
|
11
|
+
end
|
12
|
+
|
13
|
+
jasmine_config = Jasmine::Config.new
|
14
|
+
spec_builder = Jasmine::SpecBuilder.new(jasmine_config)
|
15
|
+
|
16
|
+
should_stop = false
|
17
|
+
|
18
|
+
if Jasmine::Dependencies.rspec2?
|
19
|
+
RSpec.configuration.after(:suite) do
|
20
|
+
spec_builder.stop if should_stop
|
21
|
+
end
|
22
|
+
else
|
23
|
+
Spec::Runner.configure do |config|
|
24
|
+
config.after(:suite) do
|
25
|
+
spec_builder.stop if should_stop
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
spec_builder.start
|
31
|
+
should_stop = true
|
32
|
+
spec_builder.declare_suites
|
@@ -0,0 +1,72 @@
|
|
1
|
+
/*
|
2
|
+
* This is a page-specific JavaScript framework (primarily for Rails).
|
3
|
+
*
|
4
|
+
* It allows you to target JS to run on all pages, for a specific action on all pages (e.g. index),
|
5
|
+
* for all actions for a specific controller, or for one page (specific controller/action).
|
6
|
+
*
|
7
|
+
* See README file for installation info and examples.
|
8
|
+
*/
|
9
|
+
|
10
|
+
PageSpecific = {
|
11
|
+
|
12
|
+
init: function(application, debug) {
|
13
|
+
var controllerName = $('body').attr('data-controller_name');
|
14
|
+
var actionName = $('body').attr('data-action_name');
|
15
|
+
var controllerSpecificCodeClassname;
|
16
|
+
var actionMethodName;
|
17
|
+
|
18
|
+
PageSpecific.debugging = debug;
|
19
|
+
|
20
|
+
if (controllerName != undefined) {
|
21
|
+
controllerSpecificCodeClassname = PageSpecific.camelize(controllerName);
|
22
|
+
PageSpecific.debug('controllerSpecificCodeClassname = '+ controllerSpecificCodeClassname);
|
23
|
+
}
|
24
|
+
if (actionName != undefined) {
|
25
|
+
actionMethodName = PageSpecific.camelize(actionName, true) + 'Action';
|
26
|
+
PageSpecific.debug('actionMethodName = '+ actionMethodName);
|
27
|
+
}
|
28
|
+
|
29
|
+
if (application.PageSpecific &&
|
30
|
+
application.PageSpecific.AllControllers) {
|
31
|
+
if (application.PageSpecific.AllControllers.allActions) {
|
32
|
+
PageSpecific.debug('calling AllControllers.allActions');
|
33
|
+
application.PageSpecific.AllControllers.allActions();
|
34
|
+
}
|
35
|
+
if (actionMethodName && application.PageSpecific.AllControllers[actionMethodName]) {
|
36
|
+
application.PageSpecific.AllControllers[actionMethodName]();
|
37
|
+
}
|
38
|
+
}
|
39
|
+
|
40
|
+
if (controllerSpecificCodeClassname && application.PageSpecific[controllerSpecificCodeClassname]) {
|
41
|
+
PageSpecific.debug('detected '+ controllerSpecificCodeClassname);
|
42
|
+
if (application.PageSpecific[controllerSpecificCodeClassname].allActions) {
|
43
|
+
PageSpecific.debug('calling '+ controllerSpecificCodeClassname +'.allActions');
|
44
|
+
application.PageSpecific[controllerSpecificCodeClassname].allActions();
|
45
|
+
}
|
46
|
+
|
47
|
+
if (actionMethodName && application.PageSpecific[controllerSpecificCodeClassname][actionMethodName]) {
|
48
|
+
PageSpecific.debug('calling '+ controllerSpecificCodeClassname + "." + actionMethodName);
|
49
|
+
application.PageSpecific[controllerSpecificCodeClassname][actionMethodName]();
|
50
|
+
}
|
51
|
+
}
|
52
|
+
},
|
53
|
+
|
54
|
+
camelize: function(str, lowerFirstChar) {
|
55
|
+
var newStr = str.replace(/_(.)/g, function(match, firstChar) {
|
56
|
+
return firstChar.toUpperCase();
|
57
|
+
});
|
58
|
+
if (lowerFirstChar) {
|
59
|
+
return newStr;
|
60
|
+
} else {
|
61
|
+
return newStr.replace(/^(.)/, function(match, firstChar) {
|
62
|
+
return firstChar.toUpperCase();
|
63
|
+
});
|
64
|
+
}
|
65
|
+
},
|
66
|
+
|
67
|
+
debug: function(message) {
|
68
|
+
if (PageSpecific.debugging) {
|
69
|
+
console.debug('PageSpecific: ' + message);
|
70
|
+
}
|
71
|
+
}
|
72
|
+
};
|
metadata
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: page_specific_js
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Sam Pierson
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-01-15 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake
|
16
|
+
requirement: &70329436518100 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70329436518100
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: jasmine
|
27
|
+
requirement: &70329436527180 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70329436527180
|
36
|
+
description: ! " It allows you to target JS to run on all pages,\n for
|
37
|
+
a particular action on all pages (e.g. index),\n for all actions
|
38
|
+
for a specific controller,\n or for one specific controller/action
|
39
|
+
(i.e. a single page).\n "
|
40
|
+
email:
|
41
|
+
- sam@sampierson.com
|
42
|
+
executables: []
|
43
|
+
extensions: []
|
44
|
+
extra_rdoc_files: []
|
45
|
+
files:
|
46
|
+
- .gitignore
|
47
|
+
- .rvmrc
|
48
|
+
- Gemfile
|
49
|
+
- README.md
|
50
|
+
- Rakefile
|
51
|
+
- lib/page_specific_js.rb
|
52
|
+
- lib/page_specific_js/helper.rb
|
53
|
+
- lib/page_specific_js/version.rb
|
54
|
+
- page_specific_js.gemspec
|
55
|
+
- spec/javascripts/fixtures/.gitkeep
|
56
|
+
- spec/javascripts/helpers/jasmine-jquery-1.3.1.js
|
57
|
+
- spec/javascripts/helpers/jquery.js
|
58
|
+
- spec/javascripts/page_specific_spec.js
|
59
|
+
- spec/javascripts/support/jasmine.yml
|
60
|
+
- spec/javascripts/support/jasmine_config.rb
|
61
|
+
- spec/javascripts/support/jasmine_runner.rb
|
62
|
+
- vendor/assets/javascripts/page_specific.js
|
63
|
+
homepage: ''
|
64
|
+
licenses: []
|
65
|
+
post_install_message:
|
66
|
+
rdoc_options: []
|
67
|
+
require_paths:
|
68
|
+
- lib
|
69
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
70
|
+
none: false
|
71
|
+
requirements:
|
72
|
+
- - ! '>='
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
|
+
none: false
|
77
|
+
requirements:
|
78
|
+
- - ! '>='
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '0'
|
81
|
+
requirements: []
|
82
|
+
rubyforge_project: page_specific_js
|
83
|
+
rubygems_version: 1.8.10
|
84
|
+
signing_key:
|
85
|
+
specification_version: 3
|
86
|
+
summary: Page-specific JavaScript framework for Rails
|
87
|
+
test_files:
|
88
|
+
- spec/javascripts/fixtures/.gitkeep
|
89
|
+
- spec/javascripts/helpers/jasmine-jquery-1.3.1.js
|
90
|
+
- spec/javascripts/helpers/jquery.js
|
91
|
+
- spec/javascripts/page_specific_spec.js
|
92
|
+
- spec/javascripts/support/jasmine.yml
|
93
|
+
- spec/javascripts/support/jasmine_config.rb
|
94
|
+
- spec/javascripts/support/jasmine_runner.rb
|