sequenceid 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.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/Rakefile +2 -0
- data/lib/generators/sequenceid/sequenceid_generator.rb +52 -0
- data/lib/generators/sequenceid/template/migration.rb +26 -0
- data/lib/sequenceid/sequenceid/model_adapters/active_record_adapter.rb +5 -0
- data/lib/sequenceid/sequenceid/model_adapters/sequenceid_logic.rb +42 -0
- data/lib/sequenceid/sequenceid/version.rb +3 -0
- data/lib/sequenceid/version.rb +3 -0
- data/lib/sequenceid.rb +2 -0
- data/sequenceid.gemspec +21 -0
- metadata +76 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Rakefile
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
require 'rails/generators/active_record'
|
|
2
|
+
|
|
3
|
+
class SequenceidGenerator < ActiveRecord::Generators::Base
|
|
4
|
+
#Rails::Generators::NamedBase
|
|
5
|
+
desc "This generator is for sequenceid, which creates sequential id's in the URL for nested resources. Especially useful for SaaS based apps where the unique resource is likely a nested resource of company"
|
|
6
|
+
ORM=ActiveRecord::Base #TODO: Make this generic for other ORM's
|
|
7
|
+
source_root File.expand_path("../template",__FILE__)
|
|
8
|
+
|
|
9
|
+
def initialize_sequenceid
|
|
10
|
+
parent_resource_s=@_initializer[0][0]
|
|
11
|
+
nested_resource_s=@_initializer[0][1]
|
|
12
|
+
if (!parent_resource_s || !nested_resource_s)
|
|
13
|
+
puts "Usage: rails generate sequenceid <parent resource> <nested resource> "
|
|
14
|
+
puts "eg, rails generate company user"
|
|
15
|
+
puts "the nested resource must be in someway nested to the parent resource to avoid a conflict in url's"
|
|
16
|
+
puts "eg, if a company is represented by subdomain of the url, you will always have a unique url for the user even though the sequence id's might be shared"
|
|
17
|
+
puts "so if two company's exist, 7vals and sevenvals, then their first users will have urls: http://7vals.easyofficeinventory.com/users/1 and http://sevenvals.easyofficeinventory.com/users/1"
|
|
18
|
+
puts "note how the users/1 is now used to get the sequence number and NOT the unique id of the user"
|
|
19
|
+
exit
|
|
20
|
+
end
|
|
21
|
+
begin
|
|
22
|
+
@parent_resource=eval parent_resource_s.classify
|
|
23
|
+
@nested_resource=eval nested_resource_s.classify
|
|
24
|
+
if((!@parent_resource.ancestors.include?ORM) || (!@nested_resource.ancestors.include?ORM))
|
|
25
|
+
puts "both #{parent_resource_s} and #{nested_resource_s} need to be ActiveRecords"
|
|
26
|
+
exit
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
if((!@nested_resource.new.respond_to? parent_resource_s) & (!@nested_resource.new.respond_to? parent_resource_s.pluralize))
|
|
30
|
+
puts "#{nested_resource_s} should have an association with #{parent_resource_s} otherwise its not possible to have a unique sequence number to identify #{nested_resource_s} from the url"
|
|
31
|
+
#TODO need to check for belongs_to relation
|
|
32
|
+
exit
|
|
33
|
+
end
|
|
34
|
+
#if not belongs to
|
|
35
|
+
if(!@parent_resource.new.respond_to? @nested_resource.to_s.downcase.pluralize)
|
|
36
|
+
exit unless yes?("The resource #{nested_resource_s} should have a belongs to association with #{parent_resource_s}, do you still want to continue (risky)?")
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
rescue =>e
|
|
40
|
+
puts "both #{parent_resource_s} and #{nested_resource_s} need to be ActiveRecords #{e.message}"
|
|
41
|
+
exit
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def do_work
|
|
46
|
+
#create migration and run migrate script
|
|
47
|
+
migration_template "migration.rb", "db/migrate/add_sequence_num_to_#{@nested_resource.to_s.downcase.pluralize}"
|
|
48
|
+
#inject into model class module includeA
|
|
49
|
+
@model_path ||= File.join("app", "models", "#{@nested_resource.to_s.underscore}.rb")
|
|
50
|
+
inject_into_class(@model_path,@nested_resource,"sequenceid :'#{@parent_resource.to_s}','#{@nested_resource.to_s.downcase.pluralize}\n")
|
|
51
|
+
end
|
|
52
|
+
end
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
class AddSequenceNumTo<%= @nested_resource.to_s.downcase.pluralize.camelize %> < ActiveRecord::Migration
|
|
2
|
+
<% @parent_resource_name=@parent_resource.to_s.downcase%>
|
|
3
|
+
<% @nested_resource_name=@nested_resource.to_s.downcase.pluralize%>
|
|
4
|
+
def self.up
|
|
5
|
+
add_column :<%= @nested_resource_name %>, :sequence_num, :integer,:null=>false
|
|
6
|
+
update_sequence_num_values
|
|
7
|
+
add_index :<%= @nested_resource_name%>, [:sequence_num,:<%=@parent_resource_name%>_id], :unique=>true
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def self.down
|
|
11
|
+
remove_index :<%=@nested_resource_name%>,:column=>[:sequence_num,:<%=@parent_resource_name%>_id]
|
|
12
|
+
remove_column :<%=@nested_resource_name%>, :sequence_num
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def self.update_sequence_num_values
|
|
16
|
+
<%=@parent_resource.to_s%>.all.each do |parent|
|
|
17
|
+
cntr=1
|
|
18
|
+
parent.<%=@nested_resource_name%>.order("created_at").all.each do |nested|
|
|
19
|
+
nested.sequence_num =cntr
|
|
20
|
+
cntr+=1
|
|
21
|
+
nested.save
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
module Sequenceid
|
|
2
|
+
module Logic
|
|
3
|
+
|
|
4
|
+
def sequenceid(parent_rel,current_rel)
|
|
5
|
+
#TODO:need to ensure multiple before_saves are not added - possibly use respond_to?
|
|
6
|
+
class_eval do
|
|
7
|
+
before_create :set_sequence_num
|
|
8
|
+
after_rollback :reset_sequence_num
|
|
9
|
+
include InstanceMethods
|
|
10
|
+
end
|
|
11
|
+
@relation_for_sequence_string=(parent_rel+"."+current_rel)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
module InstanceMethods
|
|
15
|
+
def to_param
|
|
16
|
+
"#{sequence_num}"
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
protected
|
|
20
|
+
def set_sequence_num
|
|
21
|
+
if new_record?
|
|
22
|
+
@relation_sequence=self.send @relation_for_sequence_string
|
|
23
|
+
self.sequence_num=@relation_sequence.order("id").last.try(:sequence_num) || 0
|
|
24
|
+
self.sequence_num+=1
|
|
25
|
+
end
|
|
26
|
+
return true
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def reset_sequence_num
|
|
30
|
+
@save_counter||=1
|
|
31
|
+
if new_record? && @save_counter<3
|
|
32
|
+
logger.info "SQUENCENUM:: attempt number #{@save_counter} of a max 2"
|
|
33
|
+
self.sequence_num=@relation_sequence.order("id").last.try(:sequence_num) +1
|
|
34
|
+
@save_counter+=1
|
|
35
|
+
save
|
|
36
|
+
else
|
|
37
|
+
raise
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
data/lib/sequenceid.rb
ADDED
data/sequenceid.gemspec
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
|
3
|
+
require "sequenceid/version"
|
|
4
|
+
|
|
5
|
+
Gem::Specification.new do |s|
|
|
6
|
+
s.name = "sequenceid"
|
|
7
|
+
s.version = Sequenceid::VERSION
|
|
8
|
+
s.platform = Gem::Platform::RUBY
|
|
9
|
+
s.authors = ["Syed Ali"]
|
|
10
|
+
s.email = ["info@7vals.com"]
|
|
11
|
+
s.homepage = ""
|
|
12
|
+
s.summary = %q{Create resource URL's starting from 1 for each SaaS company}
|
|
13
|
+
s.description = %q{For SaaS applications, there are times when we dont want to show the resource id to the user since its being shared with multiple companies that have signed up. And for scenarios where we want the URL to show numbers and not worded URL's like what friendly_id provide, this is the gem for you! It'll create id's sequentially starting from 1 for each relation parent/nested resource relation defined}
|
|
14
|
+
|
|
15
|
+
s.rubyforge_project = "sequenceid"
|
|
16
|
+
|
|
17
|
+
s.files = `git ls-files`.split("\n")
|
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
|
19
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
|
20
|
+
s.require_paths = ["lib"]
|
|
21
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: sequenceid
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
hash: 29
|
|
5
|
+
prerelease:
|
|
6
|
+
segments:
|
|
7
|
+
- 0
|
|
8
|
+
- 0
|
|
9
|
+
- 1
|
|
10
|
+
version: 0.0.1
|
|
11
|
+
platform: ruby
|
|
12
|
+
authors:
|
|
13
|
+
- Syed Ali
|
|
14
|
+
autorequire:
|
|
15
|
+
bindir: bin
|
|
16
|
+
cert_chain: []
|
|
17
|
+
|
|
18
|
+
date: 2011-05-26 00:00:00 Z
|
|
19
|
+
dependencies: []
|
|
20
|
+
|
|
21
|
+
description: For SaaS applications, there are times when we dont want to show the resource id to the user since its being shared with multiple companies that have signed up. And for scenarios where we want the URL to show numbers and not worded URL's like what friendly_id provide, this is the gem for you! It'll create id's sequentially starting from 1 for each relation parent/nested resource relation defined
|
|
22
|
+
email:
|
|
23
|
+
- info@7vals.com
|
|
24
|
+
executables: []
|
|
25
|
+
|
|
26
|
+
extensions: []
|
|
27
|
+
|
|
28
|
+
extra_rdoc_files: []
|
|
29
|
+
|
|
30
|
+
files:
|
|
31
|
+
- .gitignore
|
|
32
|
+
- Gemfile
|
|
33
|
+
- Rakefile
|
|
34
|
+
- lib/generators/sequenceid/sequenceid_generator.rb
|
|
35
|
+
- lib/generators/sequenceid/template/migration.rb
|
|
36
|
+
- lib/sequenceid.rb
|
|
37
|
+
- lib/sequenceid/sequenceid/model_adapters/active_record_adapter.rb
|
|
38
|
+
- lib/sequenceid/sequenceid/model_adapters/sequenceid_logic.rb
|
|
39
|
+
- lib/sequenceid/sequenceid/version.rb
|
|
40
|
+
- lib/sequenceid/version.rb
|
|
41
|
+
- sequenceid.gemspec
|
|
42
|
+
homepage: ""
|
|
43
|
+
licenses: []
|
|
44
|
+
|
|
45
|
+
post_install_message:
|
|
46
|
+
rdoc_options: []
|
|
47
|
+
|
|
48
|
+
require_paths:
|
|
49
|
+
- lib
|
|
50
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
51
|
+
none: false
|
|
52
|
+
requirements:
|
|
53
|
+
- - ">="
|
|
54
|
+
- !ruby/object:Gem::Version
|
|
55
|
+
hash: 3
|
|
56
|
+
segments:
|
|
57
|
+
- 0
|
|
58
|
+
version: "0"
|
|
59
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
60
|
+
none: false
|
|
61
|
+
requirements:
|
|
62
|
+
- - ">="
|
|
63
|
+
- !ruby/object:Gem::Version
|
|
64
|
+
hash: 3
|
|
65
|
+
segments:
|
|
66
|
+
- 0
|
|
67
|
+
version: "0"
|
|
68
|
+
requirements: []
|
|
69
|
+
|
|
70
|
+
rubyforge_project: sequenceid
|
|
71
|
+
rubygems_version: 1.7.2
|
|
72
|
+
signing_key:
|
|
73
|
+
specification_version: 3
|
|
74
|
+
summary: Create resource URL's starting from 1 for each SaaS company
|
|
75
|
+
test_files: []
|
|
76
|
+
|