dynamic_router 0.0.1

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 (46) hide show
  1. checksums.yaml +7 -0
  2. data/.coveralls.yml +1 -0
  3. data/.gitignore +55 -0
  4. data/.travis.yml +10 -0
  5. data/Gemfile +4 -0
  6. data/LICENSE +22 -0
  7. data/LICENSE.txt +22 -0
  8. data/README.md +31 -0
  9. data/Rakefile +10 -0
  10. data/dynamic_router.gemspec +30 -0
  11. data/lib/dynamic_router.rb +8 -0
  12. data/lib/dynamic_router/class_methods.rb +59 -0
  13. data/lib/dynamic_router/version.rb +3 -0
  14. data/spec/db/create_database.rb +13 -0
  15. data/spec/dummy/Rakefile +7 -0
  16. data/spec/dummy/app/controllers/application_controller.rb +6 -0
  17. data/spec/dummy/app/controllers/dummy_controller.rb +5 -0
  18. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  19. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  20. data/spec/dummy/config.ru +4 -0
  21. data/spec/dummy/config/application.rb +47 -0
  22. data/spec/dummy/config/boot.rb +10 -0
  23. data/spec/dummy/config/database.yml +22 -0
  24. data/spec/dummy/config/environment.rb +5 -0
  25. data/spec/dummy/config/environments/development.rb +26 -0
  26. data/spec/dummy/config/environments/production.rb +50 -0
  27. data/spec/dummy/config/environments/test.rb +35 -0
  28. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  29. data/spec/dummy/config/initializers/inflections.rb +10 -0
  30. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  31. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  32. data/spec/dummy/config/initializers/session_store.rb +8 -0
  33. data/spec/dummy/config/locales/en.yml +5 -0
  34. data/spec/dummy/config/locales/pt_BR.yml +2 -0
  35. data/spec/dummy/config/routes.rb +2 -0
  36. data/spec/dummy/db/test.sqlite3 +0 -0
  37. data/spec/dummy/log/test.log +1463 -0
  38. data/spec/dummy/public/404.html +26 -0
  39. data/spec/dummy/public/422.html +26 -0
  40. data/spec/dummy/public/500.html +26 -0
  41. data/spec/dummy/public/favicon.ico +0 -0
  42. data/spec/dummy/public/stylesheets/.gitkeep +0 -0
  43. data/spec/dummy/script/rails +6 -0
  44. data/spec/dynamic_router_spec.rb +46 -0
  45. data/spec/spec_helper.rb +31 -0
  46. metadata +219 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: abc0a2d19bc3fb9974bba3a517de829a29fa82d1
