Permalink
Browse files

Converted api extension into Rails3 engine.

  • Loading branch information...
1 parent 35388a7 commit fcf13cc0619be813a64ec4aab3c078636d8f0d81 @schof schof committed Jun 22, 2010
Showing with 188 additions and 249 deletions.
  1. 0 vendor/extensions/api/README.markdown → api/README.md
  2. +62 −0 api/Rakefile
  3. 0 {vendor/extensions → }/api/app/controllers/api/base_controller.rb
  4. 0 {vendor/extensions → }/api/app/controllers/api/countries_controller.rb
  5. 0 {vendor/extensions → }/api/app/controllers/api/inventory_units_controller.rb
  6. 0 {vendor/extensions → }/api/app/controllers/api/line_items_controller.rb
  7. 0 {vendor/extensions → }/api/app/controllers/api/orders_controller.rb
  8. 0 {vendor/extensions → }/api/app/controllers/api/products_controller.rb
  9. 0 {vendor/extensions → }/api/app/controllers/api/shipments_controller.rb
  10. 0 {vendor/extensions → }/api/app/controllers/api/states_controller.rb
  11. 0 {vendor/extensions → }/api/app/helpers/api/shipments_helper.rb
  12. 0 {vendor/extensions → }/api/app/views/admin/users/_api_fields.html.erb
  13. 0 {vendor/extensions → }/api/config/locales/en.yml
  14. 0 {vendor/extensions → }/api/config/locales/ru-RU.yml
  15. +36 −0 api/config/routes.rb
  16. 0 {vendor/extensions → }/api/db/migrate/20100107141738_add_api_key_to_users.rb
  17. 0 {vendor/extensions → }/api/db/seeds.rb
  18. +59 −0 api/lib/spree_api.rb
  19. +6 −0 api/lib/spree_api/engine.rb
  20. 0 vendor/extensions/api/api_hooks.rb → api/lib/spree_api/spree_api_hooks.rb
  21. +23 −0 api/spree_api.gemspec
  22. 0 {vendor/extensions → }/api/test/functional/admin/users_controller_test.rb
  23. 0 {vendor/extensions → }/api/test/integration/inventory_units_api_test.rb
  24. 0 {vendor/extensions → }/api/test/integration/orders_api_test.rb
  25. 0 {vendor/extensions → }/api/test/integration/shipments_api_test.rb
  26. 0 {vendor/extensions → }/api/test/test_helper.rb
  27. 0 {vendor/extensions → }/api/test/unit/user_test.rb
  28. +1 −0 core/lib/spree_core/all.rb
  29. +1 −0 spree.gemspec
  30. +0 −137 vendor/extensions/api/Rakefile
  31. +0 −75 vendor/extensions/api/api_extension.rb
  32. +0 −20 vendor/extensions/api/config/routes.rb
  33. +0 −17 vendor/extensions/api/lib/tasks/api_extension_tasks.rake
