regulate 0.0.1 → 0.1.0

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.
Files changed (35) hide show
  1. data/.gitignore +1 -0
  2. data/Gemfile.lock +6 -1
  3. data/LICENSE +1 -1
  4. data/README.md +78 -2
  5. data/app/controllers/regulate/admin/pages_controller.rb +23 -0
  6. data/app/views/regulate/admin/pages/_form.html.erb +11 -4
  7. data/app/views/regulate/admin/pages/index.html.erb +11 -2
  8. data/lib/generators/regulate/{install_generator.rb → mount_up_generator.rb} +2 -17
  9. data/lib/generators/regulate/{views_generator.rb → strap_generator.rb} +16 -7
  10. data/lib/generators/templates/regulate.rb +19 -0
  11. data/lib/generators/templates/regulate.yml +2 -2
  12. data/lib/generators/templates/regulate_admin.js +167 -0
  13. data/lib/regulate.rb +7 -0
  14. data/lib/regulate/engine.rb +2 -2
  15. data/lib/regulate/git.rb +0 -3
  16. data/lib/regulate/git/interface.rb +1 -1
  17. data/lib/regulate/git/model/base.rb +54 -29
  18. data/lib/regulate/version.rb +1 -1
  19. data/public/javascripts/regulate_admin.js +1 -1
  20. data/public/javascripts/regulate_index.js +32 -0
  21. data/regulate.gemspec +1 -0
  22. data/test/dummy/app/controllers/application_controller.rb +6 -0
  23. data/test/dummy/app/models/user.rb +20 -0
  24. data/test/dummy/config/initializers/regulate.rb +14 -0
  25. data/test/generators/mount_up_generator_test.rb +15 -0
  26. data/test/generators/strap_generator_test.rb +22 -0
  27. data/test/models/regulate_git_model_base_test.rb +176 -7
  28. data/test/{git_test.rb → regulate_git_interface_test.rb} +2 -2
  29. metadata +44 -28
  30. data/lib/generators/templates/regulate.css +0 -2
  31. data/lib/generators/templates/regulate.js +0 -44
  32. data/test/tmp/config/initializers/regulate.rb +0 -12
  33. data/test/tmp/config/regulate.yml +0 -10
  34. data/test/tmp/public/javascripts/regulate.js +0 -49
  35. data/test/tmp/public/stylesheets/regulate.css +0 -3
data/lib/regulate.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  # Requires
2
2
  require 'active_support/dependencies'
3
+ require 'abstract_auth'
4
+ require 'grit'
3
5
 
4
6
  # Our top level module to contain all of our engine gem functionality
5
7
  module Regulate
@@ -7,6 +9,11 @@ module Regulate
7
9
  # Autoloads
8
10
  autoload :Git , 'regulate/git'
9
11
 
12
+ # Setup our AbstractAuth requirements
13
+ AbstractAuth.setup do |config|
14
+ config.requires :authorized_user , :is_admin , :is_editor
15
+ end
16
+
10
17
  # Our host application root path
11
18
  # We set this when the engine is initialized
12
19
  mattr_accessor :app_root
@@ -33,8 +33,8 @@ module Regulate
33
33
  init = true
34
34
  end
35
35
 
36
- Grit::Repo.init(repo_path) if init
37
- config.repo = Grit::Repo.new(repo_path)
36
+ ::Grit::Repo.init(repo_path) if init
37
+ config.repo = ::Grit::Repo.new(repo_path)
38
38
 
39
39
  end
40
40
 
data/lib/regulate/git.rb CHANGED
@@ -1,6 +1,3 @@
1
- # Requires
2
- require 'grit'
3
-
4
1
  module Regulate
5
2
 
6
3
  # Our module to contain all get related functionality
@@ -208,7 +208,7 @@ module Regulate
208
208
  {
209
209
  :name => author_name.nil? ? "Anonymous" : author_name,
210
210
  :email => author_email ||= "anon@anonymous.com",
211
- :message => commit_message ||= mode.eql?(:create) ? "Creating new page." : "Updating page."
211
+ :message => commit_message ||= mode.eql?(:create) ? "Committing resource changes." : "Deleting resource."
212
212
  }
