Module: Voltron::Encryptable::InstanceMethods
- Defined in:
- lib/voltron/encryptable.rb
Instance Method Summary collapse
- #assign_attributes(new_attributes) ⇒ Object
- #find_id ⇒ Object
- #reload(options = nil) ⇒ Object
- #to_param ⇒ Object
- #update_attributes(attributes) ⇒ Object
Instance Method Details
#assign_attributes(new_attributes) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/voltron/encryptable.rb', line 66 def assign_attributes(new_attributes) new_attributes.stringify_keys! belongs = self.class.reflect_on_all_associations(:belongs_to).map { |b| { column: "#{b.name}_id", class_name: (b.[:class_name] || b.name).to_s.classify } } belongs.each do |belong| begin klass = belong[:class_name].safe_constantize value = new_attributes[belong[:column]] next if !klass.has_encrypted_id? || value.blank? record = klass.find(value) new_attributes[belong[:column]] = record.id rescue NameError, ActiveRecord::RecordNotFound end end super(new_attributes) end |
#find_id ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/voltron/encryptable.rb', line 109 def find_id # Helps determine the min/max value. For example if amount is 1100, min will be 1024, if amount is 947, min will be 1 amount = Voltron::Id.count.to_f # The range of ids from which we try to find an unused id, i.e. - (1..1024), (1025..2048), (2049..3073) factor = 1024.to_f # Get the min and max value of the range segment min = (((amount/factor).floor*factor) + 1).to_i max = (((amount+1)/factor).ceil*factor).to_i # Get all ids in the determined range segment, these are the ids we cannot choose from used = Voltron::Id.where(id: min..max).pluck(:id) # Get the candidates, an array of values from min to max, subtracting the used values from above candidates = ((min..max).to_a - used).shuffle # Get the first candidate, the first value off the shuffled array candidate = candidates.shift # Check for blacklisted words given the candidate id while crypt.blacklisted?(candidate) do # If id chosen is a blacklisted word, insert it as a placeholder record and try again Voltron::Id.create(id: candidate, resource_id: 0, resource_type: :blacklist) # If no more candidates to choose from, re-run find_id, it will get a new set of candidates from the next segment return find_id if candidates.empty? # Pick the next candidate candidate = candidates.shift end # The id chosen is good, not a blacklisted word. Use it candidate end |
#reload(options = nil) ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/voltron/encryptable.rb', line 143 def reload( = nil) clear_aggregation_cache clear_association_cache self.class.connection.clear_query_cache fresh_object = if && [:lock] self.class.unscoped { self.class.lock([:lock]).find_by(id: encryptable.resource_id) } else self.class.unscoped { self.class.find(encryptable.resource_id, bypass: true) } end @attributes = fresh_object.instance_variable_get("@attributes") @new_record = false self end |
#to_param ⇒ Object
102 103 104 105 106 107 |
# File 'lib/voltron/encryptable.rb', line 102 def to_param return super if encryptable.nil? crypt = Voltron::Encrypt.new crypt.encode(encryptable.id) end |
#update_attributes(attributes) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/voltron/encryptable.rb', line 84 def update_attributes(attributes) attributes.stringify_keys! belongs = self.class.reflect_on_all_associations(:belongs_to).map { |b| { column: "#{b.name}_id", class_name: (b.[:class_name] || b.name).to_s.classify } } belongs.each do |belong| begin klass = belong[:class_name].safe_constantize value = attributes[belong[:column]] next if !klass.has_encrypted_id? || value.blank? record = klass.find(value) attributes[belong[:column]] = record.id rescue NameError, ActiveRecord::RecordNotFound end end super(attributes) end |