beautified_url 0.0.1 → 0.0.2

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 +44 -3
  2. metadata +2 -2
@@ -2,14 +2,19 @@ module BeautifiedUrl
2
2
 
3
3
  def self.included(base)
4
4
  base.extend(ClassMethods)
5
- base.send(:before_validation, :beautify_url)
5
+ base.send(:before_create, :beautify_url_on_create)
6
+ base.send(:before_update, :beautify_url_on_update)
7
+ base.send(:before_save, :beautify_url_on_save)
8
+ base.send(:before_validation, :beautify_url_on_validate)
6
9
  end
7
10
 
8
11
  module ClassMethods
9
12
  attr_accessor :bu_token
10
13
  attr_accessor :bu_attributes_hset
11
14
  attr_accessor :bu_scope_set
15
+ attr_accessor :bu_callback_event
12
16
 
17
+ #Checks if model is well equipped to use beautified_url capability by checking up attribute names, if it is paired with attribute starting with name '_bu_'
13
18
  def is_beautifiable?
14
19
  @bu_token ||= "_bu_"
15
20
  @bu_attributes_hset = {}
@@ -22,10 +27,28 @@ module BeautifiedUrl
22
27
  (not @bu_attributes_hset.empty?)
23
28
  end
24
29
 
30
+ #Accepts scope parameter for beautified_url
31
+ #Eg => beautify_url_with_scope :company_id #For universal scope of all attributes with beautified_url capabilities.
32
+ #Eg => beautify_url_with_scope {:name => :company_id, :title => :subdomain}
25
33
  def beautify_url_with_scope(options)
26
34
  options = options.to_s if options.is_a?(Symbol)
27
35
  @bu_scope_set = options if options.is_a?(Hash) or options.is_a?(String)
28
36
  end
37
+
38
+ #Signifies on what 'before' event should the beautification take palce.
39
+ #Valid values are :create (default value), :save, :update, :validate
40
+ def beautify_on(event)
41
+ begin
42
+ event = event.to_sym unless event.is_a?(Symbol)
43
+ @bu_callback_event = [:create, :save, :update, :validate].include?(event) ? event : :create
44
+ rescue Exception => e
45
+ Rails.logger.debug e.message
46
+ Rails.logger.debug e.backtrace.join("\n")
47
+ @bu_callback_event = :create
48
+ end
49
+
50
+ @bu_callback_event
51
+ end
29
52
  end
30
53
 
31
54
  def attribute_scope_hash_set(base = self.class)
@@ -74,8 +97,8 @@ module BeautifiedUrl
74
97
  bu_attr_value_t
75
98
  end
76
99
 
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
100
+ #makes beautiful url by looking at parent attribute(s) whose bu_attributes are identified by "_bu_" (default bu_token)
101
+ #only if bu_attribute is not already populated
79
102
  def beautify_url(base = self.class)
80
103
  return unless base.is_beautifiable?
81
104
 
@@ -87,6 +110,24 @@ module BeautifiedUrl
87
110
  end
88
111
  end
89
112
  end
113
+
114
+ private
115
+ def beautify_url_on_create(base = self.class)
116
+ beautify_url if base.bu_callback_event.nil? or base.bu_callback_event == :create
117
+ end
118
+
119
+ def beautify_url_on_update(base = self.class)
120
+ beautify_url if base.bu_callback_event == :update
121
+ end
122
+
123
+ def beautify_url_on_save(base = self.class)
124
+ beautify_url if base.bu_callback_event == :save
125
+ end
126
+
127
+ def beautify_url_on_validate(base = self.class)
128
+ beautify_url if base.bu_callback_event == :validate
129
+ end
130
+
90
131
  end
91
132
 
92
133
  class String
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: beautified_url
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-24 00:00:00.000000000 Z
12
+ date: 2012-02-27 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: ! 'Basic tool which provides feature of generating tokens which can be
15
15
  used in url for identifying resource(uniquely of uniquely within a scope) instead