213
213
  end
214
214
 
@@ -15,6 +15,8 @@ module Regulate
15
15
 
16
16
  # The three fields that we absolutely need to successfully save and render any cms based model
17
17
  validates_presence_of :id, :title, :view
18
+ validates_length_of :id , :minimum => 1 , :allow_nil => false , :allow_blank => false
19
+ validates_length_of :title , :minimum => 1 , :allow_nil => false , :allow_blank => false
18
20
 
19
21
  # Standard class init
20
22
  #
@@ -31,7 +33,7 @@ module Regulate
31
33
  clear_cached_vars
32
34
  Regulate::Git::Interface.delete({
33
35
  :id => id,
34
- :commit_message => commit_message,
36
+ :commit_message => commit_message || "Deleting resource #{title}",
35
37
  :author_name => author_name,
36
38
  :author_email => author_email
37
39
  })
@@ -42,9 +44,10 @@ module Regulate
42
44
  return @_versions ||= Regulate::Git::Interface.commits(id)
43
45
  end
44
46
 
45
- # Return the rendered.html file from the repo
47
+ # Return the rendered.html file from the repo or build the rendered html
46
48
  def rendered
47
- return @_rendered ||= Regulate::Git::Interface.find_rendered(id)
49
+ @_rendered ||= ( persisted? ) ? Regulate::Git::Interface.find_rendered(id) : build_rendered_html
50
+ @_rendered
48
51
  end
49
52
 
50
53
  # Allow attributes to be mass assigned then saved
@@ -67,26 +70,28 @@ module Regulate
67
70
  # Attempt to save our record
68
71
  # @return [TrueClass,FalseClass] Whether or not the record was saved
69
72
  def save
70
- if !persisted?
71
- # Object create
72
- # A git object with that id already exists
73
- return false if self.class.exists?(id)
74
- else
75
- # Object update
76
- # No git ID with the current id exists
77
- # You are not allowed to change the ID of an object
78
- return false if !self.class.exists?(id)
79
- end
80
73
  if valid?
74
+ if !persisted?
75
+ # Object create
76
+ # A git object with that id already exists
77
+ return false if self.class.exists?(id)
78
+ else
79
+ # Object update
80
+ # No git ID with the current id exists
81
+ # You are not allowed to change the ID of an object
82
+ return false if !self.class.exists?(id)
83
+ end
84
+
81
85
  clear_cached_vars
82
86
  Regulate::Git::Interface.save({
83
87
  :id => id,
84
- :commit_message => commit_message,
88
+ :commit_message => commit_message || "#{ ( persisted? ) ? "Updating" : "Creating" } resource #{title}",
85
89
  :author_name => author_name,
86
90
  :author_email => author_email,
87
91
  :attributes => attributes.to_json(:except => ['author_email', 'author_name', 'commit_message']),
88
92
  :rendered => build_rendered_html
89
93
  })
94
+ @persisted = true
90
95
  else
91
96
  false
92
97
  end
@@ -98,17 +103,22 @@ module Regulate
98
103
  # @raise [Regulate::Git::Errors::PageDoesNotExist] Throw this only if we are attempting to save an existing record and item does not already exist in the repo with the given ID
99
104
  # @raise [Regulate::Git::Errors::InvalidGitResourceError] Record is invalid and cannot save
100
105
  def save!
101
- if !persisted?
102
- # Object create
103
- # A git object with that id already exists
104
- raise Regulate::Git::Errors::DuplicatePageError if self.class.exists?(id)
106
+ if valid?
107
+ if !persisted?
108
+ # Object create
109
+ # A git object with that id already exists
110
+ raise Regulate::Git::Errors::DuplicatePageError if self.class.exists?(id)
111
+ else
112
+ # Object update
113
+ # No git ID with the current id exists
114
+ # You are not allowed to change the ID of an object
115
+ raise Regulate::Git::Errors::PageDoesNotExist if !self.class.exists?(id)
116
+ end
117
+
118
+ save
105
119
  else
