Skip to content

Commit

Permalink
Allow using idy with namespaced models
Browse files Browse the repository at this point in the history
  • Loading branch information
vitobotta committed May 9, 2020
1 parent f44f010 commit c390493
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 7 deletions.
4 changes: 2 additions & 2 deletions lib/idy/extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ def idy_decode(hash, salt: self.salt)
end

def idy_default_salt
alphabet = Array('a'..'z') + [":"]
alphabet = Array('a'..'z')

indexes = name.downcase.split('').map do |char|
indexes = name.split("::").last.downcase.split('').map do |char|
alphabet.index(char) + 1
end

Expand Down
20 changes: 15 additions & 5 deletions spec/lib/idy/extension/idy_default_salt_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,20 @@

require 'rails_helper'

RSpec.describe Abcdefghijklm, '#idy_default_salt' do
context 'when class name has a big length' do
it 'is bases on the first 10 class letter position index on alphabet' do
expect(described_class.idy_default_salt).to eq '12345678910'
RSpec.describe '#idy_default_salt' do
describe Abcdefghijklm do
context 'when class name has a big length' do
it 'is bases on the first 10 class letter position index on alphabet' do
expect(described_class.idy_default_salt).to eq '12345678910'
end
end
end

describe Namespace::NsModel do
context 'when model is namespaced into a module' do
it 'does not take into account the namespace' do
expect(described_class.idy_default_salt).to eq '141913154512'
end
end
end
end
end
3 changes: 3 additions & 0 deletions spec/support/db/migrate/create_tables.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ def change
create_table :abcdefghijklms do |t|
end

create_table :namespace_ns_models do |t|
end

create_table :articles do |t|
t.string :title
end
Expand Down
10 changes: 10 additions & 0 deletions spec/support/models/ns_model.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

module Namespace
def self.table_name_prefix
'namespace_'
end

class NsModel < ActiveRecord::Base
end
end

0 comments on commit c390493

Please sign in to comment.