Module: Selections::Selectable::ModelMixin

Extended by:
ActiveSupport::Concern
Defined in:
lib/selections/selectable.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

HIDDEN_POSITION =

:nodoc:

999888777

Instance Method Summary collapse

Instance Method Details

#archivedObject

:nodoc:



124
125
126
# File 'lib/selections/selectable.rb', line 124

def archived #:nodoc:
  !!archived_at
end

#archived=(archived_checkbox) ⇒ Object

:nodoc:



128
129
130
131
132
133
134
# File 'lib/selections/selectable.rb', line 128

def archived=(archived_checkbox) #:nodoc:
  if archived_checkbox == "1"
    self.archived_at = Time.now unless archived_at
  else
    self.archived_at = nil
  end
end

#auto_gen_system_codeObject

:nodoc:



107
108
109
110
111
112
113
# File 'lib/selections/selectable.rb', line 107

def auto_gen_system_code #:nodoc:
  unless system_code
    self.system_code= name.to_s.underscore.split(" ").join("_").singularize.underscore.gsub(/\W/, "_")
    self.system_code= parent.system_code + "_" + self.system_code if parent
    self.system_code.gsub!(/\_{2,}/, '_')
  end
end

#check_defaultsObject

:nodoc:



115
116
117
118
# File 'lib/selections/selectable.rb', line 115

def check_defaults #:nodoc:
  siblings_with_default_set.update_attribute(:is_default, false) if self.parent && siblings_with_default_set && self.is_default
  self.is_default = false if archived
end

#disable_system_code_changeObject

:nodoc:



99
100
101
# File 'lib/selections/selectable.rb', line 99

def disable_system_code_change #:nodoc:
  errors.add(:system_code, "cannot be changed") if system_code_changed?
end

#leaf?Boolean

Returns boolean true if current node has no children

Returns:

  • (Boolean)


86
87
88
# File 'lib/selections/selectable.rb', line 86

def leaf?
  children.where(parent_id: self.id).empty?
end

#level_2Object

Returns childrens children from root or any point



91
92
93
# File 'lib/selections/selectable.rb', line 91

def level_2
  Selection.where(parent_id: child_ids)
end

#positionObject

:nodoc:



103
104
105
# File 'lib/selections/selectable.rb', line 103

def position #:nodoc:
  position_value unless position_value == HIDDEN_POSITION
end

#position=(value) ⇒ Object

:nodoc:



95
96
97
# File 'lib/selections/selectable.rb', line 95

def position=(value) #:nodoc:
  self.position_value = value || HIDDEN_POSITION
end

#siblings_with_default_setObject

:nodoc:



120
121
122
# File 'lib/selections/selectable.rb', line 120

def siblings_with_default_set #:nodoc:
  self.parent.children.where(is_default: true).where("id != ?", self.id.to_i).first
end

#sub_childrenObject



136
137
138
# File 'lib/selections/selectable.rb', line 136

def sub_children
  children.flat_map(&:children)
end

#to_sObject

:nodoc:



81
82
83
# File 'lib/selections/selectable.rb', line 81

def to_s #:nodoc:
  name.to_s
end