withardry 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/Gemfile +4 -0
- data/README.md +47 -0
- data/Rakefile +11 -0
- data/lib/withardry/base.rb +21 -0
- data/lib/withardry/version.rb +3 -0
- data/lib/withardry.rb +5 -0
- data/spec/db/database.yml +3 -0
- data/spec/db/schema.rb +10 -0
- data/spec/models.rb +11 -0
- data/spec/spec_helper.rb +13 -0
- data/spec/withardry_spec.rb +22 -0
- data/withardry.gemspec +24 -0
- metadata +64 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
# Withardry
|
2
|
+
|
3
|
+
## Install
|
4
|
+
|
5
|
+
gem "withardry", :git => "git://github.com/evrone/withardry.git"
|
6
|
+
|
7
|
+
## Usage
|
8
|
+
|
9
|
+
class Post < ActiveRecord::Base
|
10
|
+
withardry :title
|
11
|
+
|
12
|
+
# exactly same as
|
13
|
+
# named_scope :with_title, lambda { |t| { :conditions => { :title => t }}}
|
14
|
+
|
15
|
+
friendly_url
|
16
|
+
# same as to_param with model id and name attribute
|
17
|
+
|
18
|
+
friendly_url :title
|
19
|
+
# same as to_param with model id and title attribute
|
20
|
+
end
|
21
|
+
|
22
|
+
Now use this inside your app:
|
23
|
+
|
24
|
+
Post.with_title("Some title")
|
25
|
+
Post.to_param # => "1-title-example"
|
26
|
+
|
27
|
+
Sure, it can handle some options
|
28
|
+
|
29
|
+
class Post < ActiveRecord::Base
|
30
|
+
belongs_to :user
|
31
|
+
|
32
|
+
withardry :user, :prefix => "by", :as => "user_id"
|
33
|
+
|
34
|
+
// exactly same as
|
35
|
+
// named_scope by_user, lambda {|u| { :conditions => { :user_id => u }}}
|
36
|
+
end
|
37
|
+
|
38
|
+
And then anywhere in your app
|
39
|
+
|
40
|
+
Post.by_user(some_user_id)
|
41
|
+
|
42
|
+
## Contributors
|
43
|
+
|
44
|
+
- Ognevsky Andrey, [Evrone Company](https://github.com/evrone)
|
45
|
+
- [Kirill Shatrov](https://github.com/kirs/), [Evrone Company](https://github.com/evrone)
|
46
|
+
|
47
|
+
## Feel free for pull requests
|
data/Rakefile
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'spec/rake/spectask'
|
3
|
+
|
4
|
+
desc 'Default: run specs.'
|
5
|
+
task :default => :spec
|
6
|
+
|
7
|
+
desc 'Run the specs'
|
8
|
+
Spec::Rake::SpecTask.new(:spec) do |t|
|
9
|
+
t.spec_opts = ['--colour --format progress --loadby mtime --reverse']
|
10
|
+
t.spec_files = FileList['spec/**/*_spec.rb']
|
11
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Withardry
|
2
|
+
# example:
|
3
|
+
# withardry OR withardry :user, :prefix => "by", :as => "user_id"
|
4
|
+
def withardry(name, options = {})
|
5
|
+
field = options[:as] || name
|
6
|
+
prefix = options[:prefix] || "with"
|
7
|
+
|
8
|
+
scope "#{prefix}_#{name}", lambda {|value| where("#{field}" => value) }
|
9
|
+
end
|
10
|
+
|
11
|
+
# example:
|
12
|
+
# friendly_url OR friendly_url :title
|
13
|
+
def friendly_url(field = :name)
|
14
|
+
define_method(:to_param) do
|
15
|
+
attribute = self.send(field)
|
16
|
+
stack = [id.to_s]
|
17
|
+
stack << I18n.transliterate(attribute).gsub(/[^a-zA-Z0-9]+/, ' ').strip if attribute.present?
|
18
|
+
stack.join("-")
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/lib/withardry.rb
ADDED
data/spec/db/schema.rb
ADDED
data/spec/models.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
class User < ActiveRecord::Base
|
2
|
+
has_many :posts, :dependent => :destroy
|
3
|
+
end
|
4
|
+
|
5
|
+
class Post < ActiveRecord::Base
|
6
|
+
belongs_to :user
|
7
|
+
|
8
|
+
withardry :title
|
9
|
+
withardry :title, :prefix => "containing"
|
10
|
+
withardry :user, :as => "user_id", :prefix => "by"
|
11
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
begin
|
2
|
+
require File.dirname(__FILE__) + '/../../../../spec/spec_helper'
|
3
|
+
rescue LoadError
|
4
|
+
puts "You need to install rspec in your base app"
|
5
|
+
exit
|
6
|
+
end
|
7
|
+
|
8
|
+
plugin_spec_dir = File.dirname(__FILE__)
|
9
|
+
ActiveRecord::Base.logger = Logger.new(plugin_spec_dir + "/debug.log")
|
10
|
+
|
11
|
+
databases = YAML::load(IO.read(plugin_spec_dir + "/db/database.yml"))
|
12
|
+
ActiveRecord::Base.establish_connection(databases[ENV["DB"] || "sqlite3"])
|
13
|
+
load(File.join(plugin_spec_dir, "db", "schema.rb"))
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
+
require File.dirname(__FILE__) + '/models'
|
3
|
+
|
4
|
+
describe "Post" do
|
5
|
+
|
6
|
+
before(:each) do
|
7
|
+
@user = User.create! :login => "user"
|
8
|
+
@post = @user.posts.create! :title => "first"
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should find with default options" do
|
12
|
+
Post.with_title(@post.title).should == [@post]
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should find with :prefix option" do
|
16
|
+
Post.containing_title(@post.title).should == [@post]
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should find with :prefix and :as options" do
|
20
|
+
Post.by_user(@post.user.id).should == [@post]
|
21
|
+
end
|
22
|
+
end
|
data/withardry.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "withardry/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "withardry"
|
7
|
+
s.version = Withardry::VERSION
|
8
|
+
s.authors = ["Kir Shatrov", "Andrey Ognevsky"]
|
9
|
+
s.email = ["razor.psp@gmail.com"]
|
10
|
+
s.homepage = "https://github.com/evrone/withardry"
|
11
|
+
s.summary = %q{A simple plugin to DRY models}
|
12
|
+
s.description = ""
|
13
|
+
|
14
|
+
s.rubyforge_project = "withardry"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
# specify any dependencies here; for example:
|
22
|
+
# s.add_development_dependency "rspec"
|
23
|
+
# s.add_runtime_dependency "rest-client"
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: withardry
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Kir Shatrov
|
9
|
+
- Andrey Ognevsky
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
date: 2011-11-02 00:00:00.000000000Z
|
14
|
+
dependencies: []
|
15
|
+
description: ''
|
16
|
+
email:
|
17
|
+
- razor.psp@gmail.com
|
18
|
+
executables: []
|
19
|
+
extensions: []
|
20
|
+
extra_rdoc_files: []
|
21
|
+
files:
|
22
|
+
- .gitignore
|
23
|
+
- Gemfile
|
24
|
+
- README.md
|
25
|
+
- Rakefile
|
26
|
+
- lib/withardry.rb
|
27
|
+
- lib/withardry/base.rb
|
28
|
+
- lib/withardry/version.rb
|
29
|
+
- spec/db/database.yml
|
30
|
+
- spec/db/schema.rb
|
31
|
+
- spec/models.rb
|
32
|
+
- spec/spec_helper.rb
|
33
|
+
- spec/withardry_spec.rb
|
34
|
+
- withardry.gemspec
|
35
|
+
homepage: https://github.com/evrone/withardry
|
36
|
+
licenses: []
|
37
|
+
post_install_message:
|
38
|
+
rdoc_options: []
|
39
|
+
require_paths:
|
40
|
+
- lib
|
41
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ! '>='
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
48
|
+
none: false
|
49
|
+
requirements:
|
50
|
+
- - ! '>='
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0'
|
53
|
+
requirements: []
|
54
|
+
rubyforge_project: withardry
|
55
|
+
rubygems_version: 1.8.10
|
56
|
+
signing_key:
|
57
|
+
specification_version: 3
|
58
|
+
summary: A simple plugin to DRY models
|
59
|
+
test_files:
|
60
|
+
- spec/db/database.yml
|
61
|
+
- spec/db/schema.rb
|
62
|
+
- spec/models.rb
|
63
|
+
- spec/spec_helper.rb
|
64
|
+
- spec/withardry_spec.rb
|