Permalink
Browse files

Adding support for namespaced models. Should resolve #63

  • Loading branch information...
joncalhoun committed Oct 12, 2014
1 parent 8d996a9 commit 48c9a09785d7f85037e702d95aa9a2faedabe185
Showing with 763 additions and 15 deletions.
  1. +40 −4 Rakefile
  2. +26 −6 lib/upmin/model.rb
  3. +5 −5 spec/spec_helper.rb
  4. +19 −0 spec_namespaced_model/taggable_spec.rb
  5. +16 −0 test_apps/namespaced_model/.gitignore
  6. +3 −0 test_apps/namespaced_model/.rspec
  7. +20 −0 test_apps/namespaced_model/Gemfile
  8. +28 −0 test_apps/namespaced_model/README.rdoc
  9. +6 −0 test_apps/namespaced_model/Rakefile
  10. 0 test_apps/namespaced_model/app/assets/images/.keep
  11. +16 −0 test_apps/namespaced_model/app/assets/javascripts/application.js
  12. +15 −0 test_apps/namespaced_model/app/assets/stylesheets/application.css
  13. +5 −0 test_apps/namespaced_model/app/controllers/application_controller.rb
  14. 0 test_apps/namespaced_model/app/controllers/concerns/.keep
  15. +2 −0 test_apps/namespaced_model/app/helpers/application_helper.rb
  16. 0 test_apps/namespaced_model/app/mailers/.keep
  17. 0 test_apps/namespaced_model/app/models/.keep
  18. 0 test_apps/namespaced_model/app/models/concerns/.keep
  19. +3 −0 test_apps/namespaced_model/app/models/model_with_tag.rb
  20. +14 −0 test_apps/namespaced_model/app/views/layouts/application.html.erb
  21. +3 −0 test_apps/namespaced_model/bin/bundle
  22. +4 −0 test_apps/namespaced_model/bin/rails
  23. +4 −0 test_apps/namespaced_model/bin/rake
  24. +4 −0 test_apps/namespaced_model/config.ru
  25. +30 −0 test_apps/namespaced_model/config/application.rb
  26. +4 −0 test_apps/namespaced_model/config/boot.rb
  27. +25 −0 test_apps/namespaced_model/config/database.yml
  28. +5 −0 test_apps/namespaced_model/config/environment.rb
  29. +37 −0 test_apps/namespaced_model/config/environments/development.rb
  30. +78 −0 test_apps/namespaced_model/config/environments/production.rb
  31. +39 −0 test_apps/namespaced_model/config/environments/test.rb
  32. +8 −0 test_apps/namespaced_model/config/initializers/assets.rb
  33. +7 −0 test_apps/namespaced_model/config/initializers/backtrace_silencers.rb
  34. +3 −0 test_apps/namespaced_model/config/initializers/cookies_serializer.rb
  35. +4 −0 test_apps/namespaced_model/config/initializers/filter_parameter_logging.rb
  36. +16 −0 test_apps/namespaced_model/config/initializers/inflections.rb
  37. +4 −0 test_apps/namespaced_model/config/initializers/mime_types.rb
  38. +3 −0 test_apps/namespaced_model/config/initializers/session_store.rb
  39. +14 −0 test_apps/namespaced_model/config/initializers/wrap_parameters.rb
  40. +23 −0 test_apps/namespaced_model/config/locales/en.yml
  41. +3 −0 test_apps/namespaced_model/config/routes.rb
  42. +22 −0 test_apps/namespaced_model/config/secrets.yml
  43. 0 test_apps/namespaced_model/lib/assets/.keep
  44. 0 test_apps/namespaced_model/lib/tasks/.keep
  45. 0 test_apps/namespaced_model/log/.keep
  46. +67 −0 test_apps/namespaced_model/public/404.html
  47. +67 −0 test_apps/namespaced_model/public/422.html
  48. +66 −0 test_apps/namespaced_model/public/500.html
  49. 0 test_apps/namespaced_model/public/favicon.ico
  50. +5 −0 test_apps/namespaced_model/public/robots.txt
  51. 0 test_apps/namespaced_model/vendor/assets/javascripts/.keep
  52. 0 test_apps/namespaced_model/vendor/assets/stylesheets/.keep