106
- # Object update
107
- # No git ID with the current id exists
108
- # You are not allowed to change the ID of an object
109
- raise Regulate::Git::Errors::PageDoesNotExist if !self.class.exists?(id)
120
+ raise Regulate::Git::Errors::InvalidGitResourceError
110
121
  end
111
- ( valid? ) ? save : raise(Regulate::Git::Errors::InvalidGitResourceError)
112
122
  end
113
123
 
114
124
  # Replaces mustache style syntax with appropriate instance attribute values
@@ -187,20 +197,36 @@ module Regulate
187
197
 
188
198
  # Search for by id, and return, a valid resource from the repo
189
199
  #
190
- # @todo Should we be raising an exception here?
191
200
  # @raise [Regulate::Git::Errors::PageDoesNotExist] Raise this if the find fails
192
201
  # @return An instance of whatever class extends this base class
193
202
  def find(id)
203
+ resource_data = Regulate::Git::Interface.find(id)
204
+ self.new_from_git( resource_data )
205
+ end
206
+
207
+ # Search for by id, and return, a valid resource from the repo
208
+ #
209
+ # @raise [Regulate::Git::Errors::PageDoesNotExist] Raise this if the find fails
210
+ # @return An instance of whatever class extends this base class
211
+ def find!(id)
194
212
  resource_data = Regulate::Git::Interface.find(id) || raise(Regulate::Git::Errors::PageDoesNotExist)
195
213
  self.new_from_git( resource_data )
196
214
  end
197
215
 
198
216
  # Search for by version, and return, a valid resource from the repo
199
217
  #
200
- # @todo Should we be raising an exception here?
201
218
  # @raise [Regulate::Git::Errors::PageDoesNotExist] Raise this if the find fails
202
219
  # @return an instance of whatever class extends this base class
203
220
  def find_by_version(id,commit_sha)
221
+ resource_data = Regulate::Git::Interface.find_by_version(id,commit_sha)
222
+ self.new_from_git( resource_data )
223
+ end
224
+
225
+ # Search for by version, and return, a valid resource from the repo
226
+ #
227
+ # @raise [Regulate::Git::Errors::PageDoesNotExist] Raise this if the find fails
228
+ # @return an instance of whatever class extends this base class
229
+ def find_by_version!(id,commit_sha)
204
230
  resource_data = Regulate::Git::Interface.find_by_version(id,commit_sha) || raise(Regulate::Git::Errors::PageDoesNotExist)
205
231
  self.new_from_git( resource_data )
206
232
  end
@@ -216,15 +242,13 @@ module Regulate
216
242
  def create( attributes = {} )
217
243
  temp = self.new(attributes)
218
244
  temp.save
219
- temp_data = Regulate::Git::Interface.find(temp.id)
220
- return self.new_from_git(temp_data)
245
+ return self.find(temp.id)
221
246
  end
222
247
 
223
248
  def create!( attributes = {} )
224
249
  temp = self.new(attributes)
225
250
  temp.save!
226
- temp_data = Regulate::Git::Interface.find(temp.id)
227
- return self.new_from_git(temp_data)
251
+ return self.find!(temp.id)
228
252
  end
229
253
 
230
254
  end
@@ -269,6 +293,7 @@ module Regulate
269
293
 
270
294
  # Accepts a JSON string and creates a new instance of the object
271
295
  def self.new_from_git( resource_data )
296
+ return nil if resource_data.nil?
272
297
  self.new( JSON.parse( resource_data ) , true )
273
298
  end
274
299
 
@@ -1,4 +1,4 @@
1
1
  module Regulate
2
2
  # Our gem version
3
- VERSION = "0.0.1"
3
+ VERSION = "0.1.0"
4
4
  end
@@ -89,7 +89,7 @@
89
89
 
90
90
  label = $("<label>", {
91
91
  'for': 'page[edit_regions][' + new_key + ']',
92
- 'text': new_key.replace('_', " "),
92
+ 'text': new_key.replace(/_/g, " "),
93
93
  'class': new_key
94
94
  });
95
95
 
