sequence_generator 0.1.32 → 2.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.
Files changed (28) hide show
  1. checksums.yaml +4 -4
  2. data/MIT-LICENSE +1 -1
  3. data/README.md +3 -1
  4. data/Rakefile +1 -5
  5. data/app/controllers/sequence_generator/application_controller.rb +1 -39
  6. data/app/controllers/sequence_generator/sequences_controller.rb +10 -45
  7. data/app/models/sequence_generator/current_sequence.rb +16 -0
  8. data/app/models/sequence_generator/sequence.rb +40 -13
  9. data/config/routes.rb +1 -2
  10. data/db/migrate/20180330090901_create_sequence_generator_sequences.rb +1 -1
  11. data/db/migrate/20200412131738_add_name_to_sequence.rb +5 -0
  12. data/db/migrate/20200412131756_create_sequence_generator_current_sequences.rb +11 -0
  13. data/db/migrate/20210324154416_drop_extra_columns_from_sequence.rb +14 -0
  14. data/db/migrate/20210324155232_add_purpose_to_current_sequence.rb +35 -0
  15. data/lib/sequence_generator/engine.rb +1 -9
  16. data/lib/sequence_generator/extender.rb +42 -0
  17. data/lib/sequence_generator/version.rb +1 -1
  18. data/lib/sequence_generator.rb +3 -4
  19. metadata +21 -109
  20. data/app/assets/config/sequence_generator_manifest.js +0 -2
  21. data/app/assets/javascripts/sequence_generator/application.js +0 -13
  22. data/app/assets/stylesheets/sequence_generator/application.css +0 -15
  23. data/app/helpers/sequence_generator/application_helper.rb +0 -4
  24. data/app/models/concerns/act_as_sequenced.rb +0 -111
  25. data/app/serailizers/sequence_generator/base_serializer.rb +0 -19
  26. data/app/serailizers/sequence_generator/sequence_serializer.rb +0 -6
  27. data/app/views/layouts/sequence_generator/application.html.erb +0 -14
  28. data/db/migrate/20190329055517_add_deleted_at_to_sequence_generator_sequence.rb +0 -5
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1025185de92fd0fc9dd71d213bb35f51a77645f9b42f6527306603cd589a7f69
4
- data.tar.gz: 76287379c0f05f3ce439658d5acc4bce44bd9444b4129bdb3f481797ff228343
3
+ metadata.gz: 537d34f1331da9357c2bc822c71e540fd345782c6ddfef6ec6e4270b5dcc827d
4
+ data.tar.gz: 4a2e76439bd0e7e64d79ff46e6c2bea5fa3829231c6906a2f36c5f6cdc0a0801
5
5
  SHA512:
6
- metadata.gz: e090943c1a7be9aebca524454ffcbe1c73ca2f2eb0e7561b6114f13585a7d07390c28417713a79be9c75005d9855bcf175c6bdb4a5e56f0058c2f0a6522c7fce
7
- data.tar.gz: c4b4b5900f13e0b0b7bf050423b93b7146c7e83f4f821a5199293b2d7fb74dc198e0dba62ccc1215de605aa31ba1af97baf9a4dd45495d6979acb16aaab7deae
6
+ metadata.gz: e51a00fd2c6b39460c613649e31bd5c0339ce23749c371194b0d01fa8ab4703d4caa5849c19794b93893d8732a5e2d1957bea7fce7fa975049748c639306abb2
7
+ data.tar.gz: 90df8fb107ed44660cfed091d01e57c72d20fb07c1e737006ac6afb113c35f705d14f479e01d302303057c30fece82d397426daef7ab9fcf868c65a95ff79b60
data/MIT-LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright 2018 vimox-shah
1
+ Copyright 2020 Prasann Shah
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README.md CHANGED
@@ -21,8 +21,10 @@ Or install it yourself as:
21
21
  $ gem install sequence_generator
