regulate 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (81) hide show
  1. data/.gitignore +14 -0
  2. data/.rvmrc +1 -0
  3. data/.yardopts +1 -0
  4. data/Gemfile +4 -0
  5. data/Gemfile.lock +117 -0
  6. data/LICENSE +20 -0
  7. data/README.md +3 -0
  8. data/Rakefile +10 -0
  9. data/app/controllers/regulate/admin/pages_controller.rb +69 -0
  10. data/app/controllers/regulate/pages_controller.rb +13 -0
  11. data/app/models/regulate/page.rb +21 -0
  12. data/app/views/regulate/admin/pages/_form.html.erb +36 -0
  13. data/app/views/regulate/admin/pages/edit.html.erb +1 -0
  14. data/app/views/regulate/admin/pages/index.html.erb +10 -0
  15. data/app/views/regulate/admin/pages/new.html.erb +1 -0
  16. data/app/views/regulate/pages/show.html.erb +14 -0
  17. data/config/regulate.yml +10 -0
  18. data/config/routes.rb +22 -0
  19. data/lib/generators/regulate/install_generator.rb +39 -0
  20. data/lib/generators/regulate/views_generator.rb +73 -0
  21. data/lib/generators/templates/regulate.css +2 -0
  22. data/lib/generators/templates/regulate.js +44 -0
  23. data/lib/generators/templates/regulate.rb +13 -0
  24. data/lib/generators/templates/regulate.yml +10 -0
  25. data/lib/regulate.rb +30 -0
  26. data/lib/regulate/engine.rb +50 -0
  27. data/lib/regulate/git.rb +17 -0
  28. data/lib/regulate/git/errors.rb +22 -0
  29. data/lib/regulate/git/interface.rb +249 -0
  30. data/lib/regulate/git/model.rb +16 -0
  31. data/lib/regulate/git/model/base.rb +282 -0
  32. data/lib/regulate/version.rb +4 -0
  33. data/public/javascripts/jquery.min.js +167 -0
  34. data/public/javascripts/regulate_admin.js +166 -0
  35. data/regulate.gemspec +29 -0
  36. data/test/dummy/Rakefile +7 -0
  37. data/test/dummy/app/controllers/application_controller.rb +3 -0
  38. data/test/dummy/app/helpers/application_helper.rb +2 -0
  39. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  40. data/test/dummy/config.ru +4 -0
  41. data/test/dummy/config/application.rb +45 -0
  42. data/test/dummy/config/boot.rb +10 -0
  43. data/test/dummy/config/database.yml +22 -0
  44. data/test/dummy/config/environment.rb +5 -0
  45. data/test/dummy/config/environments/development.rb +26 -0
  46. data/test/dummy/config/environments/production.rb +49 -0
  47. data/test/dummy/config/environments/test.rb +35 -0
  48. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  49. data/test/dummy/config/initializers/inflections.rb +10 -0
  50. data/test/dummy/config/initializers/mime_types.rb +5 -0
  51. data/test/dummy/config/initializers/secret_token.rb +7 -0
  52. data/test/dummy/config/initializers/session_store.rb +8 -0
  53. data/test/dummy/config/locales/en.yml +5 -0
  54. data/test/dummy/config/regulate.yml +10 -0
  55. data/test/dummy/config/routes.rb +58 -0
  56. data/test/dummy/public/404.html +26 -0
  57. data/test/dummy/public/422.html +26 -0
  58. data/test/dummy/public/500.html +26 -0
  59. data/test/dummy/public/favicon.ico +0 -0
  60. data/test/dummy/public/javascripts/application.js +2 -0
  61. data/test/dummy/public/javascripts/controls.js +965 -0
  62. data/test/dummy/public/javascripts/dragdrop.js +974 -0
  63. data/test/dummy/public/javascripts/effects.js +1123 -0
  64. data/test/dummy/public/javascripts/prototype.js +6001 -0
  65. data/test/dummy/public/javascripts/rails.js +175 -0
  66. data/test/dummy/public/stylesheets/.gitkeep +0 -0
  67. data/test/dummy/script/rails +6 -0
  68. data/test/git_test.rb +79 -0
  69. data/test/integration/navigation_test.rb +7 -0
  70. data/test/models/regulate_git_model_base_lint_test.rb +7 -0
  71. data/test/models/regulate_git_model_base_test.rb +23 -0
  72. data/test/models/regulate_page_test.rb +19 -0
  73. data/test/regulate_test.rb +19 -0
  74. data/test/routing_test.rb +32 -0
  75. data/test/support/integration_case.rb +5 -0
  76. data/test/test_helper.rb +27 -0
  77. data/test/tmp/config/initializers/regulate.rb +12 -0
  78. data/test/tmp/config/regulate.yml +10 -0
  79. data/test/tmp/public/javascripts/regulate.js +49 -0
  80. data/test/tmp/public/stylesheets/regulate.css +3 -0
  81. metadata +312 -0