View
0 vendor/extensions/api/README.markdown → api/README.md
File renamed without changes.
View
62 api/Rakefile
@@ -0,0 +1,62 @@
+gem 'rdoc', '= 2.2'
+require 'rdoc'
+require 'rake'
+require 'rake/testtask'
+require 'rake/rdoctask'
+require 'rake/packagetask'
+require 'rake/gempackagetask'
+
+require File.expand_path('../../config/application', __FILE__)
+
+Rails::Application.load_tasks
+
+desc "Default Task"
+task :default => [ :test ]
+
+# Run the unit tests
+Rake::TestTask.new { |t|
+ t.libs << "test"
+ t.pattern = 'test/**/*_test.rb'
+ t.warning = true
+}
+
+# namespace :test do
+# task :isolated do
+# ruby = File.join(*RbConfig::CONFIG.values_at('bindir', 'RUBY_INSTALL_NAME'))
+# Dir.glob("test/**/*_test.rb").all? do |file|
+# system(ruby, '-Ilib:test', file)
+# end or raise "Failures"
+# end
+# end
+#
+# # Generate the RDoc documentation
+# Rake::RDocTask.new { |rdoc|
+# rdoc.rdoc_dir = 'doc'
+# rdoc.title = "Action Mailer -- Easy email delivery and testing"
+# rdoc.options << '--line-numbers' << '--inline-source' << '-A cattr_accessor=object'
+# rdoc.options << '--charset' << 'utf-8'
+# rdoc.template = ENV['template'] ? "#{ENV['template']}.rb" : '../doc/template/horo'
+# rdoc.rdoc_files.include('README', 'CHANGELOG')
+# rdoc.rdoc_files.include('lib/action_mailer.rb')
+# rdoc.rdoc_files.include('lib/action_mailer/*.rb')
+# rdoc.rdoc_files.include('lib/action_mailer/delivery_method/*.rb')
+# }
+#
+# spec = eval(File.read('actionmailer.gemspec'))
+#
+# Rake::GemPackageTask.new(spec) do |p|
+# p.gem_spec = spec
+# end
+#
+# desc "Release to gemcutter"
+# task :release => :package do
+# require 'rake/gemcutter'
+# Rake::Gemcutter::Tasks.new(spec).define
+# Rake::Task['gem:push'].invoke
+# end
+#
+# desc "Publish the API documentation"
+# task :pdoc => [:rdoc] do
+# require 'rake/contrib/sshpublisher'
+# Rake::SshDirPublisher.new("rails@api.rubyonrails.org", "public_html/am", "doc").upload
+# end
View
0 ...pi/app/controllers/api/base_controller.rb → api/app/controllers/api/base_controller.rb
File renamed without changes.
View
0 ...p/controllers/api/countries_controller.rb → ...p/controllers/api/countries_controller.rb
File renamed without changes.
View
0 ...rollers/api/inventory_units_controller.rb → ...rollers/api/inventory_units_controller.rb
File renamed without changes.
View
0 .../controllers/api/line_items_controller.rb → .../controllers/api/line_items_controller.rb
File renamed without changes.
View
0 .../app/controllers/api/orders_controller.rb → api/app/controllers/api/orders_controller.rb
File renamed without changes.
View
0 ...pp/controllers/api/products_controller.rb → ...pp/controllers/api/products_controller.rb
File renamed without changes.
View
0 ...p/controllers/api/shipments_controller.rb → ...p/controllers/api/shipments_controller.rb
File renamed without changes.
View
0 .../app/controllers/api/states_controller.rb → api/app/controllers/api/states_controller.rb
File renamed without changes.
View
0 ...s/api/app/helpers/api/shipments_helper.rb → api/app/helpers/api/shipments_helper.rb
File renamed without changes.
View
0 ...pp/views/admin/users/_api_fields.html.erb → ...pp/views/admin/users/_api_fields.html.erb
File renamed without changes.
View
0 vendor/extensions/api/config/locales/en.yml → api/config/locales/en.yml
File renamed without changes.
View
0 ...r/extensions/api/config/locales/ru-RU.yml → api/config/locales/ru-RU.yml
File renamed without changes.
View
36 api/config/routes.rb
@@ -0,0 +1,36 @@
+Spree::Application.routes.draw do |map|
+ namespace :admin do
+ resources :users do
+ member do
+ put :generate_api_key
+ put :clear_api_key
+ end
+ end
+ end
+
+ namespace :api do
+ resources :shipments, :except => [:new,:edit] do
+ put :event, :on => :member
+ resources :inventory_units, :except => [:new,:edit] do
+ put :event, :on => :member
+ end
+ end
+ resources :orders, :except => [:new,:edit] do
+ put :event, :on => :member
+ resources :shipments, :except => [:new,:edit]
+ resources :line_items, :except => [:new,:edit]
+ resources :inventory_units, :except => [:new,:edit] do
+ put :event, :on => :member
+ end
+ end
+ resources :inventory_units, :except => [:new,:edit] do
+ put :event, :on => :member
+ end
+ resources :products, :except => [:new,:edit]
+ resources :countries, :except => [:new,:edit] do
+ resources :states, :except => [:new,:edit]
+ end
+ resources :states, :except => [:new,:edit]
+ end
+
+end
View
0 ...te/20100107141738_add_api_key_to_users.rb → ...te/20100107141738_add_api_key_to_users.rb
File renamed without changes.
View
0 vendor/extensions/api/db/seeds.rb → api/db/seeds.rb
File renamed without changes.
View
59 api/lib/spree_api.rb
@@ -0,0 +1,59 @@
+require 'spree_core'
+
+ActiveSupport.on_load(:after_initialize) do
+ User.class_eval do
+
+ def clear_api_key!
+ self.update_attribute(:api_key, "")
+ end
+
+ def generate_api_key!
+ self.update_attribute(:api_key, secure_digest(Time.now, (1..10).map{ rand.to_s }))
+ end
+
+ private
+
+ def secure_digest(*args)
+ Digest::SHA1.hexdigest(args.flatten.join('--'))
+ end
+
+ end
+
+ Admin::UsersController.class_eval do
+
+ def generate_api_key
+ if object.generate_api_key!
+ flash.notice = t('api.key_generated')
+ end
+ redirect_to edit_object_path
+ end
+ def clear_api_key
+ if object.clear_api_key!
+ flash.notice = t('api.key_cleared')
+ end
+ redirect_to edit_object_path
+ end
+
+ end
+
+ Spree::BaseController.class_eval do
+ private
+ def current_user
+ return @current_user if defined?(@current_user)
+ if current_user_session && current_user_session.user
+ return @current_user = current_user_session.user
+ end
+ if token = request.headers['X-SpreeAPIKey']
+ @current_user = User.find_by_api_key(token)
+ end
+ end
+ end
+
+ LineItem.class_eval do
+ def description
+ d = variant.product.name.clone
+ d << " (#{variant.options_text})" unless variant.option_values.empty?
+ d
+ end
+ end
+end
View
6 api/lib/spree_api/engine.rb
@@ -0,0 +1,6 @@
+require "spree_api"
+
+module SpreeApi
+ class Engine < Rails::Engine
+ end
+end
View
0 vendor/extensions/api/api_hooks.rb → api/lib/spree_api/spree_api_hooks.rb
File renamed without changes.
View
23 api/spree_api.gemspec
@@ -0,0 +1,23 @@
+version = File.read(File.expand_path("../../SPREE_VERSION", __FILE__)).strip
+
+Gem::Specification.new do |s|
+ s.platform = Gem::Platform::RUBY
+ s.name = 'spree_api'
+ s.version = version
+ #s.summary = 'Email composition, delivery, and receiving framework (part of Rails).'
+ #s.description = 'Email on Rails. Compose, deliver, receive, and test emails using the familiar controller/view pattern. First-class support for multipart email and attachments.'
+ s.required_ruby_version = '>= 1.8.7'
+
+ # s.author = 'David Heinemeier Hansson'
+ # s.email = 'david@loudthinking.com'
+ # s.homepage = 'http://www.rubyonrails.org'
+ # s.rubyforge_project = 'actionmailer'
+
+ s.files = Dir['CHANGELOG', 'README', 'MIT-LICENSE', 'lib/**/*']
+ s.require_path = 'lib'
+ s.requirements << 'none'
+
+ s.has_rdoc = true
+
+ s.add_dependency('spree_core', version)
+end
View
0 ...functional/admin/users_controller_test.rb → ...functional/admin/users_controller_test.rb
File renamed without changes.
View
0 ...t/integration/inventory_units_api_test.rb → ...t/integration/inventory_units_api_test.rb
File renamed without changes.
View
0 ...s/api/test/integration/orders_api_test.rb → api/test/integration/orders_api_test.rb
File renamed without changes.
View
0 ...pi/test/integration/shipments_api_test.rb → api/test/integration/shipments_api_test.rb
File renamed without changes.
View
0 vendor/extensions/api/test/test_helper.rb → api/test/test_helper.rb
File renamed without changes.
View
0 vendor/extensions/api/test/unit/user_test.rb → api/test/unit/user_test.rb
File renamed without changes.
View
1 core/lib/spree_core/all.rb
@@ -3,6 +3,7 @@
%w(
spree_core
spree_payment_gateway
+ spree_api
).each do |extension|
begin
require "#{extension}/engine"
View
1 spree.gemspec
@@ -21,6 +21,7 @@ Gem::Specification.new do |s|
s.add_dependency('spree_core', version)
s.add_dependency('spree_payment_gateway', version)
+ s.add_dependency('spree_api', version)
# RAILS3 TODO - add more of the core extensions, etc.
s.add_dependency('bundler', '>= 0.9.26')
s.add_dependency('rails', '= 3.0.0.beta4')
View
137 vendor/extensions/api/Rakefile
@@ -1,137 +0,0 @@
-# I think this is the one that should be moved to the extension Rakefile template
-
-# In rails 1.2, plugins aren't available in the path until they're loaded.
-# Check to see if the rspec plugin is installed first and require
-# it if it is. If not, use the gem version.
-
-# Determine where the RSpec plugin is by loading the boot
-unless defined? SPREE_ROOT
- ENV["RAILS_ENV"] = "test"
- case
- when ENV["SPREE_ENV_FILE"]
- require File.dirname(ENV["SPREE_ENV_FILE"]) + "/boot"
- when File.dirname(__FILE__) =~ %r{vendor/SPREE/vendor/extensions}
- require "#{File.expand_path(File.dirname(__FILE__) + "/../../../../../")}/config/boot"
- else
- require "#{File.expand_path(File.dirname(__FILE__) + "/../../../")}/config/boot"
- end
-end
-
-require 'rake'
-require 'rake/rdoctask'
-require 'rake/testtask'
-
-rspec_base = File.expand_path(SPREE_ROOT + '/vendor/plugins/rspec/lib')
-$LOAD_PATH.unshift(rspec_base) if File.exist?(rspec_base)
-require 'spec/rake/spectask'
-# require 'spec/translator'
-
-# Cleanup the SPREE_ROOT constant so specs will load the environment
-Object.send(:remove_const, :SPREE_ROOT)
-
-extension_root = File.expand_path(File.dirname(__FILE__))
-
-task :default => :spec
-task :stats => "spec:statsetup"
-
-desc "Run all specs in spec directory"
-Spec::Rake::SpecTask.new(:spec) do |t|
- t.spec_opts = ['--options', "\"#{extension_root}/spec/spec.opts\""]
- t.spec_files = FileList["#{extension_root}/spec/**/*_spec.rb"]
-end
-
-namespace :spec do
- desc "Run all specs in spec directory with RCov"
- Spec::Rake::SpecTask.new(:rcov) do |t|
- t.spec_opts = ['--options', "\"#{extension_root}/spec/spec.opts\""]
- t.spec_files = FileList['spec/**/*_spec.rb']
- t.rcov = true
- t.rcov_opts = ['--exclude', 'spec', '--rails']
- end
-
- desc "Print Specdoc for all specs"
- Spec::Rake::SpecTask.new(:doc) do |t|
- t.spec_opts = ["--format", "specdoc", "--dry-run"]
- t.spec_files = FileList['spec/**/*_spec.rb']
- end
-
- [:models, :controllers, :views, :helpers].each do |sub|
- desc "Run the specs under spec/#{sub}"
- Spec::Rake::SpecTask.new(sub) do |t|
- t.spec_opts = ['--options', "\"#{extension_root}/spec/spec.opts\""]
- t.spec_files = FileList["spec/#{sub}/**/*_spec.rb"]
- end
- end
-
- # Hopefully no one has written their extensions in pre-0.9 style
- # desc "Translate specs from pre-0.9 to 0.9 style"
- # task :translate do
- # translator = ::Spec::Translator.new
- # dir = Rails.root + '/spec'
- # translator.translate(dir, dir)
- # end
-
- # Setup specs for stats
- task :statsetup do
- require 'code_statistics'
- ::STATS_DIRECTORIES << %w(Model\ specs spec/models)
- ::STATS_DIRECTORIES << %w(View\ specs spec/views)
- ::STATS_DIRECTORIES << %w(Controller\ specs spec/controllers)
- ::STATS_DIRECTORIES << %w(Helper\ specs spec/views)
- ::CodeStatistics::TEST_TYPES << "Model specs"
- ::CodeStatistics::TEST_TYPES << "View specs"
- ::CodeStatistics::TEST_TYPES << "Controller specs"
- ::CodeStatistics::TEST_TYPES << "Helper specs"
- ::STATS_DIRECTORIES.delete_if {|a| a[0] =~ /test/}
- end
-
- namespace :db do
- namespace :fixtures do
- desc "Load fixtures (from spec/fixtures) into the current environment's database. Load specific fixtures using FIXTURES=x,y"
- task :load => :environment do
- require 'active_record/fixtures'
- ActiveRecord::Base.establish_connection(RAILS_ENV.to_sym)
- (ENV['FIXTURES'] ? ENV['FIXTURES'].split(/,/) : Dir.glob(Rails.root.join(, 'spec', 'fixtures', '*.{yml,csv}'))).each do |fixture_file|
- Fixtures.create_fixtures('spec/fixtures', File.basename(fixture_file, '.*'))
- end
- end
- end
- end
-end
-
-desc 'Generate documentation for the api extension.'
-Rake::RDocTask.new(:rdoc) do |rdoc|
- rdoc.rdoc_dir = 'rdoc'
- rdoc.title = 'ApiExtension'
- rdoc.options << '--line-numbers' << '--inline-source'
- rdoc.rdoc_files.include('README')
- rdoc.rdoc_files.include('lib/**/*.rb')
-end
-
-# For extensions that are in transition
-desc 'Test the api extension.'
-Rake::TestTask.new(:test) do |t|
- t.libs << 'lib'
- t.pattern = 'test/**/*_test.rb'
- t.verbose = true
-end
-
-namespace :test do
- desc 'Functional test the api extension.'
- Rake::TestTask.new(:functionals) do |t|
- t.libs << 'lib'
- t.pattern = 'test/functional/*_test.rb'
- t.verbose = true
- end
-
- desc 'Unit test the api extension.'
- Rake::TestTask.new(:units) do |t|
- t.libs << 'lib'
- t.pattern = 'test/unit/*_test.rb'
- t.verbose = true
- end
-end
-
-
-# Load any custom rakefiles for extension
-Dir[File.dirname(__FILE__) + '/tasks/*.rake'].sort.each { |f| require f }
View
75 vendor/extensions/api/api_extension.rb
@@ -1,75 +0,0 @@
-# Uncomment this if you reference any of your controllers in activate
-# require_dependency 'application'
-
-class ApiExtension < Spree::Extension
- version "1.0"
- description "Describe your extension here"
- url "http://yourwebsite.com/api"
-
- # Please use api/config/routes.rb instead for extension routes.
-
- # def self.require_gems(config)
- # config.gem "gemname-goes-here", :version => '1.2.3'
- # end
-
- def activate
- User.class_eval do
-
- def clear_api_key!
- self.update_attribute(:api_key, "")
- end
-
- def generate_api_key!
- self.update_attribute(:api_key, secure_digest(Time.now, (1..10).map{ rand.to_s }))
- end
-
- private
-
- def secure_digest(*args)
- Digest::SHA1.hexdigest(args.flatten.join('--'))
- end
-
- end
-
- Admin::UsersController.class_eval do
-
- def generate_api_key
- if object.generate_api_key!
- flash.notice = t('api.key_generated')
- end
- redirect_to edit_object_path
- end
- def clear_api_key
- if object.clear_api_key!
- flash.notice = t('api.key_cleared')
- end
- redirect_to edit_object_path
- end
-
- end
-
- Spree::BaseController.class_eval do
- private
- def current_user
- return @current_user if defined?(@current_user)
- if current_user_session && current_user_session.user
- return @current_user = current_user_session.user
- end
- if token = request.headers['X-SpreeAPIKey']
- @current_user = User.find_by_api_key(token)
- end
- end
- end
-
- LineItem.class_eval do
- def description
- d = variant.product.name.clone
- d << " (#{variant.options_text})" unless variant.option_values.empty?
- d
- end
- end
-
- end
-end
-
-
View
20 vendor/extensions/api/config/routes.rb
@@ -1,20 +0,0 @@
-map.namespace :admin do |admin|
- admin.resources :users, :member => {:generate_api_key => :put, :clear_api_key => :put}
-end
-map.namespace :api do |api|
- api.resources :shipments, :except => [:new,:edit], :member => {:event => :put} do |shipments|
- shipments.resources :inventory_units, :except => [:new,:edit], :member => {:event => :put}
- end
- api.resources :orders, :except => [:new,:edit], :member => {:event => :put} do |orders|
- orders.resources :shipments, :except => [:new,:edit]
- orders.resources :line_items, :except => [:new,:edit]
- orders.resources :inventory_units, :except => [:new,:edit], :member => {:event => :put}
- end
- api.resources :inventory_units, :except => [:new,:edit], :member => {:event => :put}
- api.resources :products, :except => [:new,:edit]
-
- api.resources :countries, :except => [:new,:edit] do |countries|
- countries.resources :states, :except => [:new,:edit]
- end
- api.resources :states, :except => [:new,:edit]
-end
View
17 vendor/extensions/api/lib/tasks/api_extension_tasks.rake
@@ -1,17 +0,0 @@
-namespace :spree do
- namespace :extensions do
- namespace :api do
- desc "Copies public assets of the Api to the instance public/ directory."
- task :update => :environment do
- is_svn_git_or_dir = proc {|path| path =~ /\.svn/ || path =~ /\.git/ || File.directory?(path) }
- Dir[ApiExtension.root + "/public/**/*"].reject(&is_svn_git_or_dir).each do |file|
- path = file.sub(ApiExtension.root, '')
- directory = File.dirname(path)
- puts "Copying #{path}..."
- mkdir_p Rails.root + directory
- cp file, Rails.root + path
- end
- end
- end
- end
-end

0 comments on commit fcf13cc

Please sign in to comment.