22
22
  ```
23
23
 
24
+ rails sequence_generator:install:migrations
25
+
24
26
  ## Contributing
25
27
  Contribution directions go here.
26
28
 
27
29
  ## License
28
- The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
30
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile CHANGED
@@ -14,14 +14,11 @@ RDoc::Task.new(:rdoc) do |rdoc|
14
14
  rdoc.rdoc_files.include('lib/**/*.rb')
15
15
  end
16
16
 
17
- APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
17
+ APP_RAKEFILE = File.expand_path("test/dummy/Rakefile", __dir__)
18
18
  load 'rails/tasks/engine.rake'
19
19
 
20
-
21
20
  load 'rails/tasks/statistics.rake'
22
21
 
23
-
24
-
25
22
  require 'bundler/gem_tasks'
26
23
 
27
24
  require 'rake/testtask'
@@ -32,5 +29,4 @@ Rake::TestTask.new(:test) do |t|
32
29
  t.verbose = false
33
30
  end
34
31
 
35
-
36
32
  task default: :test
@@ -1,14 +1,5 @@
1
1
  module SequenceGenerator
2
- class ApplicationController < ActionController::Base
3
- # protect_from_forgery with: :exception
4
-
5
- # rescue_from Exception do |exception|
6
- # unless Rails.env.production?
7
- # print_stack_trace(exception)
8
- # end
9
- # api_error(status: :internal_server_error, message: exception.message)
10
- # end
11
-
2
+ class ApplicationController < ActionController::API
12
3
  rescue_from ActiveRecord::RecordNotFound, with: :resource_not_found
13
4
  rescue_from ActionController::ParameterMissing, with: :parameter_missing
14
5
 
@@ -18,22 +9,6 @@ module SequenceGenerator
18
9
  "\e[#{color_code}m#{text}\e[0m"
19
10
  end
20
11
 
21
- def print_stack_trace(e)
22
- print "\r" << (' ' * 50) << "\n"
23
- stacktrace = e.backtrace.map do |call|
24
- if parts = call.match(/^(?<file>.+):(?<line>\d+):in `(?<code>.*)'$/)
25
- file = parts[:file].sub /^#{Regexp.escape(File.join(Dir.getwd, ''))}/, ''
26
- line = "#{colorize(file, 36)} #{colorize('(', 37)}#{colorize(parts[:line], 32)}#{colorize('): ', 37)} #{colorize(parts[:code], 31)}"
27
- else
28
- line = colorize(call, 31)
29
- end
30
- line
31
- end
32
- puts e.message
33
- stacktrace.each { |line| puts line }
34
- end
35
-
36
-
37
12
  def resource_not_found(exception)
38
13
  api_error(status: :not_found, message: "#{exception.model} for given id Not found")
39
14
  end
@@ -96,18 +71,5 @@ module SequenceGenerator
96
71
  error_hash.merge!(details: errors_array)
97
72
  end
98
73
 
99
-
100
- #
101
- def custom_api_error(status, message, errors)
102
- http_status_code = Rack::Utils.status_code(status)
103
- title = Rack::Utils::HTTP_STATUS_CODES[http_status_code]
104
- error_hash = {status: http_status_code, title: title, message: message, details: errors}
105
- render json: error_hash.to_json, status: status
106
- end
107
-
108
- def api_error_for_transaction(status: 500, message: '', errors: [])
109
- api_error(status: status, message: message, errors: errors)
110
- raise ActiveRecord::Rollback
111
- end
112
74
  end
113
75
  end
@@ -1,12 +1,12 @@
1
1
  module SequenceGenerator
2
2
  class SequencesController < ApplicationController
3
- around_action :transactions_filter, only: %i[create update]
3
+ around_action :transactions_filter, only: %i[create]
4
4
 
5
5
 
6
6
  def create
7
- sequence = Sequence.new(create_sequence_params)
7
+ sequence = ::SequenceGenerator::Sequence.new(create_sequence_params)
8
8
  if sequence.save
9
- render json: sequence, serializer: SequenceSerializer
9
+ render json: sequence
10
10
  else
