Module: Attributable::ClassMethods

Defined in:
app/models/attributable.rb

Overview

Class methods for attribute configuration

Instance Method Summary collapse

Instance Method Details

#association(name, instance_method, options = {}) ⇒ Object

Defines an association with the name.



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'app/models/attributable.rb', line 103

 do
  # NOTE: The following attribute is not required for Microarray Genotyping.
  # I think this might be broken and suggests that there should be separate classes for project: one for
  # next-gen sequencing that includes this attribute in it's metadata, and one for microarray genotyping
  # that doesn't.
  include ProjectManager::Associations
  include BudgetDivision::Associations

  custom_attribute(:project_cost_code, required: true)
  custom_attribute(:funding_comments)
  custom_attribute(:collaborators)
  custom_attribute(:external_funding_source)
  custom_attribute(:sequencing_budget_cost_centre)
  custom_attribute(:project_funding_model, in: PROJECT_FUNDING_MODELS)
  custom_attribute(:gt_committee_tracking_id)

  before_validation do |record|
    record.project_cost_code = nil if record.project_cost_code.blank?
    record.project_funding_model = nil if record.project_funding_model.blank?
  end
end

#attribute_details_for(attribute_name) ⇒ Attributable::Attribute

Looks up the Attribute in a attribute_details Array

Parameters:

  • attribute_name (String)

    The name of the attribute to lookup

Returns:



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'app/models/attributable.rb', line 127

 do
  # NOTE: The following attribute is not required for Microarray Genotyping.
  # I think this might be broken and suggests that there should be separate classes for project: one for
  # next-gen sequencing that includes this attribute in it's metadata, and one for microarray genotyping
  # that doesn't.
  include ProjectManager::Associations
  include BudgetDivision::Associations

  custom_attribute(:project_cost_code, required: true)
  custom_attribute(:funding_comments)
  custom_attribute(:collaborators)
  custom_attribute(:external_funding_source)
  custom_attribute(:sequencing_budget_cost_centre)
  custom_attribute(:project_funding_model, in: PROJECT_FUNDING_MODELS)
  custom_attribute(:gt_committee_tracking_id)

  before_validation do |record|
    record.project_cost_code = nil if record.project_cost_code.blank?
    record.project_funding_model = nil if record.project_funding_model.blank?
  end
end

#attribute_namesArray<String>

Returns An array of all attribute names.

Returns:

  • (Array<String>)

    An array of all attribute names



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'app/models/attributable.rb', line 118

 do
  # NOTE: The following attribute is not required for Microarray Genotyping.
  # I think this might be broken and suggests that there should be separate classes for project: one for
  # next-gen sequencing that includes this attribute in it's metadata, and one for microarray genotyping
  # that doesn't.
  include ProjectManager::Associations
  include BudgetDivision::Associations

  custom_attribute(:project_cost_code, required: true)
  custom_attribute(:funding_comments)
  custom_attribute(:collaborators)
  custom_attribute(:external_funding_source)
  custom_attribute(:sequencing_budget_cost_centre)
  custom_attribute(:project_funding_model, in: PROJECT_FUNDING_MODELS)
  custom_attribute(:gt_committee_tracking_id)

  before_validation do |record|
    record.project_cost_code = nil if record.project_cost_code.blank?
    record.project_funding_model = nil if record.project_funding_model.blank?
  end
end

#custom_attribute(name, options = {}, override_previous = false) ⇒ void

Note:

Heavy on meta-programming here. There behaviour could also be tidied up and simplified significantly.

This method returns an undefined value.

Define a custom attribute with the provided name. Will automatically generate:

- Validations
- Form helpers
- Accessioning tie-ins
- Convert blank attributes to nil

Parameters:

  • name (Symbol, String)

    The name of the attribute to generate

  • override_previous (defaults to: false)

    false [type] Override any attributes of the same name on parent classes. (UNUSED)

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :default (Object)

    A default value for the option. (Not a proc/lambda)

  • :required (Boolean) — default: false

    Whether the option is required or not

  • :validator (Boolean) — default: false

    Set to true to defer validation to the #validator_for method

  • :integer (Boolean) — default: false

    The attribute should be an integer

  • :positive_float (Boolean) — default: false

    The attribute should be a float, greater than 0

  • :boolean (Boolean) — default: false

    The attribute should be true or false. WARNING! Currently just tests presence of the key, not actual value. Thus false=true.

  • :in (Array) — default: nil

    The attribute is a selection that must be included in the array.

  • :selection (Boolean) — default: false

    The attribute is a selection generated dynamically from #validator_for

  • :minimum (Numeric) — default: 0

    The minimum value for an integer. WARNING! Inconsistently implemented for floats

  • :with (Regexp) — default: nil

    Regexp for validating the attribute

  • :if (Symbol) — default: nil

    Passed through to the rails validator and will also switch persistence of the attribute based on the condition.

  • :on (Symbol) — default: nil

    Passed through to the rails validator. (eg. on: :create)

  • :save_blank (Boolean) — default: false

    set to true to disabling setting blank attributes to nil. (UNUSED)



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'app/models/attributable.rb', line 90

 do
  # NOTE: The following attribute is not required for Microarray Genotyping.
  # I think this might be broken and suggests that there should be separate classes for project: one for
  # next-gen sequencing that includes this attribute in it's metadata, and one for microarray genotyping
  # that doesn't.
  include ProjectManager::Associations
  include BudgetDivision::Associations

  custom_attribute(:project_cost_code, required: true)
  custom_attribute(:funding_comments)
  custom_attribute(:collaborators)
  custom_attribute(:external_funding_source)
  custom_attribute(:sequencing_budget_cost_centre)
  custom_attribute(:project_funding_model, in: PROJECT_FUNDING_MODELS)
  custom_attribute(:gt_committee_tracking_id)

  before_validation do |record|
    record.project_cost_code = nil if record.project_cost_code.blank?
    record.project_funding_model = nil if record.project_funding_model.blank?
  end
end

#defaultsHash<String,Object>

Returns a hash of default attribute values

Returns:

  • (Hash<String,Object>)

    Hash of each attribute and its default



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'app/models/attributable.rb', line 112

 do
  # NOTE: The following attribute is not required for Microarray Genotyping.
  # I think this might be broken and suggests that there should be separate classes for project: one for
  # next-gen sequencing that includes this attribute in it's metadata, and one for microarray genotyping
  # that doesn't.
  include ProjectManager::Associations
  include BudgetDivision::Associations

  custom_attribute(:project_cost_code, required: true)
  custom_attribute(:funding_comments)
  custom_attribute(:collaborators)
  custom_attribute(:external_funding_source)
  custom_attribute(:sequencing_budget_cost_centre)
  custom_attribute(:project_funding_model, in: PROJECT_FUNDING_MODELS)
  custom_attribute(:gt_committee_tracking_id)

  before_validation do |record|
    record.project_cost_code = nil if record.project_cost_code.blank?
    record.project_funding_model = nil if record.project_funding_model.blank?
  end
end