Permalink
Browse files

major refactoring to work standalone

  • Loading branch information...
1 parent 9fe21e2 commit ed31da5a5878dbe6dc8ed35358b3e01666849d1a @kristianmandrup committed Nov 26, 2010
View
@@ -12,16 +12,17 @@ Alternatively insert into Rails 3 Gemfile:
gem 'devise-links'
</pre>
-and from the terminal run: *bundle install*
+Run <code>$ bundle install</code> in terminal from root of Rails app
## Usage
<pre>
- <%= new_registration_link(:role => :admin) %>
-
+ <%= new_registration_link(:role => :admin) %>
<%= sign_out_link(:role => :admin, :label => 'Log me out!') %>
</pre>
+See specs for demonstrations of how to use the API etc.
+
## TODO
Make API more generic...
View
@@ -8,12 +8,17 @@ begin
gem.homepage = "http://github.com/kristianmandrup/devise-links"
gem.authors = ["Kristian Mandrup"]
gem.add_development_dependency "rspec", ">= 2.0.0"
- gem.add_development_dependency "rspec-action_view", "~> 0.3.1"
+ gem.add_development_dependency "rspec-action_view", "~> 0.3.4"
+ gem.add_development_dependency 'generator-spec', ">= 0.7.0"
gem.add_dependency "devise", ">= 1.2"
+ gem.add_dependency 'activesupport', ">= 3.0.0"
gem.add_dependency 'require_all', "~> 1.2.0"
- gem.add_dependency 'sugar-high', "~> 0.3.0"
- gem.add_dependency 'r3_plugin_toolbox', "~> 0.4.0"
+ gem.add_dependency 'sugar-high', "~> 0.3.0"
+ gem.add_dependency 'r3_plugin_toolbox', ">= 0.4.0"
+ gem.add_dependency 'rails3_artifactor', ">= 0.3.1"
+ gem.add_dependency 'logging_assist', "~> 0.1.6"
+
end
Jeweler::GemcutterTasks.new
rescue LoadError
View
@@ -9,7 +9,7 @@ Gem::Specification.new do |s|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Kristian Mandrup"]
- s.date = %q{2010-10-07}
+ s.date = %q{2010-10-28}
s.description = %q{Link helpers for Devise actions, including user session and registration links.}
s.email = %q{kmandrup@gmail.com}
s.extra_rdoc_files = [
@@ -52,27 +52,27 @@ Gem::Specification.new do |s|
s.specification_version = 3
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
- s.add_development_dependency(%q<rspec>, [">= 2.0.0.rc"])
+ s.add_development_dependency(%q<rspec>, [">= 2.0.0"])
s.add_development_dependency(%q<rspec-action_view>, ["~> 0.3.1"])
- s.add_runtime_dependency(%q<devise>, [">= 1.1.2"])
+ s.add_runtime_dependency(%q<devise>, [">= 1.2"])
s.add_runtime_dependency(%q<require_all>, ["~> 1.2.0"])
- s.add_runtime_dependency(%q<sugar-high>, ["~> 0.2.10"])
- s.add_runtime_dependency(%q<r3_plugin_toolbox>, ["~> 0.3.6"])
+ s.add_runtime_dependency(%q<sugar-high>, ["~> 0.3.0"])
+ s.add_runtime_dependency(%q<r3_plugin_toolbox>, ["~> 0.4.0"])
else
- s.add_dependency(%q<rspec>, [">= 2.0.0.rc"])
+ s.add_dependency(%q<rspec>, [">= 2.0.0"])
s.add_dependency(%q<rspec-action_view>, ["~> 0.3.1"])
- s.add_dependency(%q<devise>, [">= 1.1.2"])
+ s.add_dependency(%q<devise>, [">= 1.2"])
s.add_dependency(%q<require_all>, ["~> 1.2.0"])
- s.add_dependency(%q<sugar-high>, ["~> 0.2.10"])
- s.add_dependency(%q<r3_plugin_toolbox>, ["~> 0.3.6"])
+ s.add_dependency(%q<sugar-high>, ["~> 0.3.0"])
+ s.add_dependency(%q<r3_plugin_toolbox>, ["~> 0.4.0"])
end
else
- s.add_dependency(%q<rspec>, [">= 2.0.0.rc"])
+ s.add_dependency(%q<rspec>, [">= 2.0.0"])
s.add_dependency(%q<rspec-action_view>, ["~> 0.3.1"])
- s.add_dependency(%q<devise>, [">= 1.1.2"])
+ s.add_dependency(%q<devise>, [">= 1.2"])
s.add_dependency(%q<require_all>, ["~> 1.2.0"])
- s.add_dependency(%q<sugar-high>, ["~> 0.2.10"])
- s.add_dependency(%q<r3_plugin_toolbox>, ["~> 0.3.6"])
+ s.add_dependency(%q<sugar-high>, ["~> 0.3.0"])
+ s.add_dependency(%q<r3_plugin_toolbox>, ["~> 0.4.0"])
end
end
View
@@ -2,6 +2,10 @@
require 'sugar-high/module'
require 'sugar-high/alias'
require 'devise-links/namespaces'
-require_all File.dirname(__FILE__) + '/devise-links/link'
-require_all File.dirname(__FILE__) + '/devise-links/rails'
+require 'devise-links/registration'
+require 'devise-links/session'
+
+if defined? Rails
+ require 'devise-links/rails/configure'
+end
View
@@ -0,0 +1,52 @@
+module Devise::Links
+ class << self
+ def labels
+ @lbs ||= translate_labels? ? translate_labels : default_labels
+ end
+
+ attr_accessor :translate
+
+ def translate_labels?
+ translate
+ end
+
+ def auth_labels
+ %w{sign_in sign_out sign_up edit_registration}
+ end
+
+ protected
+
+ def default_labels
+ auth_labels.inject({}) {|result, action|
+ result[action.to_sym] = action.to_s.humanize
+ result
+ }
+ end
+
+ def translate_labels
+ ns_actions = 'cream.actions.auth'
+ auth_labels.inject({}) {|result, action|
+ result[action.to_sym] = t("#{ns_actions}.#{action}").humanize
+ result
+ }
+ end
+ end
+
+ module Labels
+ # create method :new_label, :index_label and so on ...
+ Devise::Links.auth_labels.each do |name|
+ class_eval %{
+ def #{name}_label
+ get_label :#{name}
+ end
+ }
+ end
+
+ alias_method :new_registration_label, :sign_up_label
+
+ # how to retrieve label in single location!
+ def get_label key
+ Devise::Links.labels[key]
+ end
+ end
+end
@@ -3,7 +3,7 @@
Rails3::Plugin::Extender.new do
extend_rails :view do
- extend_from_module Devise::Link, :registration, :session
+ extend_from_module Devise::Links, :registration, :session, :labels
end
end
@@ -1,6 +1,7 @@
-module Devise::Link
- module Registration
-
+require 'devise-links/labels'
+
+module Devise::Links
+ module Registration
REGISTRATION_LINKS = {
:new_registration => :sign_up,
:edit_registration => :edit_profile
@@ -12,8 +13,8 @@ module Registration
REGISTRATION_LINKS.keys.each do |name|
class_eval %{
def #{name}_link(options = {})
- label = options[:label] || auth_labels[:#{name}]
- path = #{name}_path options[:role]
+ label = options[:label] || #{name}_label
+ path = #{name}_path options[:role]
link_to(label, path)
end
}
@@ -1,4 +1,6 @@
-module Devise::Link
+require 'devise-links/labels'
+
+module Devise::Links
module Session
SESSION_LINKS = {
:sign_out => :log_out,
@@ -11,7 +13,7 @@ module Session
SESSION_LINKS.keys.each_with_index do |name, index|
class_eval %{
def #{name}_link(options = {})
- label = options[:label] || auth_labels[:#{name}]
+ label = options[:label] || #{name}_label
path = #{ACTIONS[index]}_session_path options[:role]
link_to(label, path)
end
@@ -0,0 +1,28 @@
+require 'rails/generators/base'
+require 'sugar-high/array'
+require 'active_support/inflector'
+require 'rails3_artifactor'
+require 'logging_assist'
+
+module Devise
+ module Generators
+ class LinksGenerator < Rails::Generators::Base
+ desc "Configures Rails app for use with Devise Links"
+
+ source_root File.dirname(__FILE__) + '/templates'
+
+ def main_flow
+ copy_locale
+ end
+
+ protected
+
+ include Rails3::Assist::BasicLogger
+ # extend Rails3::Assist::UseMacro
+
+ def copy_locale
+ template "devise_links.en.yml", "config/locales/cream.en.yml"
+ end
+ end
+ end
+end
@@ -0,0 +1,9 @@
+en:
+ cream:
+ confirm: 'Are you sure?'
+ actions:
+ auth:
+ sign_in: "Sign in"
+ sign_out: "Sign out"
+ sign_up: "Sign up"
+ edit_registration: "Edit registration"
View
No changes.
@@ -0,0 +1,34 @@
+require 'spec_helper'
+require 'generator-spec'
+
+require_generator :devise => :links
+
+RSpec::Generator.configure do |config|
+ config.debug = true
+ config.remove_temp_dir = true
+ config.default_rails_root(__FILE__)
+ config.lib = File.dirname(__FILE__) + '/../lib'
+ config.logger = :stdout # :file
+end
+
+
+describe 'Restlinks generator' do
+ use_helpers :special, :file
+
+ setup_generator :devise_links do
+ tests Devise::Generators::LinksGenerator
+ end
+
+ describe "Run it!" do
+ before :each do
+ @generator = with_generator do |g|
+ g.run_generator
+ end
+ end
+
+ it "should have created a locales file for CanCan REST links" do
+ # Rails3::Assist::Files.locale_files.file_names.should include('cream.en.yml')
+ @generator.should have_locale_file 'cream.en'
+ end
+ end
+end
@@ -1,25 +0,0 @@
-require 'spec_helper'
-require 'devise-links/link/registration'
-
-describe Devise::Link::Registration do
-
- extend_view_with Devise::Link::Registration
-
- describe '#new_registration_link' do
- it "should create a registration link" do
- with_engine do |e, view|
- label = 'new registration'
- path = 'new/reg/path'
- view.stubs(:auth_labels).returns(:new_registration => label)
- view.stubs(:user_reg_path).with(:new, :admin).returns path
- view.stubs(:link_to).with(label, path).returns 'it works'
-
- res = e.run_template do
- %{<%= new_registration_link(:role => :admin) %> }
- end
- res.should match /it works/
- end
- end
- end
-end
-
@@ -0,0 +1,31 @@
+require 'spec_helper'
+require 'devise-links/links/registration'
+require 'devise-links/links/labels'
+
+describe Devise::Links::Registration do
+
+ extend_view_with Devise::Links::Registration
+ extend_view_with Devise::Links::Labels
+
+ describe '#new_registration_link' do
+ it "should create a registration link" do
+ view_engine do |e, view|
+ label = 'new registration'
+ path = 'new/reg/path'
+
+ # view.stubs(:user_reg_path).with(:new, :admin).returns path
+ view.stubs(:new_registration_path).returns path
+
+ output_label = view.new_registration_label
+
+ view.stubs(:link_to).returns output_label
+
+ res = e.run_template do
+ %{<%= new_registration_link(:role => :admin) %> }
+ end
+ res.should match /#{output_label}/
+ end
+ end
+ end
+end
+
@@ -1,23 +1,25 @@
require 'spec_helper'
-require 'devise-links/link/session'
+require 'devise-links/links/session'
+require 'devise-links/links/labels'
-describe Devise::Link::Session do
+describe Devise::Links::Session do
- extend_view_with Devise::Link::Session
+ extend_view_with Devise::Links::Session
+ extend_view_with Devise::Links::Labels
describe '#sign_out_link' do
it "should create a sign out link" do
- with_engine do |e, view|
+ view_engine do |e, view|
label = 'log out'
path = 'admin/log/out'
- view.stubs(:auth_labels).returns(:sign_out => label)
view.stubs(:destroy_session_path).with(:admin).returns path
- view.stubs(:link_to).with(label, path).returns 'it works'
+ output_label = view.sign_out_label
+ view.stubs(:link_to).returns output_label
res = e.run_template do
%{<%= sign_out_link(:role => :admin) %> }
end
- res.should match /it works/
+ res.should match /#{output_label}/
end
end
end

0 comments on commit ed31da5

Please sign in to comment.