11
11
  api_error(status: :unprocessable_entity,
12
12
  message: 'sequence creation failed',
@@ -14,28 +14,15 @@ module SequenceGenerator
14
14
  end
15
15
  end
16
16
 
17
- def update
18
- sequence = Sequence.find(params[:id])
19
- if sequence.update(update_sequence_params)
20
- render json: sequence, serializer: SequenceSerializer
21
- else
22
- api_error(status: :unprocessable_entity,
23
- message: 'sequence update failed',
24
- errors: sequence.errors)
25
- end
17
+ def get
18
+ return unless valid_params?(params, [:id])
19
+ render json: ::SequenceGenerator::Sequence.find(params[:id])
26
20
  end
27
21
 
28
22
  def index
29
- sequences = Sequence.where(scope: params[:branch_id], purpose: params[:purpose])
30
- if sequences.present?
31
- render json: {
32
- "sequence_generator/sequences": ActiveModel::Serializer::CollectionSerializer.new(sequences, serializer: SequenceSerializer)
33
- }, status: :ok
34
- else
35
- api_error(status: :unprocessable_entity,
36
- message: 'Sequences Not found'
37
- )
38
- end
23
+ return unless valid_params?(params, [:scope, :purpose])
24
+ sequences = ::SequenceGenerator::Sequence.where(scope: params[:scope], purpose: params[:purpose])
25
+ render json: sequences, status: :ok
39
26
  end
40
27
 
41
28
  private
@@ -48,32 +35,10 @@ module SequenceGenerator
48
35
 
49
36
 
50
37
  def create_sequence_params
51
- params.permit(:sequence_prefix,
52
- :sequential_id,
53
- :start_at,
54
- :valid_from,
55
- :valid_till,
56
- :reset_from_next_year,
38
+ params.permit(:name,
57
39
  :purpose,
58
- :financial_year_start,
59
- :financial_year_end,
60
40
  :scope)
61
41
  end
62
42
 
63
-
64
- def update_sequence_params
65
- params.permit(:sequence_prefix,
66
- :sequential_id,
67
- :start_at,
68
- :valid_from,
69
- :valid_till,
70
- :reset_from_next_year,
71
- :purpose,
72
- :financial_year_start,
73
- :financial_year_end,
74
- :scope)
75
- end
76
-
77
-
78
43
  end
79
44
  end
@@ -0,0 +1,16 @@
1
+ module SequenceGenerator
2
+ class CurrentSequence < ApplicationRecord
3
+ validates_presence_of :name, :current, :scope, :purpose
4
+ validates_uniqueness_of :name, scope: [:scope, :purpose]
5
+
6
+ def self.get_next_number(prefix, scope, purpose)
7
+ current_sequence = CurrentSequence.lock.where(name: prefix, scope: scope, purpose: purpose).first
8
+ if current_sequence
9
+ current_sequence.update!(current: current_sequence.current + 1)
10
+ else
11
+ current_sequence = CurrentSequence.create!(name: prefix, scope: scope, purpose: purpose, current: 1)
12
+ end
13
+ current_sequence.current
14
+ end
15
+ end
16
+ end
@@ -1,21 +1,48 @@
1
- require_relative '../concerns/act_as_sequenced'
2
1
  module SequenceGenerator
3
2
  class Sequence < ApplicationRecord
4
- include SequenceGenerator::ActsAsSequenced
5
- acts_as_paranoid
3
+ validates_presence_of :name, :scope, :purpose
4
+ validates_uniqueness_of :name, scope: [:scope, :purpose]
6
5
 
7
- before_validation :validate_sequential_id
6
+ def generate_next(options, model)
7
+ date_to_consider = Time.now
8
+ if options[:date_column] && model.send(options[:date_column])
9
+ date_to_consider = model.send(options[:date_column])
10
+ end
8
11
 
9
- def validate_sequential_id
10
- validates_presence_of :sequential_id, :purpose
11
- end
12
+ prefix = name.gsub(/\([^()]*?\)/) do |x|
13
+ fragment = x[1..-2]
14
+ case fragment
15
+ when "YYYY"
16
+ date_to_consider.strftime('%Y')
17
+ when "YY"
18
+ date_to_consider.strftime('%y')
19
+ when "IFYY"
20
+ financial_year_start_date = DateTime.new(date_to_consider.year, 4, 1, 0, 0, 0, Rational(5.5,24))
21
+ if date_to_consider > financial_year_start_date
22
+ "#{date_to_consider.strftime('%y')}-#{date_to_consider.next_year.strftime('%y')}"
23
+ else
24
+ "#{date_to_consider.prev_year.strftime('%y')}-#{date_to_consider.strftime('%y')}"
25
+ end
26
+ when "IFY"
27
+ financial_year_start_date = DateTime.new(date_to_consider.year, 4, 1, 0, 0, 0, Rational(5.5,24))
28
+ if date_to_consider > financial_year_start_date
29
+ "#{date_to_consider.strftime('%y')}#{date_to_consider.next_year.strftime('%y')}"
30
+ else
31
+ "#{date_to_consider.prev_year.strftime('%y')}#{date_to_consider.strftime('%y')}"
32
+ end
33
+ when "MM"
34
+ date_to_consider.strftime('%m')
35
+ else
36
+ model.send(fragment)
37
+ end
38
+ end
12
39
 
13
- def generate_sequence_number
14
- sequence_number = "%0#{max_prefix_number}d" % (sequential_id + 1).to_s
15
- generated_sequential_id = "#{sequence_prefix}#{sequence_number}"
16
- self.update(sequential_id: sequential_id + 1)
17
- return generated_sequential_id
40
+ prefix = prefix[/#*$/] == "" ? prefix + "#####" : prefix
41
+ digits = prefix[/#*$/].length
42
+ prefix_without_digits = prefix.split(/#*$/)[0]
43
+ next_number = CurrentSequence.get_next_number(prefix_without_digits, scope, purpose)
44
+ sequence_number = "%0#{digits}d" % (next_number).to_s
45
+ prefix_without_digits + sequence_number
18
46
  end
19
-
20
47
  end
21
48
  end
data/config/routes.rb CHANGED
@@ -1,4 +1,3 @@
1
1
  SequenceGenerator::Engine.routes.draw do
2
- resources :sequences, only: [:create, :update, :index]
3
- # get '/sequences/fetch', to: 'sequences#fetch', controller: 'sequences'
2
+ resources :sequences, only: [:create, :get, :index]
4
3
  end
@@ -1,4 +1,4 @@
1
- class CreateSequenceGeneratorSequences < ActiveRecord::Migration[5.0]
1
+ class CreateSequenceGeneratorSequences < ActiveRecord::Migration[5.2]
2
2
  def change
3
3
  create_table :sequence_generator_sequences do |t|
4
4
 
@@ -0,0 +1,5 @@
1
+ class AddNameToSequence < ActiveRecord::Migration[5.2]
2
+ def change
3
+ add_column :sequence_generator_sequences, :name, :string
4
+ end
5
+ end
@@ -0,0 +1,11 @@
1
+ class CreateSequenceGeneratorCurrentSequences < ActiveRecord::Migration[5.2]
2
+ def change
3
+ create_table :sequence_generator_current_sequences do |t|
4
+ t.string :name, null: false
5
+ t.column :current, :integer, default: 1, null: false
6
+ t.string :scope, null: false
7
+
8
+ t.timestamps
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,14 @@
1
+ class DropExtraColumnsFromSequence < ActiveRecord::Migration[5.2]
2
+ def change
3
+ remove_columns :sequence_generator_sequences,
4
+ :sequence_prefix,
5
+ :sequential_id,
6
+ :start_at,
7
+ :valid_from,
8
+ :valid_till,
9
+ :reset_from_next_year,
10
+ :financial_year_start,
11
+ :financial_year_end,
12
+ :max_prefix_number
13
+ end
14
+ end
@@ -0,0 +1,35 @@
1
+ class AddPurposeToCurrentSequence < ActiveRecord::Migration[5.2]
2
+ def change
3
+ add_column :sequence_generator_current_sequences, :purpose, :string
4
+ SequenceGenerator::Sequence.all.map do |sequence|
5
+ current_sequences = SequenceGenerator::CurrentSequence.where(scope: sequence.scope)
6
+ prefix = sequence.name.gsub(/\([^()]*?\)/) do |x|
7
+ fragment = x[1..-2]
8
+ case fragment
9
+ when "YYYY"
10
+ '(2022|2021|2020|2019|2018)'
11
+ when "YY"
12
+ '(22|21|20|19|18)'
13
+ when "IFYY"
14
+ '(21-22|20-21|19-20|18-19|17-18)'
15
+ when "IFY"
16
+ '(2122|2021|1920|1819|1718)'
17
+ when "MM"
18
+ '(01|02|03|04|05|06|07|08|09|10|11|12)'
19
+ else
20
+ '.{1,4}'
21
+ end
22
+ end
23
+ prefix = prefix.gsub('#','')
24
+ prefix = prefix + '$'
25
+
26
+ current_sequences.map do |current_sequence|
27
+ next if current_sequence.purpose
28
+ if current_sequence.name.match Regexp.new(prefix)
29
+ current_sequence.purpose = sequence.purpose
30
+ current_sequence.save!
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -1,14 +1,6 @@
1
1
  module SequenceGenerator
2
2
  class Engine < ::Rails::Engine
3
3
  isolate_namespace SequenceGenerator
4
-
5
- initializer "sequence_generator", before: :load_config_initializers do |app|
6
- Rails.application.routes.append do
7
- mount SequenceGenerator::Engine, at: "/sequence_generator"
8
- end
9
- SequenceGenerator.load_files.each { |file|
10
- require_relative File.join("../..", file)
11
- }
12
- end
4
+ config.generators.api_only = true
13
5
  end
14
6
  end
@@ -0,0 +1,42 @@
1
+ module SequenceGenerator
2
+ module Extender
3
+
4
+ def acts_as_sequenced(options = {})
5
+ include SequenceGenerator::Generator
6
+
7
+ [:purpose, :scope, :column].each do |option|
8
+ raise StandardError.new("#{option} option needs to be provided to acts_as_sequenced") unless options[option].present?
9
+ end
10
+
11
+ options[:validation_options] ||= {on: :create}
12
+ options[:validation_options][:on] ||= :create
13
+ before_validation options[:validation_options] do
14
+ generate_sequence(options)
15
+ end
16
+ end
17
+ end
18
+
19
+ module Generator
20
+ def self.included(base)
21
+ def generate_sequence(options)
22
+ return if send(options[:column]).present?
23
+ sequence = Sequence.find_by!(purpose: options[:purpose], scope: send(options[:scope]))
24
+ assign_attributes(options[:column]=> sequence.generate_next(options, self))
25
+ end
26
+ end
27
+ end
28
+ end
29
+
30
+
31
+ ## TODO: Tests to be written ##
32
+
33
+ # Same model multiple sequence
34
+ # validation options... on create, update
35
+ # current sequence missing
36
+ # current sequence there
37
+ # No # in the sequence
38
+ # Method not defined in the sequence
39
+ # Basic: always generates just +1
40
+ # Model validations
41
+ # options like YY MM etc
42
+ # simulate locking
@@ -1,3 +1,3 @@
1
1
  module SequenceGenerator
2
- VERSION = '0.1.32'
2
+ VERSION = '2.0.1'
3
3
  end
@@ -1,9 +1,8 @@
1
1
  require "sequence_generator/engine"
2
2
 
3
3
  module SequenceGenerator
4
- # Your code goes here...
5
- def self.load_files
6
- ["app/models/sequence_generator/sequence.rb", "app/models/concerns/act_as_sequenced.rb"]
4
+ if defined?(ActiveRecord::Base)
5
+ require "sequence_generator/extender"
6
+ ActiveRecord::Base.extend SequenceGenerator::Extender
7
7
  end
8
-
9
8
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sequence_generator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.32
4
+ version: 2.0.1
5
5
  platform: ruby
6
6
  authors:
7
- - vimox-shah
8
- autorequire:
7
+ - Prasann Shah
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-04-06 00:00:00.000000000 Z
11
+ date: 2021-09-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -16,115 +16,31 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 5.0.0
19
+ version: 5.2.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: 5.0.0
26
+ version: 5.2.0
27
27
  - !ruby/object:Gem::Dependency
28
- name: pg
28
+ name: sqlite3
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
- type: :runtime
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - ">="
39
- - !ruby/object:Gem::Version
40
- version: '0'
41
- - !ruby/object:Gem::Dependency
42
- name: bundler
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - ">="
46
- - !ruby/object:Gem::Version
47
- version: '1.16'
48
- type: :runtime
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - ">="
53
- - !ruby/object:Gem::Version
54
- version: '1.16'
55
- - !ruby/object:Gem::Dependency
56
- name: rake
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - ">="
60
- - !ruby/object:Gem::Version
61
- version: '10.0'
62
- type: :runtime
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - ">="
67
- - !ruby/object:Gem::Version
68
- version: '10.0'
69
- - !ruby/object:Gem::Dependency
70
- name: activesupport
71
- requirement: !ruby/object:Gem::Requirement
72
- requirements:
73
- - - ">="
74
- - !ruby/object:Gem::Version
75
- version: '3.0'
76
- type: :runtime
77
- prerelease: false
78
- version_requirements: !ruby/object:Gem::Requirement
79
- requirements:
80
- - - ">="
81
- - !ruby/object:Gem::Version
82
- version: '3.0'
83
- - !ruby/object:Gem::Dependency
84
- name: activerecord
85
- requirement: !ruby/object:Gem::Requirement
86
- requirements:
87
- - - ">="
88
- - !ruby/object:Gem::Version
89
- version: '3.0'
90
- type: :runtime
91
- prerelease: false
92
- version_requirements: !ruby/object:Gem::Requirement
93
- requirements:
94
- - - ">="
95
- - !ruby/object:Gem::Version
96
- version: '3.0'
97
- - !ruby/object:Gem::Dependency
98
- name: active_model_serializers
99
- requirement: !ruby/object:Gem::Requirement
100
- requirements:
101
- - - "~>"
102
- - !ruby/object:Gem::Version
103
- version: 0.10.3
104
- type: :runtime
105
- prerelease: false
106
- version_requirements: !ruby/object:Gem::Requirement
107
- requirements:
108
- - - "~>"
109
- - !ruby/object:Gem::Version
110
- version: 0.10.3
111
- - !ruby/object:Gem::Dependency
112
- name: rack-cors
113
- requirement: !ruby/object:Gem::Requirement
114
- requirements:
115
- - - ">="
116
- - !ruby/object:Gem::Version
117
- version: '0'
118
- type: :runtime
34
+ type: :development
119
35
  prerelease: false
120
36
  version_requirements: !ruby/object:Gem::Requirement
121
37
  requirements:
122
38
  - - ">="
123
39
  - !ruby/object:Gem::Version
124
40
  version: '0'
125
- description: You can create sequence for specific purpose with unique scope
41
+ description: Formatted sequence for rails models with unique scope
126
42
  email:
127
- - vimox@shipmnts.com
43
+ - prasann@shipmnts.com
128
44
  executables: []
129
45
  extensions: []
130
46
  extra_rdoc_files: []
@@ -132,32 +48,29 @@ files:
132
48
  - MIT-LICENSE
133
49
  - README.md
134
50
  - Rakefile
135
- - app/assets/config/sequence_generator_manifest.js
136
- - app/assets/javascripts/sequence_generator/application.js
137
- - app/assets/stylesheets/sequence_generator/application.css
138
51
  - app/controllers/sequence_generator/application_controller.rb
139
52
  - app/controllers/sequence_generator/sequences_controller.rb
140
- - app/helpers/sequence_generator/application_helper.rb
141
53
  - app/jobs/sequence_generator/application_job.rb
142
54
  - app/mailers/sequence_generator/application_mailer.rb
143
- - app/models/concerns/act_as_sequenced.rb
144
55
  - app/models/sequence_generator/application_record.rb
56
+ - app/models/sequence_generator/current_sequence.rb
145
57
  - app/models/sequence_generator/sequence.rb
146
- - app/serailizers/sequence_generator/base_serializer.rb
147
- - app/serailizers/sequence_generator/sequence_serializer.rb
148
- - app/views/layouts/sequence_generator/application.html.erb
149
58
  - config/routes.rb
150
59
  - db/migrate/20180330090901_create_sequence_generator_sequences.rb
151
- - db/migrate/20190329055517_add_deleted_at_to_sequence_generator_sequence.rb
60
+ - db/migrate/20200412131738_add_name_to_sequence.rb
61
+ - db/migrate/20200412131756_create_sequence_generator_current_sequences.rb
62
+ - db/migrate/20210324154416_drop_extra_columns_from_sequence.rb
63
+ - db/migrate/20210324155232_add_purpose_to_current_sequence.rb
152
64
  - lib/sequence_generator.rb
153
65
  - lib/sequence_generator/engine.rb
66
+ - lib/sequence_generator/extender.rb
154
67
  - lib/sequence_generator/version.rb
155
68
  - lib/tasks/sequence_generator_tasks.rake
156
- homepage: https://github.com/vimox-shah/sequence-generator
69
+ homepage: https://github.com/shipmnts/sequence_generator
157
70
  licenses:
158
71
  - MIT
159
72
  metadata: {}
160
- post_install_message:
73
+ post_install_message:
161
74
  rdoc_options: []
162
75
  require_paths:
163
76
  - lib
@@ -172,9 +85,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
172
85
  - !ruby/object:Gem::Version
173
86
  version: '0'
174
87
  requirements: []
175
- rubyforge_project:
176
- rubygems_version: 2.7.6
177
- signing_key:
88
+ rubygems_version: 3.0.6
89
+ signing_key:
178
90
  specification_version: 4
179
- summary: This is for Sequence generation
91
+ summary: Generate sequences formatted as a string for the users
180
92
  test_files: []
@@ -1,2 +0,0 @@
1
- //= link_directory ../javascripts/sequence_generator .js
2
- //= link_directory ../stylesheets/sequence_generator .css
@@ -1,13 +0,0 @@
1
- // This is a manifest file that'll be compiled into application.js, which will include all the files
2
- // listed below.
3
- //
4
- // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
- // or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
6
- //
7
- // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
- // compiled file. JavaScript code in this file should be added after the last require_* statement.
9
- //
10
- // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
11
- // about supported directives.
12
- //
13
- //= require_tree .
@@ -1,15 +0,0 @@
1
- /*
2
- * This is a manifest file that'll be compiled into application.css, which will include all the files
3
- * listed below.
4
- *
5
- * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
- * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
- *
8
- * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
- * compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
10
- * files in this directory. Styles in this file should be added after the last require_* statement.
11
- * It is generally better to create a new file per style scope.
12
- *
13
- *= require_tree .
14
- *= require_self
15
- */
@@ -1,4 +0,0 @@
1
- module SequenceGenerator
2
- module ApplicationHelper
3
- end
4
- end
@@ -1,111 +0,0 @@
1
- require 'active_support/core_ext/hash/slice'
2
- require 'active_support/core_ext/class/attribute_accessors'
3
-
4
- module SequenceGenerator
5
- module ActsAsSequenced
6
- extend ActiveSupport::Concern
7
- DEFAULT_OPTIONS = {
8
- purpose: 'Sequence',
9
- sequential_id: 0,
10
- start_at: 1
11
- }.freeze
12
-
13
- ColumnWithSamePurposeExists = Class.new(StandardError)
14
-
15
- def self.included(base)
16
- base.extend ClassMethods
17
- end
18
-
19
- module ClassMethods
20
- def act_as_sequenced(options= {})
21
- unless defined?(sequenced_options)
22
- include SequenceGenerator::ActsAsSequenced::InstanceMethods
23
-
24
- attr_accessor :sequence_generator_id
25
- mattr_accessor :sequenced_options, instance_accessor: false
26
- self.sequenced_options = []
27
- end
28
- options[:validation_options] ||= {on: :create}
29
- options[:validation_options][:on] ||= :create
30
- options = DEFAULT_OPTIONS.merge(options)
31
- before_validation options[:validation_options] do
32
- set_sequential_ids(options)
33
- end
34
- column_name = options[:column]
35
- purpose = options[:purpose]
36
-
37
- if sequenced_options.any? { |options| options[:column] == column_name && options[:purpose] == purpose }
38
- raise(ColumnWithSamePurposeExists, <<-MSG.squish)
39
- Tried to set #{column_name} as sequenced but there was already a
40
- definition here. Did you accidentally call acts_as_sequenced
41
- multiple times on the same column with same purpose?
42
- MSG
43
- else
44
- sequenced_options << options
45
- end
46
- end
47
- end
48
-
49
- module InstanceMethods
50
- def set_sequential_ids(options)
51
- return if send(options[:column]).present?
52
- prefix_id = options[:prefix_column] ? self[options[:prefix_column]] : self.sequence_generator_id
53
- if prefix_id.present?
54
- sequence = Sequence.find(prefix_id)
55
- if sequence.scope.to_s == send(options[:scope]).to_s
56
- assign_attributes(options[:column]=> sequence.generate_sequence_number)
57
- else
58
- errors.add(:sequential_id, 'Sequence is not associated with your branch')
59
- end
60
- else
61
- date_to_consider = Time.now
62
- if options[:date_column] && send(options[:date_column])
63
- date_to_consider = send(options[:date_column])
64
- end
65
- sequence = Sequence.where(purpose: options[:purpose], scope: send(options[:scope]))
66
- .where('valid_from <= ? AND valid_till >= ?', date_to_consider, date_to_consider).first
67
- unless self.as_json[options[:column]].present?
68
- if sequence
69
- sequence.lock!
70
- assign_attributes(options[:column]=> sequence.generate_sequence_number)
71
- else
72
- original_sequence = Sequence.where(purpose: options[:purpose], scope: send(options[:scope])).last
73
- if original_sequence.nil?
74
- errors.add(:sequential_id, 'Sequence is not created')
75
- else
76
- valid_from = original_sequence.valid_from
77
- valid_till = original_sequence.valid_till
78
- new_start_at = original_sequence.sequential_id
79
- difference = (valid_till - valid_from).to_i
80
- new_valid_from = Date.today
81
- new_valid_till = new_valid_from + difference
82
- if original_sequence.reset_from_next_year
83
- sequence = Sequence.create!(original_sequence.as_json.except('id', 'start_at',
84
- 'valid_from', 'valid_till',
85
- 'sequential_id',
86
- 'created_at', 'updated_at')
87
- .merge!(start_at: 1,
88
- valid_from: new_valid_from,
89
- valid_till: new_valid_till,
90
- sequential_id: 1))
91
- else
92
- sequence = Sequence.create!(original_sequence.as_json.except('id', 'start_at',
93
- 'valid_from', 'valid_till',
94
- 'sequential_id',
95
- 'created_at', 'updated_at')
96
- .merge!(start_at: new_start_at,
97
- valid_from: new_valid_from,
98
- valid_till: new_valid_till,
99
- sequential_id: new_start_at))
100
- end
101
- assign_attributes(options[:column]=> sequence.generate_sequence_number)
102
- end
103
- end
104
- end
105
- end
106
- end
107
- end
108
- end
109
- end
110
-
111
- # ActiveRecord::Base.send(:include, SequenceGenerator::ActsAsSequenced)
@@ -1,19 +0,0 @@
1
- module SequenceGenerator
2
- class BaseSerializer < ActiveModel::Serializer
3
-
4
- attributes :id, :created_at, :updated_at
5
-
6
- def id
7
- object.id.to_s if object.id.present?
8
- end
9
-
10
- def created_at
11
- object.created_at.to_i if object.created_at
12
- end
13
-
14
- def updated_at
15
- object.updated_at.to_i if object.updated_at
16
- end
17
-
18
- end
19
- end
@@ -1,6 +0,0 @@
1
- module SequenceGenerator
2
- class SequenceSerializer < BaseSerializer
3
- attributes :id, :sequence_prefix, :sequential_id, :start_at, :valid_from, :valid_till,
4
- :reset_from_next_year, :purpose, :financial_year_start, :financial_year_end, :scope
5
- end
6
- end
@@ -1,14 +0,0 @@
1
- <!DOCTYPE html>
2
- <html>
3
- <head>
4
- <title>Sequence generator</title>
5
- <%= stylesheet_link_tag "sequence_generator/application", media: "all" %>
6
- <%= javascript_include_tag "sequence_generator/application" %>
7
- <%= csrf_meta_tags %>
8
- </head>
9
- <body>
10
-
11
- <%= yield %>
12
-
13
- </body>
14
- </html>
@@ -1,5 +0,0 @@
1
- class AddDeletedAtToSequenceGeneratorSequence < ActiveRecord::Migration[5.2]
2
- def change
3
- add_column :sequence_generator_sequences, :deleted_at, :timestamp
4
- end
5
- end