@@ -0,0 +1,32 @@
1
+ // Triggers an event on an element and returns the event result
2
+ function fire(obj, name, data) {
3
+ var event = new $.Event(name);
4
+ obj.trigger(event, data);
5
+ return event.result !== false;
6
+ }
7
+ function allowAction(element) {
8
+ var message = element.attr('data-confirm');
9
+ return !message || (fire(element, 'confirm') && confirm(message));
10
+ }
11
+ // Handles "data-method" on links such as:
12
+ // <a href="/users/5" data-method="delete" rel="nofollow" data-confirm="Are you sure?">Delete</a>
13
+ function handleMethod(link) {
14
+ var href = link.attr('href'),
15
+ method = link.attr('data-method'),
16
+ csrf_token = $('meta[name=csrf-token]').attr('content'),
17
+ csrf_param = $('meta[name=csrf-param]').attr('content'),
18
+ form = $('<form method="post" action="' + href + '"></form>'),
19
+ metadata_input = '<input name="_method" value="' + method + '" type="hidden" />';
20
+ if (csrf_param !== undefined && csrf_token !== undefined) {
21
+ metadata_input += '<input name="' + csrf_param + '" value="' + csrf_token + '" type="hidden" />';
22
+ }
23
+ form.hide().append(metadata_input).appendTo('body');
24
+ form.submit();
25
+ }
26
+ $('a.delete_link').bind('click',function(e){
27
+ var link = $(this);
28
+ if (!allowAction(link)){ return false; }
29
+ handleMethod(link);
30
+ return false;
31
+ });
32
+
data/regulate.gemspec CHANGED
@@ -16,6 +16,7 @@ Gem::Specification.new do |s|
16
16
 
17
17
  s.add_dependency "rails", "~> 3.0.0"
18
18
  s.add_dependency "grit", "~> 2.3.0"
19
+ s.add_dependency "abstract_auth", "~> 0.1.0"
19
20
  s.add_development_dependency "bluecloth", "~> 2.0.9"
20
21
  s.add_development_dependency "bundler", "~> 1.0.0"
21
22
  s.add_development_dependency "capybara", "~> 0.4.0"
@@ -1,3 +1,9 @@
1
1
  class ApplicationController < ActionController::Base
2
+
2
3
  protect_from_forgery
4
+
5
+ def self.current_user
6
+ User.new
7
+ end
8
+
3
9
  end
@@ -0,0 +1,20 @@
1
+ class User
2
+
3
+ attr_accessor :role , :first_name , :last_name , :email
4
+
5
+ def initialize
6
+ @role = "editor"
7
+ @first_name = "Sir Lucius"
8
+ @last_name = "Leftfoot"
9
+ @email = "sir_lucius@quickleft.com"
10
+ end
11
+
12
+ def is_admin?
13
+ @role == "admin"
14
+ end
15
+
16
+ def is_editor?
17
+ @role == "editor"
18
+ end
19
+
20
+ end
@@ -0,0 +1,14 @@
1
+ # Regulate stuff
2
+
3
+ # AbstractAuth Implementations
4
+ AbstractAuth.implement :authorized_user do
5
+ ::ApplicationController.instance_eval "current_user"
6
+ end
7
+
8
+ AbstractAuth.implement :is_admin do
9
+ ::ApplicationController.instance_eval("current_user").is_admin?
10
+ end
11
+
12
+ AbstractAuth.implement :is_editor do
13
+ ::ApplicationController.instance_eval("current_user").is_editor?
14
+ end
@@ -0,0 +1,15 @@
1
+ require 'test_helper'
2
+ require "rails/generators/test_case"
3
+ require File.join(File.dirname(__FILE__), "../../lib/generators/regulate/mount_up_generator")
4
+
5
+ class MountUpGeneratorTest < Rails::Generators::TestCase
6
+ tests Regulate::Generators::MountUpGenerator
7
+ destination File.expand_path("../tmp", File.dirname(__FILE__))
8
+ setup :prepare_destination
9
+
10
+ test "Assert all files are properly created" do
11
+ run_generator
12
+ assert_file "config/initializers/regulate.rb"
13
+ end
14
+ end
15
+
@@ -0,0 +1,22 @@
1
+ require 'test_helper'
2
+ require "rails/generators/test_case"
3
+ require File.join(File.dirname(__FILE__), "../../lib/generators/regulate/strap_generator")
4
+
5
+ class StrapGeneratorTest < Rails::Generators::TestCase
6
+ tests Regulate::Generators::StrapGenerator
7
+ destination File.expand_path("../tmp", File.dirname(__FILE__))
8
+ setup :prepare_destination
9
+
10
+ test "Assert all files are properly created" do
11
+ run_generator
12
+ assert_file "app/views/regulate/admin/pages/index.html.erb"
13
+ assert_file "app/views/regulate/admin/pages/_form.html.erb"
14
+ assert_file "app/views/regulate/admin/pages/edit.html.erb"
15
+ assert_file "app/views/regulate/admin/pages/new.html.erb"
16
+ assert_file "app/views/regulate/pages/show.html.erb"
17
+ assert_file "public/javascripts/regulate_admin.js"
18
+ assert_file "config/regulate.yml"
19
+ end
20
+ end
21
+
22
+
@@ -2,22 +2,191 @@ require 'test_helper'
2
2
 