4
+ data.tar.gz: 5ec55c93c6ff0b7f08e1f52aec5e73e1c33d0820
5
+ SHA512:
6
+ metadata.gz: 78b07a36b939ca786e39b6ad039ac0b92508bdb551c7f2e5580639b7e74edc400aa90e07ecb63621ff434e6ea9b959a06352d83e31665fc2416f5b97a7fe37b4
7
+ data.tar.gz: 917822ef9a54f88c789402700b9e360e51578dd0166ab94e07fb58e87483e47d5a023a7fed2b483d5f7b7c83abb2febec4cf3dd991c37a4aaa35a7987e3c976a
data/.coveralls.yml ADDED
@@ -0,0 +1 @@
1
+ service_name: travis-ci
data/.gitignore ADDED
@@ -0,0 +1,55 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
23
+ /.config
24
+ /coverage/
25
+ /InstalledFiles
26
+ /pkg/
27
+ /spec/reports/
28
+ /test/tmp/
29
+ /test/version_tmp/
30
+ /tmp/
31
+
32
+ ## Specific to RubyMotion:
33
+ .dat*
34
+ .repl_history
35
+ build/
36
+
37
+ ## Documentation cache and generated files:
38
+ /.yardoc/
39
+ /_yardoc/
40
+ /doc/
41
+ /rdoc/
42
+
43
+ ## Environment normalisation:
44
+ /.bundle/
45
+ /lib/bundler/man/
46
+
47
+ # for a library or gem, you might want to ignore these files since the code is
48
+ # intended to run in multiple environments; otherwise, check them in:
49
+ # Gemfile.lock
50
+ # .ruby-version
51
+ # .ruby-gemset
52
+
53
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
54
+ .rvmrc
55
+ .project
data/.travis.yml ADDED
@@ -0,0 +1,10 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+
5
+ notifications:
6
+ email:
7
+ recipients:
8
+ - gberdugo@gmail.com
9
+ on_failure: change
10
+ on_success: never
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in dynamic_router.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 gberdugo
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Gustavo Berdugo
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # DynamicRouter
2
+
3
+ [![Build Status](https://travis-ci.org/gberdugo/dynamic_router.png?branch=master)](https://travis-ci.org/gberdugo/dynamic_router) [![Gem Version](https://badge.fury.io/rb/dynamic_router.png)](http://badge.fury.io/rb/dynamic_router) [![Coverage Status](https://coveralls.io/repos/coyosoftware/dynamic_router/badge.png)](https://coveralls.io/r/coyosoftware/dynamic_router)
4
+
5
+ Add dynamic routes to your project
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'dynamic_router'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install dynamic_router
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it ( https://github.com/gberdugo/dynamic_router/fork )
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'rspec/core/rake_task'
2
+ require "bundler/gem_tasks"
3
+
4
+ # Default directory to look in is `/specs`
5
+ # Run with `rake spec`
6
+ RSpec::Core::RakeTask.new(:spec) do |task|
7
+ task.rspec_opts = ['--color', '--format', 'documentation']
8
+ end
9
+
10
+ task :default => :spec
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'dynamic_router/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "dynamic_router"
8
+ spec.version = DynamicRouter::VERSION
9
+ spec.authors = ["Gustavo Berdugo"]
10
+ spec.email = ["gberdugo@gmail.com"]
11
+ spec.summary = %q{Add dynamic routes to your project}
12
+ spec.description = %q{Use this gem to add dynamic routes to your project.}
13
+ spec.homepage = "http://rubygems.org/gems/dynamic_router"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency('rails', '~> 4.0')
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.6"
24
+ spec.add_development_dependency "rake", "~> 10.3"
25
+ spec.add_development_dependency "rspec", "~> 3.1"
26
+ spec.add_development_dependency "pry", "~> 0.10"
27
+ spec.add_development_dependency "coveralls", "~> 0.7"
28
+ spec.add_development_dependency "sqlite3", "~> 1.3"
29
+
30
+ end
@@ -0,0 +1,8 @@
1
+ require "dynamic_router/version"
2
+ require "dynamic_router/class_methods"
3
+
4
+ module DynamicRouter
5
+ def self.extended(receiver)
6
+ receiver.extend ClassMethods
7
+ end
8
+ end
@@ -0,0 +1,59 @@
1
+ module DynamicRouter
2
+ module ClassMethods
3
+ def has_dynamic_route(url, target, options = {})
4
+ method = options[:method] || :get
5
+ klass = self
6
+
7
+ if ActiveRecord::Base.connection.table_exists? klass.table_name
8
+ Rails.application.routes.draw do
9
+ klass.find_each do |model|
10
+ defaults = DynamicRouter::ClassMethods.parse_defaults(options[:defaults], model)
11
+
12
+ self.send(method, url.call(model).to_s, :to => target.to_s, :defaults => defaults)
13
+ end
14
+ end
15
+ end
16
+
17
+ add_after_save_callback(klass, method, url, target, options[:defaults])
18
+ end
19
+
20
+ def add_after_save_callback(klass, method, url, target, defaults)
21
+ klass.cattr_accessor :_method
22
+ klass.cattr_accessor :_url
23
+ klass.cattr_accessor :_target
24
+ klass.cattr_accessor :_defaults
25
+
26
+ klass._method = method
27
+ klass._url = url
28
+ klass._target = target
29
+ klass._defaults = defaults
30
+
31
+ klass.class_eval do
32
+ after_save :create_route
33
+
34
+ def create_route
35
+ klass = self
36
+
37
+ Rails.application.routes.draw do
38
+ defaults = DynamicRouter::ClassMethods.parse_defaults(klass._defaults, klass)
39
+
40
+ self.send(klass._method, klass._url.call(klass).to_s, :to => klass._target.to_s, :defaults => defaults)
41
+ end
42
+ end
43
+ end
44
+ end
45
+
46
+ module_function
47
+ def parse_defaults(_defaults, klass)
48
+ unless _defaults.blank?
49
+ defaults = {}
50
+
51
+ _defaults.each do |key, value|
52
+ defaults[key.to_sym] = value.call(klass)
53
+ end
54
+ end
55
+
56
+ defaults
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,3 @@
1
+ module DynamicRouter
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,13 @@
1
+ class CreateDatabase < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :examples do |t|
4
+ t.string :first_path
5
+ t.string :second_path
6
+ t.string :default_field
7
+ end
8
+ end
9
+
10
+ def self.down
11
+ drop_table :examples
12
+ end
13
+ end
@@ -0,0 +1,7 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require File.expand_path('../config/application', __FILE__)
5
+ require 'rake'
6
+
7
+ Dummy::Application.load_tasks
@@ -0,0 +1,6 @@
1
+ class ApplicationController < ActionController::Base
2
+ protect_from_forgery
3
+
4
+ def dummy_method
5
+ end
6
+ end
@@ -0,0 +1,5 @@
1
+ class DummyController < ApplicationController
2
+ def dummy_method
3
+
4
+ end
5
+ end
@@ -0,0 +1,2 @@
1
+ module ApplicationHelper
2
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Dummy</title>
5
+ <%= stylesheet_link_tag :all %>
6
+ <%= javascript_include_tag :defaults %>
7
+ <%= csrf_meta_tag %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
@@ -0,0 +1,4 @@
1
+ # This file is used by Rack-based servers to start the application.
2
+
3
+ require ::File.expand_path('../config/environment', __FILE__)
4
+ run Dummy::Application
@@ -0,0 +1,47 @@
1
+ require File.expand_path('../boot', __FILE__)
2
+
3
+ require "active_model/railtie"
4
+ require "active_record/railtie"
5
+ require "action_controller/railtie"
6
+ require "action_view/railtie"
7
+ require "action_mailer/railtie"
8
+
9
+ Bundler.require
10
+
11
+ module Dummy
12
+ class Application < Rails::Application
13
+ # Settings in config/environments/* take precedence over those specified here.
14
+ # Application configuration should go into files in config/initializers
15
+ # -- all .rb files in that directory are automatically loaded.
16
+
17
+ # Custom directories with classes and modules you want to be autoloadable.
18
+ # config.autoload_paths += %W(#{config.root}/extras)
19
+
20
+ # Only load the plugins named here, in the order given (default is alphabetical).
21
+ # :all can be used as a placeholder for all plugins not explicitly named.
22
+ # config.plugins = [ :exception_notification, :ssl_requirement, :all ]
23
+
24
+ # Activate observers that should always be running.
25
+ # config.active_record.observers = :cacher, :garbage_collector, :forum_observer
26
+
27
+ # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
28
+ # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
29
+ # config.time_zone = 'Central Time (US & Canada)'
30
+
31
+ # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
32
+ # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
33
+ # config.i18n.default_locale = :de
34
+
35
+ # JavaScript files you want as :defaults (application.js is always included).
36
+ # config.action_view.javascript_expansions[:defaults] = %w(jquery rails)
37
+
38
+ # Configure the default encoding used in templates for Ruby 1.9.
39
+ config.encoding = "utf-8"
40
+
41
+ # Configure sensitive parameters which will be filtered from the log file.
42
+ config.filter_parameters += [:password]
43
+
44
+ config.i18n.locale = :pt_BR
45
+ config.i18n.default_locale = :pt_BR
46
+ end
47
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ gemfile = File.expand_path('../../../../Gemfile', __FILE__)
3
+
4
+ if File.exist?(gemfile)
5
+ ENV['BUNDLE_GEMFILE'] = gemfile
6
+ require 'bundler'
7
+ Bundler.setup
8
+ end
9
+
10
+ $:.unshift File.expand_path('../../../../lib', __FILE__)
@@ -0,0 +1,22 @@
1
+ # SQLite version 3.x
2
+ # gem install sqlite3-ruby (not necessary on OS X Leopard)
3
+ development:
4
+ adapter: sqlite3
5
+ database: db/development.sqlite3
6
+ pool: 5
7
+ timeout: 5000
8
+
9
+ # Warning: The database defined as "test" will be erased and
10
+ # re-generated from your development database when you run "rake".
11
+ # Do not set this db to the same as development or production.
12
+ test:
13
+ adapter: sqlite3
14
+ database: db/test.sqlite3
15
+ pool: 5
16
+ timeout: 5000
17
+
18
+ production:
19
+ adapter: sqlite3
20
+ database: db/production.sqlite3
21
+ pool: 5
22
+ timeout: 5000
@@ -0,0 +1,5 @@
1
+ # Load the rails application
2
+ require File.expand_path('../application', __FILE__)
3
+
4
+ # Initialize the rails application
5
+ Dummy::Application.initialize!