Class: Flattener::Column
- Inherits:
-
Object
- Object
- Flattener::Column
- Includes:
- ActiveModel::Model
- Defined in:
- lib/has_helpers/flattener/column.rb,
lib/has_helpers/flattener/column/types.rb
Defined Under Namespace
Modules: Types
Constant Summary collapse
- TIMESTAMP_COLUMNS =
["created_at", "updated_at"]
Instance Attribute Summary collapse
-
#alias_name ⇒ Object
Returns the value of attribute alias_name.
-
#association ⇒ Object
Returns the value of attribute association.
-
#expand_on ⇒ Array
Returns the column's expansions.
-
#klass ⇒ Object
Returns the value of attribute klass.
- #partial_type_id ⇒ Object
-
#reject_if ⇒ Object
Returns the value of attribute reject_if.
-
#sql_column ⇒ Object
Returns the value of attribute sql_column.
- #type ⇒ Object
Class Method Summary collapse
Instance Method Summary collapse
- #description ⇒ Object
- #is_currency? ⇒ Boolean
- #is_percent? ⇒ Boolean
- #is_primary_key? ⇒ Boolean
- #is_timestamp? ⇒ Boolean
- #update(args) ⇒ Object
Instance Attribute Details
#alias_name ⇒ Object
Returns the value of attribute alias_name.
10 11 12 |
# File 'lib/has_helpers/flattener/column.rb', line 10 def alias_name @alias_name end |
#association ⇒ Object
Returns the value of attribute association.
10 11 12 |
# File 'lib/has_helpers/flattener/column.rb', line 10 def association @association end |
#expand_on ⇒ Array
Returns the column's expansions
Example
- array of strings (default expansion column is
type) update_column :override, expand_on: ["Approved", "Submitted"]- array of hashes
update_column :amount, expand_on: [{value: "Single", column_name: "product_option"}, {value: "Target", column_name: "product_option"}]- proc
`update_column :amount, expand_on: proc { ProductType.all.map(&:name) }
51 52 53 54 55 56 57 58 |
# File 'lib/has_helpers/flattener/column.rb', line 51 def return @expand_on if @expand_on_memoized @expand_on_memoized = true @expand_on = @expand_on.call if @expand_on.is_a?(Proc) @expand_on = Array(@expand_on).map do |expansion| expansion.is_a?(Hash) ? expansion : { value: expansion, column_name: "type" } end end |
#klass ⇒ Object
Returns the value of attribute klass.
10 11 12 |
# File 'lib/has_helpers/flattener/column.rb', line 10 def klass @klass end |
#partial_type_id ⇒ Object
73 74 75 76 77 78 79 |
# File 'lib/has_helpers/flattener/column.rb', line 73 def partial_type_id if @partial_type_id.is_a?(Proc) @partial_type_id.call else @partial_type_id end end |
#reject_if ⇒ Object
Returns the value of attribute reject_if.
10 11 12 |
# File 'lib/has_helpers/flattener/column.rb', line 10 def reject_if @reject_if end |
#sql_column ⇒ Object
Returns the value of attribute sql_column.
10 11 12 |
# File 'lib/has_helpers/flattener/column.rb', line 10 def sql_column @sql_column end |
#type ⇒ Object
81 82 83 |
# File 'lib/has_helpers/flattener/column.rb', line 81 def type @type ||= ::Flattener::Column::Types.sql_type_to_type(sql_type) end |
Class Method Details
.build_sql_column(name, sql_type) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/has_helpers/flattener/column.rb', line 85 def self.build_sql_column(name, sql_type) cast_type = ActiveRecord::Base.connection.send(:lookup_cast_type, sql_type) || ActiveRecord::Type.lookup(sql_type.to_s.sub(/\(.+\)\z/, "").to_sym) OpenStruct.new( name: name.to_s, sql_type: sql_type, cast_type: cast_type ) end |
Instance Method Details
#description ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/has_helpers/flattener/column.rb', line 60 def description @description ||= ActiveRecord::Base.connection.execute(<<-SQL).values[0][0] SELECT d.description FROM pg_attribute a LEFT JOIN pg_description d ON a.attrelid = d.objoid AND a.attnum = d.objsubid WHERE a.attrelid = '#{table_name}'::regclass AND a.attnum > 0 AND NOT a.attisdropped AND a.attname = '#{name}'; SQL end |
#is_currency? ⇒ Boolean
36 37 38 |
# File 'lib/has_helpers/flattener/column.rb', line 36 def is_currency? sql_column.cast_type.scale == 2 || sql_column.cast_type.scale == 5 end |
#is_percent? ⇒ Boolean
32 33 34 |
# File 'lib/has_helpers/flattener/column.rb', line 32 def is_percent? sql_column.cast_type.scale == 4 end |
#is_primary_key? ⇒ Boolean
24 25 26 |
# File 'lib/has_helpers/flattener/column.rb', line 24 def is_primary_key? klass.primary_key == name end |
#is_timestamp? ⇒ Boolean
28 29 30 |
# File 'lib/has_helpers/flattener/column.rb', line 28 def TIMESTAMP_COLUMNS.include? name end |
#update(args) ⇒ Object
16 17 18 19 20 |
# File 'lib/has_helpers/flattener/column.rb', line 16 def update(args) args.each do |k, v| send("#{k}=", v) end end |