governor 0.3.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -11,7 +11,6 @@ group :development, :test do
11
11
  gem 'factory_girl_rails', '~> 1.1.beta'
12
12
  gem 'activerecord-nulldb-adapter'
13
13
 
14
- gem 'will_paginate', '~> 3.0.beta'
15
14
  gem 'devise'
16
15
  gem 'governor', :path => './'
17
16
  gem 'dynamic_form'
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ./
3
3
  specs:
4
- governor (0.3.0)
4
+ governor (0.3.1)
5
5
  rails (~> 3.0.5)
6
6
 
7
7
  GEM
@@ -103,7 +103,6 @@ GEM
103
103
  tzinfo (0.3.24)
104
104
  warden (1.0.3)
105
105
  rack (>= 1.0.0)
106
- will_paginate (3.0.pre2)
107
106
 
108
107
  PLATFORMS
109
108
  ruby
@@ -120,4 +119,3 @@ DEPENDENCIES
120
119
  rails (~> 3.0.5)
121
120
  rspec-rails
122
121
  sqlite3
123
- will_paginate (~> 3.0.beta)
data/README.rdoc CHANGED
@@ -11,10 +11,9 @@ try the specs, they're fresh!
11
11
  == Dependencies
12
12
 
13
13
  * ActiveRecord