View
@@ -13,12 +13,17 @@ end
task :default => "spec:all"
def update_files
# Drop and reload spec files
def update_files(gemfile = nil)
# Drop and reload spec files
sh "rm -rf spec/"
sh "cp -R ../../spec spec"
sh "cp ../../.rspec .rspec"
# Copy gemfile specific specs if they exist
if Dir.exists?("../../spec_#{gemfile}")
sh "cp -R ../../spec_#{gemfile} spec/#{gemfile}"
end
# Drop and reload Upmin::Model files
sh "rm -rf app/upmin/"
sh "cp -R ../../test_app_upmin app/upmin"
@@ -69,16 +74,47 @@ namespace :spec do
end
end
desc "Run Tests with namespaced models"
task :namespaced_model do
Dir.chdir("test_apps/namespaced_model")
puts "Testing in #{`pwd`}"
sh "bundle install --quiet"
sh "bundle update --quiet"
# Drop migrations and recreate
sh "rm -rf db/migrate/*.test_models.rb"
sh "bundle exec rake railties:install:migrations > /dev/null"
sh "RAILS_ENV=test bundle exec rake db:drop db:create db:migrate db:seed --quiet > /dev/null"
update_files("namespaced_model")
# Run tests
sh "bundle exec rake"
end
desc "Run Tests with namespaced models quickly (no bundle install etc)"
task :namespaced_model_quick do
Dir.chdir("test_apps/namespaced_model")
puts "Re-Testing in #{`pwd`}. Bundle install and migration updates will NOT happen!"
update_files("namespaced_model")
# Run tests
sh "bundle exec rake"
end
desc "Run Tests against all ORMs"
task :all do
%w(active_record_32 active_record_40 active_record_41 active_record_42 will_paginate data_mapper).each do |gemfile|
%w(active_record_32 active_record_40 active_record_41 active_record_42 will_paginate data_mapper namespaced_model).each do |gemfile|
sh "rake spec:#{gemfile}"
end
end
desc "Run Tests against all ORMs"
task :all_quick do
%w(active_record_32 active_record_40 active_record_41 active_record_42 will_paginate data_mapper).each do |gemfile|
%w(active_record_32 active_record_40 active_record_41 active_record_42 will_paginate data_mapper namespaced_model).each do |gemfile|
sh "rake spec:#{gemfile}_quick"
end
end
View
@@ -110,10 +110,26 @@ def Model.find_class(model)
def Model.find_or_create_class(model_name)
::Rails.application.eager_load!
return "Admin#{model_name}".constantize
create_name = model_name.gsub(":", "")
return "Admin#{create_name}".constantize
rescue NameError
eval("class ::Admin#{model_name} < Upmin::Model; end")
return "Admin#{model_name}".constantize
if model_name.match(/::/)
class_str = <<-class_string
class ::Admin#{create_name} < Upmin::Model
def self.model_class
return #{model_name}
end
def self.inferred_model_class
return #{model_name}
end
end
class_string
eval(class_str)
else
eval("class ::Admin#{create_name} < Upmin::Model; end")
end
return "Admin#{create_name}".constantize
end
# Returns all upmin models.
@@ -136,24 +152,28 @@ def Model.model_class
end
def Model.inferred_model_class
name = model_class_name
name = inferred_model_class_name
return name.constantize
rescue NameError => error
raise if name && !error.missing_name?(name)
raise Upmin::UninferrableSourceError.new(self)
end
def Model.model_class_name
def Model.inferred_model_class_name
raise NameError if name.nil? || name.demodulize !~ /Admin.+$/
return name.demodulize[5..-1]
end
def Model.model_class_name
n = model_class.name
end
def Model.model_name
return ActiveModel::Name.new(model_class)
end
def Model.humanized_name(type = :plural)
names = model_class_name.split(/(?=[A-Z])/)
names = model_class_name.split(/(?=[A-Z])/).map{|n| n.gsub(":", "")}
if type == :plural
names[names.length-1] = names.last.pluralize
end
View
@@ -46,11 +46,11 @@
end
# Uncomment this if you want to the page to be saved and opened after any test failure.
config.after do |example|
if example.metadata[:type] == :feature && example.exception.present?
save_and_open_page
end
end
# config.after do |example|
# if example.metadata[:type] == :feature && example.exception.present?
# save_and_open_page
# end
# end
config.include(FactoryGirl::Syntax::Methods)
config.include(ActionView::Helpers::NumberHelper, type: :view)
@@ -0,0 +1,19 @@
# encoding: UTF-8
require "spec_helper"
feature("Taggable Search Views") do
background do
# Setup BG Stuff
end
scenario("Acts As Taggable On Tags") do
# acts as taggable crap isn't eager loaded for some reason, so go here first
visit("/upmin/m/ActsAsTaggableOn::Tag")
expect(page).to(have_selector("a.search-result-link", count: 1))
visit("/upmin/m/ActsAsTaggableOn::Tag/i/#{ActsAsTaggableOn::Tag.first.id}")
expect(page).to(have_selector("h3", text: "Acts As Taggable On Tag # 1"))
end
end
@@ -0,0 +1,16 @@
# See https://help.github.com/articles/ignoring-files for more about ignoring files.
#
# If you find yourself ignoring temporary files generated by your text editor
# or operating system, you probably want to add a global ignore instead:
# git config --global core.excludesfile '~/.gitignore_global'
# Ignore bundler config.
/.bundle
# Ignore the default SQLite database.
/db/*.sqlite3
/db/*.sqlite3-journal
# Ignore all logfiles and tempfiles.
/log/*.log
/tmp
@@ -0,0 +1,3 @@
--color
--require spec_helper
--format=d
@@ -0,0 +1,20 @@
source 'https://rubygems.org'
gem 'rails', '4.1.6'
gem 'sqlite3'
gem 'sass-rails', '~> 4.0.3'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.0.0'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 2.0'
gem 'spring', group: :development
# Used for testing
gem 'test_models', git: "git://github.com/upmin/test_models.git"
gem 'acts-as-taggable-on', '~> 3.4'
gemspec :path => '../../'
@@ -0,0 +1,28 @@
== README
This README would normally document whatever steps are necessary to get the
application up and running.
Things you may want to cover:
* Ruby version
* System dependencies
* Configuration
* Database creation
* Database initialization
* How to run the test suite
* Services (job queues, cache servers, search engines, etc.)
* Deployment instructions
* ...
Please feel free to use a different markup language if you do not plan to run
<tt>rake doc:app</tt>.
@@ -0,0 +1,6 @@
# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
require File.expand_path('../config/application', __FILE__)
Rails.application.load_tasks
@@ -0,0 +1,16 @@
// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// compiled file.
//
// Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require_tree .
@@ -0,0 +1,15 @@
/*
* This is a manifest file that'll be compiled into application.css, which will include all the files
* listed below.
*
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
* or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
*
* You're free to add application-wide styles to this file and they'll appear at the bottom of the
* compiled file so the styles you add here take precedence over styles defined in any styles
* defined in the other CSS/SCSS files in this directory. It is generally better to create a new
* file per style scope.
*
*= require_tree .
*= require_self
*/
@@ -0,0 +1,5 @@
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
end
@@ -0,0 +1,2 @@
module ApplicationHelper
end
@@ -0,0 +1,3 @@
class ModelWithTag < ActiveRecord::Base
acts_as_taggable
end
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html>
<head>
<title>ActiveRecord41</title>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= csrf_meta_tags %>
</head>
<body>
<%= yield %>
</body>
</html>
@@ -0,0 +1,3 @@
#!/usr/bin/env ruby
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
load Gem.bin_path('bundler', 'bundle')
@@ -0,0 +1,4 @@
#!/usr/bin/env ruby
APP_PATH = File.expand_path('../../config/application', __FILE__)
require_relative '../config/boot'
require 'rails/commands'
@@ -0,0 +1,4 @@
#!/usr/bin/env ruby
require_relative '../config/boot'
require 'rake'
Rake.application.run
@@ -0,0 +1,4 @@
# This file is used by Rack-based servers to start the application.
require ::File.expand_path('../config/environment', __FILE__)
run Rails.application
@@ -0,0 +1,30 @@
require File.expand_path('../boot', __FILE__)
# Pick the frameworks you want:
require "active_model/railtie"
require "active_record/railtie"
require "action_controller/railtie"
require "action_mailer/railtie"
require "action_view/railtie"
require "sprockets/railtie"
# require "rails/test_unit/railtie"
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
module ActiveRecord41
class Application < Rails::Application
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
# config.time_zone = 'Central Time (US & Canada)'
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
# config.i18n.default_locale = :de
end
end
@@ -0,0 +1,4 @@
# Set up gems listed in the Gemfile.
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE'])
@@ -0,0 +1,25 @@
# SQLite version 3.x
# gem install sqlite3
#
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem 'sqlite3'
#
default: &default
adapter: sqlite3
pool: 5
timeout: 5000
development:
<<: *default
database: db/development.sqlite3
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
<<: *default
database: db/test.sqlite3
production:
<<: *default
database: db/production.sqlite3
@@ -0,0 +1,5 @@
# Load the Rails application.
require File.expand_path('../application', __FILE__)
# Initialize the Rails application.
Rails.application.initialize!
Oops, something went wrong.

0 comments on commit 48c9a09

Please sign in to comment.