Class: Stratagem::ApplicationExtensions::Models::Adapters::ActiveRecord::Metadata
- Inherits:
-
Object
- Object
- Stratagem::ApplicationExtensions::Models::Adapters::ActiveRecord::Metadata
- Defined in:
- lib/stratagem/framework_extensions/models/adapters/active_record/metadata.rb
Overview
prefix method names with to avoid collision
Instance Attribute Summary collapse
-
#instance ⇒ Object
readonly
Returns the value of attribute instance.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
Instance Method Summary collapse
- #attribute_names ⇒ Object
- #attribute_type(name) ⇒ Object
- #blacklists_attributes? ⇒ Boolean
-
#column_from_error(database_error) ⇒ Object
parses a database error and returns the columns that had problems this is typically a not null enforced by the database but not by the model.
-
#initialize(model) ⇒ Metadata
constructor
A new instance of Metadata.
-
#internal_attributes ⇒ Object
Attributes generally used by the persistence mechanism that should not be human writable accessible from the class.
- #invalid_columns(instance) ⇒ Object
-
#relations(relation_type = nil) ⇒ Object
:belongs_to, :has_many.
- #unaccessible_attributes ⇒ Object
- #valid? ⇒ Boolean
- #whitelists_attributes? ⇒ Boolean
Constructor Details
#initialize(model) ⇒ Metadata
Returns a new instance of Metadata.
8 9 10 11 |
# File 'lib/stratagem/framework_extensions/models/adapters/active_record/metadata.rb', line 8 def initialize(model) @model = model @instance = @model.new unless (@model == ActiveRecord::Base) end |
Instance Attribute Details
#instance ⇒ Object (readonly)
Returns the value of attribute instance.
6 7 8 |
# File 'lib/stratagem/framework_extensions/models/adapters/active_record/metadata.rb', line 6 def instance @instance end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
6 7 8 |
# File 'lib/stratagem/framework_extensions/models/adapters/active_record/metadata.rb', line 6 def model @model end |
Instance Method Details
#attribute_names ⇒ Object
59 60 61 |
# File 'lib/stratagem/framework_extensions/models/adapters/active_record/metadata.rb', line 59 def attribute_names instance.attribute_names.map {|a| a.to_sym} - model.stratagem.ignore_attributes end |
#attribute_type(name) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/stratagem/framework_extensions/models/adapters/active_record/metadata.rb', line 76 def attribute_type(name) column = instance.column_for_attribute(name.to_s) if (!column.nil?) if (model.stratagem.foreign_keys.include?(name.to_sym)) :integer else column.type end else if (name.to_s =~ /password/) :string else types = [:string, :boolean, :integer] types[rand(3)] end end end |
#blacklists_attributes? ⇒ Boolean
55 56 57 |
# File 'lib/stratagem/framework_extensions/models/adapters/active_record/metadata.rb', line 55 def blacklists_attributes? !model.protected_attributes.nil? end |
#column_from_error(database_error) ⇒ Object
parses a database error and returns the columns that had problems this is typically a not null enforced by the database but not by the model
36 37 38 39 40 41 42 43 44 |
# File 'lib/stratagem/framework_extensions/models/adapters/active_record/metadata.rb', line 36 def column_from_error(database_error) if (database_error.kind_of?(Mysql::Error) || database_error.kind_of?(::ActiveRecord::StatementInvalid)) database_error. =~ /Column '(.*)?' cannot/ $1 ? $1.to_sym : nil else puts database_error.class.name nil end end |
#internal_attributes ⇒ Object
Attributes generally used by the persistence mechanism that should not be human writable accessible from the class
65 66 67 68 69 70 71 72 73 74 |
# File 'lib/stratagem/framework_extensions/models/adapters/active_record/metadata.rb', line 65 def internal_attributes attrs = [:id, :created_at, :updated_at] attrs += attribute_names.select {|a| (a.to_s =~ /_count$/) || (a.to_s =~ /_salt$/) || (a.to_s =~ /_token$/) || (a.to_s == 'type') }.map {|a| a.to_sym } attrs end |
#invalid_columns(instance) ⇒ Object
94 95 96 97 98 99 |
# File 'lib/stratagem/framework_extensions/models/adapters/active_record/metadata.rb', line 94 def invalid_columns(instance) instance.valid? errors = [] instance.errors.each {|error,i| errors << error.to_s.to_sym } errors & attribute_names end |
#relations(relation_type = nil) ⇒ Object
:belongs_to, :has_many
13 14 15 16 17 18 19 20 21 22 |
# File 'lib/stratagem/framework_extensions/models/adapters/active_record/metadata.rb', line 13 def relations(relation_type=nil) # :belongs_to, :has_many @relations ||= {} @relations[relation_type || :all] ||= model.reflect_on_all_associations(relation_type).map {|a| begin Stratagem::ApplicationExtensions::Models::Metadata::StratagemAssociation.new(a.name.to_sym, a.association_foreign_key.to_sym, a.klass, a.macro, a.) rescue puts "ERROR: #{$!.}" end }.compact end |
#unaccessible_attributes ⇒ Object
24 25 26 27 28 29 30 31 |
# File 'lib/stratagem/framework_extensions/models/adapters/active_record/metadata.rb', line 24 def unaccessible_attributes attrs = [] if (model.accessible_attributes) attrs = model.stratagem.attribute_names - model.accessible_attributes.map {|a| a.to_sym } end attrs += model.protected_attributes.map {|a| a.to_sym } if model.protected_attributes attrs end |
#valid? ⇒ Boolean
47 48 49 |
# File 'lib/stratagem/framework_extensions/models/adapters/active_record/metadata.rb', line 47 def valid? model.valid? end |
#whitelists_attributes? ⇒ Boolean
51 52 53 |
# File 'lib/stratagem/framework_extensions/models/adapters/active_record/metadata.rb', line 51 def whitelists_attributes? !model.accessible_attributes.nil? end |