14
- * will_paginate[https://github.com/mislav/will_paginate]
15
14
 
16
- At some point (at least by v1.0), these dependencies will be removed. Sorry
17
- about the meantime.
15
+ By 1.0, I'd like to make it possible to use Governor with other ORMs, but I'm
16
+ not sure when that'll be.
18
17
 
19
18
  == Setting Up
20
19
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.1
1
+ 0.4.0
@@ -15,7 +15,11 @@ class Governor::ArticlesController < ApplicationController
15
15
  # GET /articles
16
16
  # GET /articles.xml
17
17
  def index
18
- set_resources model_class.paginate :page => params[:page], :order => 'created_at DESC'
18
+ set_resources(if model_class.respond_to?(:paginate)
19
+ model_class.paginate :page => params[:page], :order => 'created_at DESC'
20
+ else
21
+ model_class.all :order => 'created_at DESC'
22
+ end)
19
23
  respond_with resources
20
24
  end
21
25
 
@@ -43,7 +47,7 @@ class Governor::ArticlesController < ApplicationController
43
47
  set_resource model_class.new(params[mapping.singular])
44
48
  resource.author = the_governor
45
49
  if resource.save
46
- flash[:notice] = "#{mapping.humanize} was successfully created."
50
+ flash[:notice] = t('governor.article_created', :resource_type => mapping.humanize)
47
51
  end
48
52
  respond_with resource
49
53
  end
@@ -52,7 +56,7 @@ class Governor::ArticlesController < ApplicationController
52
56
  # PUT /articles/1.xml
53
57
  def update
54
58
  if resource.update_attributes(params[mapping.singular])
55
- flash[:notice] = "#{mapping.humanize} was successfully updated."
59
+ flash[:notice] = t('governor.article_updated', :resource_type => mapping.humanize)
56
60
  end
57
61
  respond_with resource
58
62
  end
@@ -61,7 +65,7 @@ class Governor::ArticlesController < ApplicationController
61
65
  # DELETE /articles/1.xml
62
66
  def destroy
63
67
  resource.destroy
64
- flash[:notice] = "#{mapping.humanize} was successfully updated."
68
+ flash[:notice] = t('governor.article_destroyed', :resource_type => mapping.humanize)
65
69
  respond_with resource
66
70
  end
67
71
 
@@ -1,6 +1,4 @@
1
1
  module GovernorHelper
2
- @@months = %w(January February March April May June July August September October November December)
3
-
4
2
  Governor::PluginManager.plugins.map{|p| p.helpers }.flatten.each do |mod|
5
3
  include mod.constantize # FIXME this feels pretty dirty, there has to be a better way
6
4
  end
@@ -14,14 +12,8 @@ module GovernorHelper
14
12
  return output.html_safe
15
13
  end
16
14
 
17
- def get_date_label
18
- if not params[:day].nil?
19
- "#{@@months[params[:month].to_i - 1]} #{params[:day]}, #{params[:year]}"
20
- elsif not params[:month].nil?
21
- "#{@@months[params[:month].to_i - 1]} #{params[:year]}"
22
- else
23
- params[:year]
24
- end
15
+ def get_date_label(year, month=nil, day=nil)
16
+ l Date.new(*[year, month, day].compact), :format => "#{'%B ' if month}#{'%d, ' if day}%Y"
25
17
  end
26
18
 
27
19
  def show_time_ago(date)
@@ -2,5 +2,5 @@
2
2
  <%= render :partial => 'article', :collection => resources %>
3
3
 
4
4
  <p>
5
- <%= will_paginate resources %>
5
+ <%= will_paginate(resources) if respond_to?(:will_paginate) %>
6
6
  </p>
@@ -1,6 +1,6 @@
1
- # Sample localization file for English. Add more files in this directory for other locales.
2
- # See http://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points.
3
-
4
1
  en:
5
- hello: "Hello world"
6
- unauthorized_edit: "You're not allowed to edit this %{resource_type}."
2
+ governor:
3
+ article_created: "%{resource_type} was successfully created."
4
+ article_updated: "%{resource_type} was successfully updated."
5
+ article_destroyed: "%{resource_type} was successfully deleted."
6
+ unauthorized_edit: "You're not allowed to edit this %{resource_type}."
data/governor.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{governor}
8
- s.version = "0.3.1"
8
+ s.version = "0.4.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Liam Morley"]
12
- s.date = %q{2011-04-24}
12
+ s.date = %q{2011-04-28}
13
13
  s.description = %q{Because Blogojevich would be too tough to remember. It's a pluggable blogging system for Rails 3.}
14
14
  s.email = %q{liam@carpeliam.com}
15
15
  s.extra_rdoc_files = [
@@ -37,8 +37,8 @@ Gem::Specification.new do |s|
37
37
  "config/locales/en.yml",
38
38
  "governor.gemspec",
39
39
  "lib/generators/USAGE",
40
+ "lib/generators/governor/configure_generator.rb",
40
41
  "lib/generators/governor/create_articles_generator.rb",
41
- "lib/generators/governor/install_generator.rb",
42
42
  "lib/generators/governor/migrate_generator.rb",
43
43
  "lib/generators/governor/templates/governor.rb",
44
44
  "lib/generators/governor/templates/migrations/create_articles.rb",
@@ -171,7 +171,6 @@ Gem::Specification.new do |s|
171
171
  s.add_development_dependency(%q<factory_girl>, ["~> 2.0.0.beta"])
172
172
  s.add_development_dependency(%q<factory_girl_rails>, ["~> 1.1.beta"])
173
173
  s.add_development_dependency(%q<activerecord-nulldb-adapter>, [">= 0"])
174
- s.add_development_dependency(%q<will_paginate>, ["~> 3.0.beta"])
175
174
  s.add_development_dependency(%q<devise>, [">= 0"])
176
175
  s.add_development_dependency(%q<governor>, [">= 0"])
177
176
  s.add_development_dependency(%q<dynamic_form>, [">= 0"])
@@ -184,7 +183,6 @@ Gem::Specification.new do |s|
184
183
  s.add_dependency(%q<factory_girl>, ["~> 2.0.0.beta"])
185
184
  s.add_dependency(%q<factory_girl_rails>, ["~> 1.1.beta"])
186
185
  s.add_dependency(%q<activerecord-nulldb-adapter>, [">= 0"])
187
- s.add_dependency(%q<will_paginate>, ["~> 3.0.beta"])
188
186
  s.add_dependency(%q<devise>, [">= 0"])
189
187
  s.add_dependency(%q<governor>, [">= 0"])
190
188
  s.add_dependency(%q<dynamic_form>, [">= 0"])
@@ -198,7 +196,6 @@ Gem::Specification.new do |s|
198
196
  s.add_dependency(%q<factory_girl>, ["~> 2.0.0.beta"])
199
197
  s.add_dependency(%q<factory_girl_rails>, ["~> 1.1.beta"])
200
198
  s.add_dependency(%q<activerecord-nulldb-adapter>, [">= 0"])
201
- s.add_dependency(%q<will_paginate>, ["~> 3.0.beta"])
202
199
  s.add_dependency(%q<devise>, [">= 0"])
203
200
  s.add_dependency(%q<governor>, [">= 0"])
204
201
  s.add_dependency(%q<dynamic_form>, [">= 0"])
@@ -1,5 +1,5 @@
1
1
  module Governor
2
- class InstallGenerator < Rails::Generators::Base
2
+ class ConfigureGenerator < Rails::Generators::Base
3
3
  source_root File.expand_path('../templates', __FILE__)
4
4
 
5
5
  def copy_initializer
@@ -2,10 +2,12 @@
2
2
 
3
3
  # How to reference the author of an article
4
4
  Governor.author = Proc.new do
5
- if respond_to?(:current_user)
5
+ if defined?(Devise)
6
+ send("current_#{Devise.default_scope}")
7
+ elsif respond_to?(:current_user)
6
8
  current_user
7
9
  else
8
- raise "Set up Governor.author in #{File.expand_path(__FILE__)}"
10
+ raise "Please define Governor.author. Run `rails generator governor:configure` to install an initializer."
9
11
  end
10
12
  end
11
13
 
@@ -13,10 +15,14 @@ end
13
15
  Governor.authorize_if do |action, article|
14
16
  case action.to_sym
15
17
  when :new, :create
16
- if respond_to?(:user_signed_in?)
17
- user_signed_in?
18
+ if defined?(Devise) && respond_to?("#{Devise.default_scope}_signed_in?")
19
+ send("#{Devise.default_scope}_signed_in?")
20
+ elsif respond_to?(:signed_in?)
21
+ signed_in?
22
+ elsif respond_to?(:current_user)
23
+ current_user.present?
18
24
  else
19
- raise "Set up Governor.authorize_if in #{File.expand_path(__FILE__)}"
25
+ raise "Please define Governor.authorize_if. Run `rails generator governor:configure` to install an initializer."
20
26
  end
21
27
  when :edit, :update, :destroy
22
28
  article.author == instance_eval(&Governor.author)
@@ -28,9 +34,11 @@ end
28
34
  # What should Governor do if someone tries to do something they weren't
29
35
  # authorized to do?
30
36
  Governor.if_not_allowed do
31
- if respond_to?(Devise)
37
+ if defined?(Devise)
32
38
  send("authenticate_#{Devise.default_scope}!")
39
+ elsif respond_to?(:deny_access)
40
+ deny_access
33
41
  else
34
- raise ArgumentError.new("Set up Governor.if_not_allowed in #{File.expand_path(__FILE__)}")
42
+ redirect_to :root
35
43
  end
36
44
  end
data/lib/governor.rb CHANGED
@@ -16,7 +16,8 @@ module Governor
16
16
 
17
17
  # Specifies the action that should occur if an unpermitted action is
18
18
  # attempted. Usually specified within the initializer, though evaluated in
19
- # the scope of a Rails session.
19
+ # the scope of a Rails session. If not defined, the default is to redirect
20
+ # to the root path.
20
21
  #
21
22
  # Example:
22
23
  #
@@ -29,6 +30,15 @@ module Governor
29
30
  # end
30
31
  #
31
32
  mattr_accessor :if_not_allowed
33
+ Governor.if_not_allowed do
34
+ if defined?(Devise)
35
+ send("authenticate_#{Devise.default_scope}!")
36
+ elsif respond_to?(:deny_access)
37
+ deny_access
38
+ else
39
+ redirect_to :root
40
+ end
41
+ end
32
42
 
33
43
  # Specifies how to reference the author of an article. Usually specified
34
44
  # within the initializer, though evaluated in the scope of a Rails session.
@@ -44,6 +54,15 @@ module Governor
44
54
  # end
45
55
  #
46
56
  mattr_accessor :author
57
+ Governor.author = Proc.new do
58
+ if defined?(Devise)
59
+ send("current_#{Devise.default_scope}")
60
+ elsif respond_to?(:current_user)
61
+ current_user
62
+ else
63
+ raise "Please define Governor.author. Run `rails generator governor:configure` to install an initializer."
64
+ end
65
+ end
47
66
 
48
67
  # Maps a given resource name with a pair of options, to be supplied as
49
68
  # arguments to the routes. Usually called from within +governate+.
@@ -75,4 +94,22 @@ module Governor
75
94
  def self.authorize_if(&blk)
76
95
  @@authorization_rules = blk
77
96
  end
97
+ Governor.authorize_if do |action, article|
98
+ case action.to_sym
99
+ when :new, :create
100
+ if defined?(Devise) && respond_to?("#{Devise.default_scope}_signed_in?")
101
+ send("#{Devise.default_scope}_signed_in?")
102
+ elsif respond_to?(:signed_in?)
103
+ signed_in?
104
+ elsif respond_to?(:current_user)
105
+ current_user.present?
106
+ else
107
+ raise "Please define Governor.authorize_if. Run `rails generator governor:configure` to install an initializer."
108
+ end
109
+ when :edit, :update, :destroy
110
+ article.author == instance_eval(&Governor.author)
111
+ else
112
+ raise ArgumentError.new('action must be new, create, edit, update, or destroy')
113
+ end
114
+ end
78
115
  end
@@ -6,6 +6,11 @@ module Governor
6
6
  base.belongs_to :author, :polymorphic => true
7
7
  base.validates_presence_of :author, :title, :post
8
8
 
9
+ if defined?(WillPaginate)
10
+ base.cattr_reader :per_page
11
+ base.class_eval "@@per_page = 10"
12
+ end
13
+
9
14
  Governor::PluginManager.plugins.each do |plugin|
10
15
  plugin.include_in_model(base)
11
16
  end
@@ -17,7 +22,11 @@ module Governor
17
22
  def base.find_all_by_date(year, month = nil, day = nil, page = 1)
18
23
  from, to = self.time_delta(year, month, day)
19
24
  conditions = ['created_at BETWEEN ? AND ?', from, to]
20
- paginate :page => page, :conditions => conditions, :order => 'created_at DESC'
25
+ if model_class.respond_to?(:paginate)
26
+ paginate :page => page, :conditions => conditions, :order => 'created_at DESC'
27
+ else
28
+ all :conditions => conditions, :order => 'created_at DESC'
29
+ end
21
30
  end
22
31
 
23
32
  private
@@ -109,6 +109,7 @@ module Governor
109
109
  assigns[:article].should_not be_a_new_record
110
110
  assigns[:article].author.should == @user
111
111
  response.should redirect_to(assigns[:article])
112
+ flash[:notice].should == 'Article was successfully created.'
112
113
  end
113
114
  end
114
115
  end
@@ -126,6 +127,7 @@ module Governor
126
127
  it "updates the article" do
127
128
  put :update, :governor_mapping => :articles, :id => @article.id, :article => {:title => 'I am awesome, you are awesome'}
128
129
  assigns[:article].title.should == 'I am awesome, you are awesome'
130
+ flash[:notice].should == 'Article was successfully updated.'
129
131
  end
130
132
  end
131
133
  end
@@ -146,6 +148,7 @@ module Governor
146
148
  assigns[:article].should be_destroyed
147
149
  ::Article.count.should == 0
148
150
  response.should redirect_to(articles_path)
151
+ flash[:notice].should == 'Article was successfully deleted, and this is a custom localized message.'
149
152
  end
150
153
  end
151
154
  end
@@ -12,8 +12,6 @@ gem 'governor', :path => '../..'
12
12
 
13
13
  gem 'dynamic_form'
14
14
 
15
- gem 'will_paginate', '~> 3.0.beta'
16
-
17
15
  # Use unicorn as the web server
18
16
  # gem 'unicorn'
19
17
 
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ../..
3
3
  specs:
4
- governor (0.3.0)
4
+ governor (0.3.1)
5
5
  rails (~> 3.0.5)
6
6
 
7
7
  GEM
@@ -77,7 +77,6 @@ GEM
77
77
  tzinfo (0.3.25)
78
78
  warden (1.0.3)
79
79
  rack (>= 1.0.0)
80
- will_paginate (3.0.pre2)
81
80
 
82
81
  PLATFORMS
83
82
  ruby
@@ -88,4 +87,3 @@ DEPENDENCIES
88
87
  governor!
89
88
  rails (= 3.0.5)
90
89
  sqlite3
91
- will_paginate (~> 3.0.beta)
@@ -1,5 +1,3 @@
1
1
  class Article < ActiveRecord::Base
2
2
  include Governor::Article
3
- cattr_reader :per_page
4
- @@per_page = 10
5
3
  end
@@ -2,4 +2,6 @@
2
2
  # See http://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points.
3
3
 
4
4
  en:
5
+ governor:
6
+ article_destroyed: "%{resource_type} was successfully deleted, and this is a custom localized message."
5
7
  hello: "Hello world"
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: governor
3
3
  version: !ruby/object:Gem::Version
4
- hash: 17
4
+ hash: 15
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 3
9
- - 1
10
- version: 0.3.1
8
+ - 4
9
+ - 0
10
+ version: 0.4.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Liam Morley
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-04-24 00:00:00 -04:00
18
+ date: 2011-04-28 00:00:00 -04:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -139,27 +139,11 @@ dependencies:
139
139
  - 0
140
140
  version: "0"
141
141
  requirement: *id008
142
- - !ruby/object:Gem::Dependency
143
- type: :development
144
- prerelease: false
145
- name: will_paginate
146
- version_requirements: &id009 !ruby/object:Gem::Requirement
147
- none: false
148
- requirements:
149
- - - ~>
150
- - !ruby/object:Gem::Version
151
- hash: 31098121
152
- segments:
153
- - 3
154
- - 0
155
- - beta
156
- version: 3.0.beta
157
- requirement: *id009
158
142
  - !ruby/object:Gem::Dependency
159
143
  type: :development
160
144
  prerelease: false
161
145
  name: devise
162
- version_requirements: &id010 !ruby/object:Gem::Requirement
146
+ version_requirements: &id009 !ruby/object:Gem::Requirement
163
147
  none: false
164
148
  requirements:
165
149
  - - ">="
@@ -168,12 +152,12 @@ dependencies:
168
152
  segments:
169
153
  - 0
170
154
  version: "0"
171
- requirement: *id010
155
+ requirement: *id009
172
156
  - !ruby/object:Gem::Dependency
173
157
  type: :development
174
158
  prerelease: false
175
159
  name: governor
176
- version_requirements: &id011 !ruby/object:Gem::Requirement
160
+ version_requirements: &id010 !ruby/object:Gem::Requirement
177
161
  none: false
178
162
  requirements:
179
163
  - - ">="
@@ -182,12 +166,12 @@ dependencies:
182
166
  segments:
183
167
  - 0
184
168
  version: "0"
185
- requirement: *id011
169
+ requirement: *id010
186
170
  - !ruby/object:Gem::Dependency
187
171
  type: :development
188
172
  prerelease: false
189
173
  name: dynamic_form
190
- version_requirements: &id012 !ruby/object:Gem::Requirement
174
+ version_requirements: &id011 !ruby/object:Gem::Requirement
191
175
  none: false
192
176
  requirements:
193
177
  - - ">="
@@ -196,7 +180,7 @@ dependencies:
196
180
  segments:
197
181
  - 0
198
182
  version: "0"
199
- requirement: *id012
183
+ requirement: *id011
200
184
  description: Because Blogojevich would be too tough to remember. It's a pluggable blogging system for Rails 3.
201
185
  email: liam@carpeliam.com
202
186
  executables: []
@@ -227,8 +211,8 @@ files:
227
211
  - config/locales/en.yml
228
212
  - governor.gemspec
229
213
  - lib/generators/USAGE
214
+ - lib/generators/governor/configure_generator.rb
230
215
  - lib/generators/governor/create_articles_generator.rb
231
- - lib/generators/governor/install_generator.rb
232
216
  - lib/generators/governor/migrate_generator.rb
233
217
  - lib/generators/governor/templates/governor.rb
234
218
  - lib/generators/governor/templates/migrations/create_articles.rb