3
3
  class Regulate::Git::Model::BaseTest < ActiveSupport::TestCase
4
4
 
5
+ @@iterator = 0
6
+
7
+ def get_iterator
8
+ @@iterator += 1
9
+ @@iterator
10
+ end
11
+
5
12
  def setup
13
+ iterator = get_iterator
6
14
  @resource = Regulate::Git::Model::Base.new({
7
- :title => "Happy Title",
8
- :view => "{{title}}"
15
+ :id => "happy-title-#{iterator}",
16
+ :title => "Happy Title #{iterator}",
17
+ :view => "<h1>{{title}}</h1>"
9
18
  })
10
19
  end
11
20
 
21
+ # /////////
22
+ # Instance Methods
23
+ # /////////
24
+
12
25
  test "new is not persisted" do
13
- assert_equal false , @resource.persisted?
26
+ assert !@resource.persisted?
27
+ end
28
+
29
+ test "resource without id is not valid" do
30
+ @resource.id = nil
31
+ assert !@resource.valid?
32
+ end
33
+
34
+ test "resource save when invalid returns false" do
35
+ @resource.id = nil
36
+ assert !@resource.save
37
+ end
38
+
39
+ test "resource save! when inavlid throws error" do
40
+ @resource.id = nil
41
+ assert_raise Regulate::Git::Errors::InvalidGitResourceError do
42
+ @resource.save!
43
+ end
44
+ end
45
+
46
+ test "can save a resource" do
47
+ assert @resource.save
48
+ end
49
+
50
+ test "resource is persisted after save" do
51
+ @resource.save
52
+ assert @resource.persisted?
14
53
  end
15
54
 
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?
55
+ test "can delete a resource" do
56
+ @resource.save
57
+ assert @resource.destroy
58
+ assert_nil Regulate::Git::Model::Base.find(@resource.id)
20
59
  end
21
60
 