@@ -0,0 +1,175 @@
1
+ (function() {
2
+ // Technique from Juriy Zaytsev
3
+ // http://thinkweb2.com/projects/prototype/detecting-event-support-without-browser-sniffing/
4
+ function isEventSupported(eventName) {
5
+ var el = document.createElement('div');
6
+ eventName = 'on' + eventName;
7
+ var isSupported = (eventName in el);
8
+ if (!isSupported) {
9
+ el.setAttribute(eventName, 'return;');
10
+ isSupported = typeof el[eventName] == 'function';
11
+ }
12
+ el = null;
13
+ return isSupported;
14
+ }
15
+
16
+ function isForm(element) {
17
+ return Object.isElement(element) && element.nodeName.toUpperCase() == 'FORM'
18
+ }
19
+
20
+ function isInput(element) {
21
+ if (Object.isElement(element)) {
22
+ var name = element.nodeName.toUpperCase()
23
+ return name == 'INPUT' || name == 'SELECT' || name == 'TEXTAREA'
24
+ }
25
+ else return false
26
+ }
27
+
28
+ var submitBubbles = isEventSupported('submit'),
29
+ changeBubbles = isEventSupported('change')
30
+
31
+ if (!submitBubbles || !changeBubbles) {
32
+ // augment the Event.Handler class to observe custom events when needed
33
+ Event.Handler.prototype.initialize = Event.Handler.prototype.initialize.wrap(
34
+ function(init, element, eventName, selector, callback) {
35
+ init(element, eventName, selector, callback)
36
+ // is the handler being attached to an element that doesn't support this event?
37
+ if ( (!submitBubbles && this.eventName == 'submit' && !isForm(this.element)) ||
38
+ (!changeBubbles && this.eventName == 'change' && !isInput(this.element)) ) {
39
+ // "submit" => "emulated:submit"
40
+ this.eventName = 'emulated:' + this.eventName
41
+ }
42
+ }
43
+ )
44
+ }
45
+
46
+ if (!submitBubbles) {
47
+ // discover forms on the page by observing focus events which always bubble
48
+ document.on('focusin', 'form', function(focusEvent, form) {
49
+ // special handler for the real "submit" event (one-time operation)
50
+ if (!form.retrieve('emulated:submit')) {
51
+ form.on('submit', function(submitEvent) {
52
+ var emulated = form.fire('emulated:submit', submitEvent, true)
53
+ // if custom event received preventDefault, cancel the real one too
54
+ if (emulated.returnValue === false) submitEvent.preventDefault()
55
+ })
56
+ form.store('emulated:submit', true)
57
+ }
58
+ })
59
+ }
60
+
61
+ if (!changeBubbles) {
62
+ // discover form inputs on the page
63
+ document.on('focusin', 'input, select, texarea', function(focusEvent, input) {
64
+ // special handler for real "change" events
65
+ if (!input.retrieve('emulated:change')) {
66
+ input.on('change', function(changeEvent) {
67
+ input.fire('emulated:change', changeEvent, true)
68
+ })
69
+ input.store('emulated:change', true)
70
+ }
71
+ })
72
+ }
73
+
74
+ function handleRemote(element) {
75
+ var method, url, params;
76
+
77
+ var event = element.fire("ajax:before");
78
+ if (event.stopped) return false;
79
+
80
+ if (element.tagName.toLowerCase() === 'form') {
81
+ method = element.readAttribute('method') || 'post';
82
+ url = element.readAttribute('action');
83
+ params = element.serialize();
84
+ } else {
85
+ method = element.readAttribute('data-method') || 'get';
86
+ url = element.readAttribute('href');
87
+ params = {};
88
+ }
89
+
90
+ new Ajax.Request(url, {
91
+ method: method,
92
+ parameters: params,
93
+ evalScripts: true,
94
+
95
+ onComplete: function(request) { element.fire("ajax:complete", request); },
96
+ onSuccess: function(request) { element.fire("ajax:success", request); },
97
+ onFailure: function(request) { element.fire("ajax:failure", request); }
98
+ });
99
+
100
+ element.fire("ajax:after");
101
+ }
102
+
103
+ function handleMethod(element) {
104
+ var method = element.readAttribute('data-method'),
105
+ url = element.readAttribute('href'),
106
+ csrf_param = $$('meta[name=csrf-param]')[0],
107
+ csrf_token = $$('meta[name=csrf-token]')[0];
108
+
109
+ var form = new Element('form', { method: "POST", action: url, style: "display: none;" });
110
+ element.parentNode.insert(form);
111
+
112
+ if (method !== 'post') {
113
+ var field = new Element('input', { type: 'hidden', name: '_method', value: method });
114
+ form.insert(field);
115
+ }
116
+
117
+ if (csrf_param) {
118
+ var param = csrf_param.readAttribute('content'),
119
+ token = csrf_token.readAttribute('content'),
120
+ field = new Element('input', { type: 'hidden', name: param, value: token });
121
+ form.insert(field);
122
+ }
123
+
124
+ form.submit();
125
+ }
126
+
127
+
128
+ document.on("click", "*[data-confirm]", function(event, element) {
129
+ var message = element.readAttribute('data-confirm');
130
+ if (!confirm(message)) event.stop();
131
+ });
132
+
133
+ document.on("click", "a[data-remote]", function(event, element) {
134
+ if (event.stopped) return;
135
+ handleRemote(element);
136
+ event.stop();
137
+ });
138
+
139
+ document.on("click", "a[data-method]", function(event, element) {
140
+ if (event.stopped) return;
141
+ handleMethod(element);
142
+ event.stop();
143
+ });
144
+
145
+ document.on("submit", function(event) {
146
+ var element = event.findElement(),
147
+ message = element.readAttribute('data-confirm');
148
+ if (message && !confirm(message)) {
149
+ event.stop();
150
+ return false;
151
+ }
152
+
153
+ var inputs = element.select("input[type=submit][data-disable-with]");
154
+ inputs.each(function(input) {
155
+ input.disabled = true;
156
+ input.writeAttribute('data-original-value', input.value);
157
+ input.value = input.readAttribute('data-disable-with');
158
+ });
159
+
160
+ var element = event.findElement("form[data-remote]");
161
+ if (element) {
162
+ handleRemote(element);
163
+ event.stop();
164
+ }
165
+ });
166
+
167
+ document.on("ajax:after", "form", function(event, element) {
168
+ var inputs = element.select("input[type=submit][disabled=true][data-disable-with]");
169
+ inputs.each(function(input) {
170
+ input.value = input.readAttribute('data-original-value');
171
+ input.removeAttribute('data-original-value');
172
+ input.disabled = false;
173
+ });
174
+ });
175
+ })();
File without changes
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ # This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
3
+
4
+ APP_PATH = File.expand_path('../../config/application', __FILE__)
5
+ require File.expand_path('../../config/boot', __FILE__)
6
+ require 'rails/commands'
data/test/git_test.rb ADDED
@@ -0,0 +1,79 @@
1
+ require 'test_helper'
2
+
3
+ class Regulate::Git::InterfaceTest < ActiveSupport::TestCase
4
+
5
+ @@iterator = 0
6
+
7
+ def get_iterator
8
+ @@iterator += 1
9
+ @@iterator
10
+ end
11
+
12
+ # lets create a commit to run our tests with
13
+ def setup
14
+ @commit_data = {
15
+ :id => "a-real-deal-id-#{get_iterator}",
16
+ :commit_message => 'Creating Git Resource',
17
+ :author_name => 'Collin',
18
+ :author_email => 'collin@quickleft.com',
19
+ :attributes => "{ 'test' : 'test #{get_iterator}' }",
20
+ :rendered => "<h1>Test #{get_iterator}</h1>"
21
+ }
22
+ @commit_sha = Regulate::Git::Interface.save(@commit_data)
23
+ end
24
+
25
+ # Test that our commit returns a sha
26
+ # Just check length > 0
27
+ test "create a commit" do
28
+ assert_operator @commit_sha.length , :> , 0
29
+ end
30
+
31
+ test "commits return" do
32
+ commit_found = false
33
+ Regulate::Git::Interface.commits(@commit_data[:id]).each do |commit|
34
+ commit_found = true if commit.sha == @commit_sha
35
+ end
36
+ assert commit_found
37
+ end
38
+
39
+ test "can find by id" do
40
+ result = Regulate::Git::Interface.find(@commit_data[:id])
41
+ assert_equal @commit_data[:attributes] , result
42
+ end
43
+
44
+ test "can find all git resources" do
45
+ results = Regulate::Git::Interface.find_all
46
+ assert results.include? @commit_data[:attributes]
47
+ end
48
+
49
+ test "can grab rendered from the repo" do
50
+ assert_equal @commit_data[:rendered] , Regulate::Git::Interface.find_rendered(@commit_data[:id])
51
+ end
52
+
53
+ test "can check if a resource exists" do
54
+ assert Regulate::Git::Interface.exists?(@commit_data[:id])
55
+ end
56
+
57
+ test "can grab the most recent commit" do
58
+ assert_equal @commit_sha , Regulate::Git::Interface.last_commit.sha
59
+ end
60
+
61
+ test "can delete a git resource" do
62
+ Regulate::Git::Interface.delete(@commit_data).inspect
63
+ assert_nil Regulate::Git::Interface.find(@commit_data[:id])
64
+ end
65
+
66
+ test "can find a specific version of a git resource" do
67
+ new_commit_data = @commit_data.merge({
68
+ :commit_message => 'Updating Git Resource',
69
+ :author_name => 'Collin',
70
+ :author_email => 'collin@quickleft.com',
71
+ :attributes => "{ 'test' : '1234567' }"
72
+ })
73
+ new_commit_sha = Regulate::Git::Interface.save(new_commit_data)
74
+ assert_equal @commit_data[:attributes] , Regulate::Git::Interface.find_by_version(@commit_data[:id],@commit_sha)
75
+ assert_equal new_commit_data[:attributes] , Regulate::Git::Interface.find_by_version(@commit_data[:id],new_commit_sha)
76
+ end
77
+
78
+ end
79
+
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class NavigationTest < ActiveSupport::IntegrationCase
4
+ test "truth" do
5
+ assert_kind_of Dummy::Application, Rails.application
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ class Regulate::Git::Model::BaseLintTest < ActiveModel::TestCase
2
+ include ActiveModel::Lint::Tests
3
+ def setup
4
+ @model = Regulate::Git::Model::Base.new
5
+ end
6
+ end
7
+
@@ -0,0 +1,23 @@
1
+ require 'test_helper'
2
+
3
+ class Regulate::Git::Model::BaseTest < ActiveSupport::TestCase
4
+
5
+ def setup
6
+ @resource = Regulate::Git::Model::Base.new({
7
+ :title => "Happy Title",
8
+ :view => "{{title}}"
9
+ })
10
+ end
11
+
12
+ test "new is not persisted" do
13
+ assert_equal false , @resource.persisted?
14
+ end
15
+
16
+ test "new sets attr methods" do
17
+ assert_respond_to @resource , :title
18
+ assert_respond_to @resource , :title=
19
+ assert_respond_to @resource , :title?
20
+ end
21
+
22
+ end
23
+
@@ -0,0 +1,19 @@
1
+ require 'test_helper'
2
+
3
+ class Regulate::PageTest < ActiveSupport::TestCase
4
+
5
+ test "title setter sets id" do
6
+ page = Regulate::Page.new({
7
+ :title => "A Fancy Title"
8
+ })
9
+ assert_equal "a-fancy-title" , page.id
10
+ end
11
+
12
+ test "title with crazy symbols gets regulated" do
13
+ page = Regulate::Page.new({
14
+ :title => "*@A Fa$ncy titlE!!!"
15
+ })
16
+ assert_equal "a-fa-ncy-title" , page.id
17
+ end
18
+
19
+ end
@@ -0,0 +1,19 @@
1
+ require 'test_helper'
2
+
3
+ class RegulateTest < ActiveSupport::TestCase
4
+
5
+ test "truth" do
6
+ assert_kind_of Module, Regulate
7
+ end
8
+
9
+ test 'setup block yields self' do
10
+ Regulate.setup do |config|
11
+ assert_equal Regulate, config
12
+ end
13
+ end
14
+
15
+ test 'repo is set' do
16
+ assert_equal File.join(Regulate.app_root,"db", "repos", "test.git", ".git"), Regulate.repo.path
17
+ end
18
+
19
+ end
@@ -0,0 +1,32 @@
1
+ require 'test_helper'
2
+
3
+ class DefaultRoutingTest < ActionController::TestCase
4
+
5
+ @@sample_page = Regulate::Page.create!({
6
+ :title => "Whatup sample page",
7
+ :view => "{{title}}"
8
+ })
9
+
10
+
11
+ # Setup our tests
12
+ def setup
13
+ Regulate.setup do |config|
14
+ config.route_namespace = "a_test_namespace"
15
+ end
16
+ Dummy::Application.reload_routes!
17
+ end
18
+
19
+ test 'map public page show' do
20
+ assert_routing( { :path => "#{Regulate.route_namespace}/pages/#{@@sample_page.id}" , :method => :get } , { :controller => 'regulate/pages' , :action => 'show', :id => @@sample_page.id } )
21
+ end
22
+
23
+ test 'map admin pages index' do
24
+ assert_routing( { :path => "#{Regulate.route_namespace}/admin/pages" , :method => :get } , { :controller => 'regulate/admin/pages' , :action => 'index' } )
25
+ end
26
+
27
+ test 'map admin edit page' do
28
+ assert_routing( { :path => "#{Regulate.route_namespace}/admin/pages/#{@@sample_page.id}/edit" , :method => :get } , { :controller => 'regulate/admin/pages' , :action => 'edit', :id => @@sample_page.id } )
29
+ end
30
+
31
+ end
32
+
@@ -0,0 +1,5 @@
1
+ # Define a bare test case to use with Capybara
2
+ class ActiveSupport::IntegrationCase < ActiveSupport::TestCase
3
+ include Capybara
4
+ include Rails.application.routes.url_helpers
5
+ end
@@ -0,0 +1,27 @@
1
+ # Configure Rails Envinronment
2
+ ENV["RAILS_ENV"] = "test"
3
+ ENV["RAILS_ROOT"] = File.expand_path("../dummy", __FILE__)
4
+
5
+ require 'fileutils'
6
+ repo_path = File.join(ENV["RAILS_ROOT"], "db", "repos", "test.git")
7
+ FileUtils.remove_dir(repo_path) if File.directory?(repo_path)
8
+
9
+ require File.expand_path("../dummy/config/environment.rb", __FILE__)
10
+ require "rails/test_help"
11
+
12
+ ActionMailer::Base.delivery_method = :test
13
+ ActionMailer::Base.perform_deliveries = true
14
+ ActionMailer::Base.default_url_options[:host] = "test.com"
15
+
16
+ Rails.backtrace_cleaner.remove_silencers!
17
+
18
+ # Configure capybara for integration testing
19
+ require 'capybara/rails'
20
+ Capybara.default_driver = :rack_test
21
+ Capybara.default_selector = :css
22
+
23
+ # Run any available migration
24
+ ActiveRecord::Migrator.migrate File.expand_path("../dummy/db/migrate/", __FILE__)
25
+
26
+ # Load support files
27
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
@@ -0,0 +1,12 @@
1
+ Regulate.setup do |config|
2
+ # Role that allows a user to edit pages, default is :editor
3
+ # config.editor_role = :editor
4
+
5
+ # Role that allows a user to manage pages and define what parts are editable, default is :admin
6
+ # config.admin_role = :admin
7
+
8
+ #YAML.load_file(File.join(Rails.root, "config", "regulate.yml"))[Rails.env]
9
+ #config.
10
+ end
11
+
12
+ # Make sure we have a git repo all setup based on our environment
@@ -0,0 +1,10 @@
1
+ development:
2
+ repo: db/repos/development.git
3
+ test: &test
4
+ repo: db/repos/test.git
5
+ production:
6
+ repo: db/repos/production.git
7
+ daily:
8
+ repo: db/repos/daily.git
9
+ cucumber:
10
+ <<: *test