Module: Prioritize::PriorityAfter::ClassMethods

Defined in:
lib/prioritize.rb

Overview

Методы класса после расширения модулем PriorityAfter перечислены в этом подмодуле.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#priority_columnObject

def priority_column=(value)

@priority_column = value

end



162
163
164
# File 'lib/prioritize.rb', line 162

def priority_column
  @priority_column
end

#priority_nestedObject

def priority_column=(value)

@priority_column = value

end



162
163
164
# File 'lib/prioritize.rb', line 162

def priority_nested
  @priority_nested
end

#priority_parentObject

def priority_column=(value)

@priority_column = value

end



162
163
164
# File 'lib/prioritize.rb', line 162

def priority_parent
  @priority_parent
end

Class Method Details

.extended(base) ⇒ Object



164
165
166
167
168
# File 'lib/prioritize.rb', line 164

def self.extended(base)
  base.class_eval do
    # @priority_column = :priority
  end
end

Instance Method Details

#priority_after(prev_id, moved_id) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/prioritize.rb', line 70

def priority_after(prev_id, moved_id)
  if connection.adapter_name == 'SQLite'
    priority_sqlite(prev_id, moved_id)
  else
    connection.exec_query(
      priority_sql,
      'priority_after',
      [[nil, prev_id], [nil, moved_id]]
    )
  end
end

#priority_sqlObject



82
83
84
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/prioritize.rb', line 82

def priority_sql
  sql = <<-SQL
    UPDATE #{table_name} o
    SET "#{priority_column}" = ordered.rn
    FROM (
      SELECT *, ROW_NUMBER() OVER() AS rn
      FROM (
        -- Выбрать записи от начала и до предыдущего (включая его),
        -- конечно исключая перемещаемую запись.
        (
          SELECT o.*
          FROM #{table_name} o
          LEFT JOIN #{table_name} prev ON prev.id IS NOT DISTINCT FROM $1
          LEFT JOIN #{table_name} moved ON moved.id IS NOT DISTINCT FROM $2
          WHERE
            o."#{priority_column}" <= prev."#{priority_column}"
            AND o.id <> $2
            nested_condition
          ORDER BY o."#{priority_column}" ASC
        )
        UNION ALL
        -- Выбрать перемещаемую запись.
        (
          SELECT o.*
          FROM #{table_name} o
          WHERE o.id = $2
        )
        UNION ALL
        -- Выбрать от предыдущего (не включая его) и до конца, так же
        -- исключая перемещаемую запись.
        (
          SELECT o.*
          FROM #{table_name} o
          LEFT JOIN #{table_name} prev ON prev.id IS NOT DISTINCT FROM $1
          LEFT JOIN #{table_name} moved ON moved.id IS NOT DISTINCT FROM $2
          WHERE
            (o."#{priority_column}" > prev."#{priority_column}" OR prev."#{priority_column}" IS NULL)
            AND o.id <> $2
            nested_condition
          ORDER BY o."#{priority_column}" ASC
        )
      ) numbered
    ) ordered
    WHERE  o.id = ordered.id
  SQL
  nested_condition = priority_nested ?
    %Q(AND o."#{priority_parent}" IS NOT DISTINCT FROM moved."#{priority_parent}") :
    ''
  sql.gsub! 'nested_condition', nested_condition
  sql
end

#priority_sqlite(prev_id, moved_id) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/prioritize.rb', line 134

def priority_sqlite(prev_id, moved_id)
  list = select('id', "#{priority_column}").order("#{priority_column}" => :asc).where.not(id: moved_id).to_a
  moved = select('id', "#{priority_column}").find_by_id(moved_id)
  if prev_id.nil?
    list.unshift(moved)
  else
    list.each_with_index do |model, index|
      if model.id.to_s == prev_id
        list.insert(index + 1, moved)
        break
      end
    end
  end
  transaction do
    list.each_with_index do |model, index|
      model.update("#{priority_column}" => index)
    end
  end
end