61
+ test "can update attributes of saved resource" do
62
+ @resource.save
63
+ assert @resource.update_attributes({
64
+ :title => "Whoa New Title"
65
+ })
66
+ updated_resource = Regulate::Git::Model::Base.find(@resource.id)
67
+ assert_not_nil updated_resource
68
+ assert_equal "Whoa New Title" , updated_resource.title
69
+ end
70
+
71
+ test "cannot change id when saving resource" do
72
+ @resource.save
73
+ assert !@resource.update_attributes({
74
+ :id => "whoa-new-title",
75
+ :title => "Whoa New Title"
76
+ })
77
+ assert_nil Regulate::Git::Model::Base.find("whoa-new-title")
78
+ end
79
+
80
+ test "can get a list of versions of a resource" do
81
+ @resource.save
82
+ versions = @resource.versions
83
+ assert_equal 1 , versions.length
84
+ assert_kind_of Grit::Commit , versions[0]
85
+ end
86
+
87
+ test "can build the rendered html" do
88
+ assert_equal "<h1>Happy Title #{@@iterator}</h1>" , @resource.build_rendered_html
89
+ assert_equal "<h1>Happy Title #{@@iterator}</h1>" , @resource.rendered
90
+ end
91
+
92
+ test "can grab the rendered html from git" do
93
+ @resource.save
94
+ assert_equal "<h1>Happy Title #{@@iterator}</h1>" , @resource.rendered
95
+ end
96
+
97
+ test "can get our attributes as a hash" do
98
+ assert_kind_of Hash , @resource.attributes
99
+ assert_equal "happy-title-#{@@iterator}" , @resource.attributes["id"]
100
+ assert_equal "Happy Title #{@@iterator}" , @resource.attributes["title"]
101
+ assert_equal "<h1>{{title}}</h1>" , @resource.attributes["view"]
102
+ end
103
+
104
+ # /////////
105
+ # End Instance Methods
106
+ # /////////
107
+
108
+ # /////////
109
+ # Class Methods
110
+ # /////////
111
+
112
+ test "can setup class level attributes" do
113
+ class ::RandomTestClass < Regulate::Git::Model::Base
114
+ attributes :a_random_attribute
115
+ end
116
+ assert RandomTestClass._attributes.include?(:a_random_attribute)
117
+ x = RandomTestClass.new
118
+ assert_respond_to x , :a_random_attribute
119
+ assert_respond_to x , :a_random_attribute=
120
+ assert_respond_to x , :a_random_attribute?
121
+ end
122
+
123
+ test "can check if a resource with the given id exists" do
124
+ @resource.save
125
+ assert Regulate::Git::Model::Base.exists?(@resource.id)
126
+ end
127
+
128
+ test "can find a git resource given an id" do
129
+ @resource.save
130
+ found = Regulate::Git::Model::Base.find(@resource.id)
131
+ assert_not_nil found
132
+ assert_kind_of Regulate::Git::Model::Base , found
133
+ end
134
+
135
+ test "find will return nil if nothing is found" do
136
+ assert_nil Regulate::Git::Model::Base.find("a-random-id")
137
+ end
138
+
139
+ test "find! will raise an exception if nothing is found" do
140
+ assert_raise Regulate::Git::Errors::PageDoesNotExist do
141
+ Regulate::Git::Model::Base.find!("a-random-id")
142
+ end
143
+ end
144
+
145
+ test "can find a version of a git resource by id and sha" do
146
+ @resource.save
147
+ @resource.title = "Oh Hey a new TItle!"
148
+ @resource.save
149
+ versions = @resource.versions
150
+ assert_equal 2 , versions.length
151
+ old_version = Regulate::Git::Model::Base.find_by_version(@resource.id,versions[1].sha)
152
+ assert_not_nil old_version
153
+ assert_equal "Happy Title #{@@iterator}" , old_version.title
154
+ end
155
+
156
+ test "find_by_versions! will raise an exception if nothing is found" do
157
+ assert_raise Regulate::Git::Errors::PageDoesNotExist do
158
+ Regulate::Git::Model::Base.find_by_version!("a-random-id","2798hjfsdjfgk25jklj")
159
+ end
160
+ end
161
+
162
+ test "find_all returns all resources in the repo" do
163
+ all = Regulate::Git::Model::Base.find_all
164
+ assert_operator 1 , :< , all.length
165
+ assert_kind_of Regulate::Git::Model::Base , all[0]
166
+ end
167
+
168
+ test "can create with attributes hash" do
169
+ new_resource = Regulate::Git::Model::Base.create({
170
+ :id => "happy-title-#{@@iterator}",
171
+ :title => "Happy Title #{@@iterator}",
172
+ :view => "<h1>{{title}}</h1>"
173
+ })
174
+ assert_not_nil new_resource
175
+ assert_kind_of Regulate::Git::Model::Base , new_resource
176
+ assert new_resource.persisted?
177
+ end
178
+
179
+ test "create will return nil" do
180
+ new_resource = Regulate::Git::Model::Base.create({
181
+ :title => "Happy Title #{@@iterator}",
182
+ :view => "<h1>{{title}}</h1>"
183
+ })
184
+ assert_nil new_resource
185
+ end
186
+
187
+ # /////////
188
+ # End Class Methods
189
+ # /////////
190
+
22
191
  end
23
192