beautified_url 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/beautified_url.rb +100 -0
  2. metadata +51 -0
@@ -0,0 +1,100 @@
1
+ module BeautifiedUrl
2
+
3
+ def self.included(base)
4
+ base.extend(ClassMethods)
5
+ base.send(:before_validation, :beautify_url)
6
+ end
7
+
8
+ module ClassMethods
9
+ attr_accessor :bu_token
10
+ attr_accessor :bu_attributes_hset
11
+ attr_accessor :bu_scope_set
12
+
13
+ def is_beautifiable?
14
+ @bu_token ||= "_bu_"
15
+ @bu_attributes_hset = {}
16
+ attribute_names.each do |attribute_name|
17
+ if attribute_name.starts_with?(@bu_token)
18
+ p_attr = attribute_name.gsub(@bu_token, "") #parent attribute name
19
+ @bu_attributes_hset[p_attr] = attribute_name if attribute_names.include?(p_attr)
20
+ end
21
+ end
22
+ (not @bu_attributes_hset.empty?)
23
+ end
24
+
25
+ def beautify_url_with_scope(options)
26
+ options = options.to_s if options.is_a?(Symbol)
27
+ @bu_scope_set = options if options.is_a?(Hash) or options.is_a?(String)
28
+ end
29
+ end
30
+
31
+ def attribute_scope_hash_set(base = self.class)
32
+ scope_hset = base.bu_scope_set
33
+
34
+ if base.bu_scope_set.is_a?(String) and base.attribute_names.include?(base.bu_scope_set)
35
+ scope_hset = {}
36
+ base.bu_attributes_hset.keys.each { |p_attr| scope_hset[p_attr] = base.bu_scope_set }
37
+ end
38
+ scope_hset ||{}
39
+ end
40
+
41
+ #generates condition array to be used while searching for unique record from bu_attribute value
42
+ def conditions_array(p_attr, bu_attr, bu_attr_value, base = self.class)
43
+ condition_array = ["#{bu_attr} = ? ", bu_attr_value]
44
+ unless self.send(base.primary_key).nil?
45
+ condition_array[0] = "#{condition_array[0]} and #{base.primary_key} != ?"
46
+ condition_array.push(self.send(base.primary_key))
47
+ end
48
+
49
+ attribute_scope_hash_set.each do |sp_key, sp_key_scope|
50
+ sp_key, sp_key_scope = sp_key.to_s, sp_key_scope.to_s
51
+ if p_attr == sp_key and base.attribute_names.include?(sp_key) and base.attribute_names.include?(sp_key_scope)
52
+ condition_array[0] = "#{condition_array[0]} and #{sp_key_scope} = ?"
53
+ condition_array.push(self.send(sp_key_scope))
54
+ end
55
+ end
56
+ condition_array
57
+ end
58
+
59
+ #finds uniqueness of bu_attribute's value (with scope if defined)
60
+ def make_unique_beautiful_url(p_attr, bu_attr, bu_attr_value, base = self.class)
61
+ bu_attr_value_t = bu_attr_value
62
+
63
+ keep_looking_flag = true
64
+ while (keep_looking_flag)
65
+ if base.where(conditions_array(p_attr, bu_attr, bu_attr_value_t)).count == 0
66
+ keep_looking_flag = false
67
+ else
68
+ counter ||= 0
69
+ counter += 1
70
+ bu_attr_value_t = "#{bu_attr_value}-#{counter}"
71
+ end
72
+ end
73
+
74
+ bu_attr_value_t
75
+ end
76
+
77
+ #makes beautiful url by looking at parent attribute(s) whose bu_attributes are identified by "_bu_" (default bu_token)
78
+ #only if bu_attribute is not already populated
79
+ def beautify_url(base = self.class)
80
+ return unless base.is_beautifiable?
81
+
82
+ base.bu_attributes_hset.each do |p_attr, bu_attr|
83
+ if self.send(bu_attr).nil?
84
+ unless (p_attr.nil? or p_attr.empty?) #If parent attribute is not nil or empty
85
+ self.send("#{bu_attr}=", make_unique_beautiful_url(p_attr, bu_attr, self.send(p_attr).beautify_me))
86
+ end
87
+ end
88
+ end
89
+ end
90
+ end
91
+
92
+ class String
93
+ def beautify_me
94
+ self.downcase.gsub(/[ -]+/, "-").gsub(/[^a-zA-Z0-9-]/, "")
95
+ end
96
+ end
97
+
98
+
99
+ #Adding to ActiveRecord::Base
100
+ ActiveRecord::Base.send(:include, BeautifiedUrl)
metadata ADDED
@@ -0,0 +1,51 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: beautified_url
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Praveen Kumar Sinha
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-02-24 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: ! 'Basic tool which provides feature of generating tokens which can be
15
+ used in url for identifying resource(uniquely of uniquely within a scope) instead
16
+ of :id. Just add another field which starts with _bu_ of one or many existing fields
17
+ to make it all happen. Example: For a blog post, if you have a ''title'' field and
18
+ want to beautify and use it instead of :id add another field like ''_bu_title''
19
+ and now modify routes and application to refer and fetch resource from ''_bu_title''
20
+ field.'
21
+ email: praveen.kumar.sinha@gmail.com
22
+ executables: []
23
+ extensions: []
24
+ extra_rdoc_files: []
25
+ files:
26
+ - lib/beautified_url.rb
27
+ homepage: https://github.com/praveenkumarsinha/BeautifiedUrl
28
+ licenses: []
29
+ post_install_message:
30
+ rdoc_options: []
31
+ require_paths:
32
+ - lib
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ required_rubygems_version: !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ requirements: []
46
+ rubyforge_project:
47
+ rubygems_version: 1.8.24
48
+ signing_key:
49
+ specification_version: 3
50
+ summary: BeautifiedUrl
51
+ test_files: []