Module: ActiveRecordMask::InstanceMethods

Defined in:
lib/active_record_mask/active_record_mask.rb

Instance Method Summary collapse

Instance Method Details

#_read_attribute(attr_name, &block) ⇒ Object



75
76
77
78
79
80
81
82
83
# File 'lib/active_record_mask/active_record_mask.rb', line 75

def _read_attribute(attr_name, &block)
  return super(attr_name, &block) if @show_real_data

  if self.class.allowed_attributes.include?(attr_name.to_sym) || self.class.allowed_attributes.empty?
    return super(attr_name, &block)
  end

  empty_value_for_attribute(attr_name)
end

#empty_value_for_attribute(attr_name) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/active_record_mask/active_record_mask.rb', line 85

def empty_value_for_attribute(attr_name)
  column = self.class.columns_hash[attr_name.to_s]
  return nil unless column

  if self.class.revealed_defaults.key?(attr_name.to_sym)
    return self.class.revealed_defaults[attr_name.to_sym]
  end

  case column.type
  when :string, :text
    if (column.try(:array) rescue false)
      []
    else
      ""
    end
  when :integer, :float, :decimal
    0
  when :datetime, :timestamp, :time, :date
    nil
  when :boolean
    false
  when :json, :jsonb, :hstore
    {}
  when :array
    []
  else
    nil
  end
end