acts_as_param 1.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.
@@ -0,0 +1,2 @@
1
+ /log
2
+ /pkg
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in acts_as_param.gemspec
4
+ gemspec
@@ -0,0 +1,14 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ acts_as_param (1.0.0)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+
10
+ PLATFORMS
11
+ ruby
12
+
13
+ DEPENDENCIES
14
+ acts_as_param!
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 [name of plugin creator]
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,29 @@
1
+ ActsAsParam
2
+ ===========
3
+
4
+ Example
5
+ =======
6
+
7
+ // In you model
8
+ class Product
9
+ acts_as_param :name, :serial
10
+ end
11
+
12
+ // How To Use
13
+ product = Product.create(:name => 'name',:serial => 'serial'}
14
+ product.to_param == 'name'
15
+
16
+ product = Product.create(:name => nil,:serial => 'serial'}
17
+ product.to_param == 'serial'
18
+
19
+ product = Product.create(:name => nil,:serial => nil}
20
+ product.to_param == product.id
21
+
22
+ product.find_by_param('serial') # find by product's serial
23
+ product.find_by_param('name') # find by product's name
24
+ product.find_by_param(2) # find by Product's primary key
25
+
26
+ product.find_by_param!('serial') # raise ActiveRecord::RecordNotFound exception if not found
27
+
28
+
29
+ Copyright (c) 2010 Jinzhu / wosmvp@gmail.com, released under the MIT license
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "acts_as_param"
6
+ s.version = "1.0.1"
7
+ s.authors = ["Jinzhu"]
8
+ s.email = ["wosmvp@gmail.com"]
9
+ s.homepage = "https://github.com/jinzhu/acts_as_param"
10
+ s.summary = %q{acts as param}
11
+ s.description = %q{acts as param}
12
+
13
+ s.rubyforge_project = "acts_as_param"
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.executables = s.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
18
+ s.require_paths = ["lib"]
19
+
20
+ # specify any dependencies here; for example:
21
+ # s.add_development_dependency "rspec"
22
+ # s.add_runtime_dependency "rest-client"
23
+ end
@@ -0,0 +1,58 @@
1
+ module Acts
2
+ module As
3
+ module Param < Rails::Engine
4
+ module Base
5
+ def self.included(base)
6
+ base.extend ClassMethods
7
+ end
8
+
9
+ def self.included(base)
10
+ base.extend(ClassMethods)
11
+ end
12
+
13
+ module ClassMethods
14
+ def acts_as_param(*fields)
15
+ fields << self.primary_key
16
+ include ActiveRecord::Acts::Param::InstanceMethods
17
+ extend ActiveRecord::Acts::Param::SingletonMethods
18
+
19
+ class_eval <<-EOV
20
+ def self._acts_as_param_fields
21
+ #{fields.inspect}
22
+ end
23
+ EOV
24
+ end
25
+ end
26
+
27
+ module SingletonMethods
28
+ def find_by_param(key)
29
+ _acts_as_param_fields.map do |field|
30
+ record = first(:conditions => [ "#{field} = ?", key])
31
+ return record if record.present?
32
+ end
33
+ return nil
34
+ end
35
+
36
+ def find_by_param!(key)
37
+ record = find_by_param(key)
38
+ raise ActiveRecord::RecordNotFound unless record
39
+ return record
40
+ end
41
+ end
42
+
43
+ module InstanceMethods
44
+ def to_param
45
+ self.class._acts_as_param_fields.map do |field|
46
+ value = self.send(field.to_sym)
47
+ return value.to_s if value.present?
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
53
+
54
+ initializer :insert_acts_as_param, :before => :load_config_initializers do
55
+ ActiveRecord::Base.send :include, ::Acts::As::Param::Base
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :acts_as_param do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1 @@
1
+ require File.join(PLUGIN_RAILS_ROOT,'app','controllers','application_controller.rb')
@@ -0,0 +1,2 @@
1
+ class ApplicationController < ActionController::Base
2
+ end
@@ -0,0 +1,3 @@
1
+ class Product < ActiveRecord::Base
2
+ acts_as_param :serial, :name
3
+ end
@@ -0,0 +1,108 @@
1
+ PLUGIN_RAILS_ROOT = "#{File.dirname(__FILE__)}/.."
2
+ RAILS_ROOT = PLUGIN_RAILS_ROOT unless defined?(RAILS_ROOT)
3
+
4
+ module Rails
5
+ class << self
6
+ def boot!
7
+ unless booted?
8
+ preinitialize
9
+ pick_boot.run
10
+ end
11
+ end
12
+
13
+ def booted?
14
+ defined? Rails::Initializer
15
+ end
16
+
17
+ def pick_boot
18
+ (vendor_rails? ? VendorBoot : GemBoot).new
19
+ end
20
+
21
+ def vendor_rails?
22
+ File.exist?("#{RAILS_ROOT}/vendor/rails")
23
+ end
24
+
25
+ def preinitialize
26
+ load(preinitializer_path) if File.exist?(preinitializer_path)
27
+ end
28
+
29
+ def preinitializer_path
30
+ "#{RAILS_ROOT}/config/preinitializer.rb"
31
+ end
32
+ end
33
+
34
+ class Boot
35
+ def run
36
+ load_initializer
37
+ Rails::Initializer.run(:set_load_path)
38
+ end
39
+ end
40
+
41
+ class VendorBoot < Boot
42
+ def load_initializer
43
+ require "#{RAILS_ROOT}/vendor/rails/railties/lib/initializer"
44
+ Rails::Initializer.run(:install_gem_spec_stubs)
45
+ Rails::GemDependency.add_frozen_gem_path
46
+ end
47
+ end
48
+
49
+ class GemBoot < Boot
50
+ def load_initializer
51
+ self.class.load_rubygems
52
+ load_rails_gem
53
+ require 'initializer'
54
+ end
55
+
56
+ def load_rails_gem
57
+ if version = self.class.gem_version
58
+ gem 'rails', version
59
+ else
60
+ gem 'rails'
61
+ end
62
+ rescue Gem::LoadError => load_error
63
+ $stderr.puts %(Missing the Rails #{version} gem. Please `gem install -v=#{version} rails`.)
64
+ exit 1
65
+ end
66
+
67
+ class << self
68
+ def rubygems_version
69
+ Gem::RubyGemsVersion rescue nil
70
+ end
71
+
72
+ def gem_version
73
+ if defined? RAILS_VERSION
74
+ RAILS_VERSION
75
+ elsif ENV.include?('RAILS_VERSION')
76
+ ENV['RAILS_VERSION']
77
+ else
78
+ parse_gem_version(read_environment_rb)
79
+ end
80
+ end
81
+
82
+ def load_rubygems
83
+ require 'rubygems'
84
+ min_version = '1.3.1'
85
+ unless rubygems_version >= min_version
86
+ $stderr.puts %Q(Rails requires RubyGems >= #{min_version} (you have #{rubygems_version}). Please `gem update --system` and try again.)
87
+ exit 1
88
+ end
89
+
90
+ rescue LoadError
91
+ $stderr.puts %Q(Rails requires RubyGems >= #{min_version}. Please install RubyGems and try again: http://rubygems.rubyforge.org)
92
+ exit 1
93
+ end
94
+
95
+ def parse_gem_version(text)
96
+ $1 if text =~ /^[^#]*RAILS_VERSION\s*=\s*["']([!~<>=]*\s*[\d.]+)["']/
97
+ end
98
+
99
+ private
100
+ def read_environment_rb
101
+ File.read("#{RAILS_ROOT}/config/environment.rb")
102
+ end
103
+ end
104
+ end
105
+ end
106
+
107
+ # All that for this:
108
+ Rails.boot!
@@ -0,0 +1,25 @@
1
+ in_memory:
2
+ adapter: sqlite3
3
+ database: ":memory:"
4
+ verbosity: quiet
5
+
6
+ sqlite:
7
+ adapter: sqlite
8
+ dbfile: newplugin.sqlite.db
9
+
10
+ sqlite3:
11
+ adapter: sqlite3
12
+ dbfile: newplugin.sqlite3.db
13
+
14
+ postgresql:
15
+ adapter: postgresql
16
+ username: postgres
17
+ password: postgres
18
+ database: newplugin
19
+
20
+ mysql:
21
+ adapter: mysql
22
+ host: localhost
23
+ username: root
24
+ password:
25
+ database: newplugin
@@ -0,0 +1,16 @@
1
+ ENV['RAILS_ENV'] ||= 'in_memory'
2
+
3
+ require File.join(File.dirname(__FILE__), 'boot')
4
+
5
+ Rails::Initializer.run do |config|
6
+ config.cache_classes = false
7
+ config.whiny_nils = true
8
+
9
+ # test/app_root/config/environment.rb
10
+ config.load_paths << File.join(File.dirname(__FILE__), '../../../lib')
11
+
12
+ config.action_controller.session = {
13
+ :key => '_newplugin_session',
14
+ :secret => '1634aa213e0052cb68c48c1dd90e96303fa4566cf6726509d82a30fd5980c1971d2ce9017555b1d5f7a8a6ea07f1fe5e2f5f120579c10df764a46086f2afa371'
15
+ }
16
+ end
@@ -0,0 +1,4 @@
1
+ # load plugin
2
+ plugin_path = File.join(File.dirname(__FILE__), *%w(.. .. .. ..))
3
+
4
+ load File.join(plugin_path, "init.rb")
@@ -0,0 +1,4 @@
1
+ ActionController::Routing::Routes.draw do |map|
2
+ map.connect ':controller/:action/:id'
3
+ map.connect ':controller/:action/:id.:format'
4
+ end
@@ -0,0 +1,14 @@
1
+ class CreateProducts < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :products do |t|
4
+ t.column :name, :string
5
+ t.column :serial, :string
6
+ t.column :created_at, :datetime
7
+ t.column :updated_at, :datetime
8
+ end
9
+ end
10
+
11
+ def self.down
12
+ drop_table :products
13
+ end
14
+ end
@@ -0,0 +1,6 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/app_root/config/environment')
2
+ require 'test_help'
3
+
4
+ # Run the migrations
5
+ ActiveRecord::Migration.verbose = false
6
+ ActiveRecord::Migrator.migrate("#{PLUGIN_RAILS_ROOT}/db/migrate")
@@ -0,0 +1,32 @@
1
+ require 'test/test_helper'
2
+
3
+ class ProductTest < ActiveSupport::TestCase
4
+ def setup
5
+ @product1 = Product.create(:name => 'name1', :serial => 'serial1')
6
+ @product2 = Product.create(:name => 'name2', :serial => '')
7
+ @product3 = Product.create(:name => '', :serial => '')
8
+ end
9
+
10
+ def test_product_to_param
11
+ assert_equal @product1.to_param, 'serial1'
12
+ assert_equal @product2.to_param, 'name2'
13
+ assert_equal @product3.to_param, @product3.id
14
+ end
15
+
16
+ def test_product_find_by_param
17
+ assert_equal Product.find_by_param('serial1'), @product1
18
+ assert_equal Product.find_by_param('name1'), @product1
19
+ assert_equal Product.find_by_param('name2'), @product2
20
+ assert_equal Product.find_by_param(@product1.id), @product1
21
+ assert_equal Product.find_by_param(@product3.id), @product3
22
+ end
23
+
24
+ def test_product_find_by_param
25
+ assert_equal Product.find_by_param!('serial1'), @product1
26
+ assert_equal Product.find_by_param!('name1'), @product1
27
+ assert_equal Product.find_by_param!('name2'), @product2
28
+ assert_raise ActiveRecord::RecordNotFound do
29
+ Product.find_by_param!('not_exist')
30
+ end
31
+ end
32
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acts_as_param
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jinzhu
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-08-14 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: acts as param
15
+ email:
16
+ - wosmvp@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - Gemfile.lock
24
+ - MIT-LICENSE
25
+ - README
26
+ - Rakefile
27
+ - acts_as_param.gemspec
28
+ - lib/acts_as_param.rb
29
+ - tasks/acts_as_param_tasks.rake
30
+ - test/app_root/app/controllers/application.rb
31
+ - test/app_root/app/controllers/application_controller.rb
32
+ - test/app_root/app/models/product.rb
33
+ - test/app_root/config/boot.rb
34
+ - test/app_root/config/database.yml
35
+ - test/app_root/config/environment.rb
36
+ - test/app_root/config/environments/in_memory.rb
37
+ - test/app_root/config/environments/mysql.rb
38
+ - test/app_root/config/environments/postgresql.rb
39
+ - test/app_root/config/environments/sqlite.rb
40
+ - test/app_root/config/environments/sqlite3.rb
41
+ - test/app_root/config/initializers/plugin.rb
42
+ - test/app_root/config/routes.rb
43
+ - test/app_root/db/migrate/20100715040112_create_products.rb
44
+ - test/test_helper.rb
45
+ - test/unit/test_product.rb
46
+ homepage: https://github.com/jinzhu/acts_as_param
47
+ licenses: []
48
+ post_install_message:
49
+ rdoc_options: []
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ! '>='
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ! '>='
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ requirements: []
65
+ rubyforge_project: acts_as_param
66
+ rubygems_version: 1.8.24
67
+ signing_key:
68
+ specification_version: 3
69
+ summary: acts as param
70
+ test_files:
71
+ - test/app_root/app/controllers/application.rb
72
+ - test/app_root/app/controllers/application_controller.rb
73
+ - test/app_root/app/models/product.rb
74
+ - test/app_root/config/boot.rb
75
+ - test/app_root/config/database.yml
76
+ - test/app_root/config/environment.rb
77
+ - test/app_root/config/environments/in_memory.rb
78
+ - test/app_root/config/environments/mysql.rb
79
+ - test/app_root/config/environments/postgresql.rb
80
+ - test/app_root/config/environments/sqlite.rb
81
+ - test/app_root/config/environments/sqlite3.rb
82
+ - test/app_root/config/initializers/plugin.rb
83
+ - test/app_root/config/routes.rb
84
+ - test/app_root/db/migrate/20100715040112_create_products.rb
85
+ - test/test_helper.rb
86
+ - test/unit/test_product.rb
87
+ has_rdoc: