Class: Forms::FieldLocks

Inherits:
Object
  • Object
show all
Defined in:
lib/forms/field_locks.rb

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (FieldLocks) initialize(params = {})

@param a instance of some ActiveRecord model



10
11
12
13
# File 'lib/forms/field_locks.rb', line 10

def initialize(params = {})
  @locks = params
  @locks ||= {}
end

Instance Attribute Details

- (Object) locks

object_name: [method_name, method_name1]



7
8
9
# File 'lib/forms/field_locks.rb', line 7

def locks
  @locks
end

Instance Method Details

- (Boolean) has_locks?

Returns:

  • (Boolean)


19
20
21
# File 'lib/forms/field_locks.rb', line 19

def has_locks?
  locks.keys.any?
end

- (Object) lock(object_name, method)



36
37
38
39
# File 'lib/forms/field_locks.rb', line 36

def lock(object_name, method)
  locks[object_name] ||= {}
  locks[object_name].merge!(method => 1)
end

- (Boolean) locked?(object_name, method)

Returns:

  • (Boolean)


15
16
17
# File 'lib/forms/field_locks.rb', line 15

def locked?(object_name, method)
  (locks[object_name.to_s] && locks[object_name.to_s][method.to_s].to_s == '1') ? true : false
end

- (Object) resolve(object_name, method, value)



27
28
29
30
31
32
33
34
# File 'lib/forms/field_locks.rb', line 27

def resolve(object_name, method, value)
  return nil if value.nil?
  if locked?(object_name.to_s, method.to_s)
    value
  else
    nil
  end
end

- (Object) to_params



23
24
25
# File 'lib/forms/field_locks.rb', line 23

def to_params
  locks
end

- (Object) unlock(object_name, method)



41
42
43
# File 'lib/forms/field_locks.rb', line 41

def unlock(object_name, method)
  locks[object_name].delete(method)
end