Class: Auth::Shopping::BarCode

Inherits:
Object
  • Object
show all
Includes:
Mongoid::Document
Defined in:
app/models/auth/shopping/bar_code.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#assigned_to_objectObject

the primary link of the object to which to redirect. this is se tbefore show.



17
18
19
# File 'app/models/auth/shopping/bar_code.rb', line 17

def assigned_to_object
  @assigned_to_object
end

#force_showObject

this is used in the show_action to force render the show view, in case we just want to view the barcode record and not redirect to the



12
13
14
# File 'app/models/auth/shopping/bar_code.rb', line 12

def force_show
  @force_show
end

#go_to_next_stepObject

Returns the value of attribute go_to_next_step.



13
14
15
# File 'app/models/auth/shopping/bar_code.rb', line 13

def go_to_next_step
  @go_to_next_step
end

Class Method Details

.allow_paramsObject

these should be added to any controller that is going to be implementing the bar_code_concern. see auth/shopping/product_controller_concern, for an example of how the permitted params are



33
34
35
# File 'app/models/auth/shopping/bar_code.rb', line 33

def self.allow_params
	[:bar_code_tag,:remove_bar_code]
end

.clear_object(assigned_to_object_id) ⇒ Object

tries to clear the assigned object id from the barcode record. if it returns null, then it will first check if any record exists, to which that object_id was assigned, if no record exists, returns true(because it means that this object id was already cleared from some barcode before.), on the other hand if some record is found, then returns false.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/models/auth/shopping/bar_code.rb', line 39

def self.clear_object(assigned_to_object_id)
	## we just do a find one and update
	begin
		returned_document = where(:assigned_to_object_id => assigned_to_object_id).find_one_and_update(
			{
				"$set" => {
					"assigned_to_object_id" => nil,
					"assigned_to_object_class" => nil
				}
			},
			{
				:return_document => :after
			}
		)
		true
	rescue
		doc_exists = where(:assigned_to_object_id => assigned_to_object_id).first
		return true unless doc_exists
		return false
	end
end

.find(*args) ⇒ Object

Raises:

  • (Mongoid::Errors::DocumentNotFound)


25
26
27
28
29
# File 'app/models/auth/shopping/bar_code.rb', line 25

def self.find(*args)
	@bar_codes = Auth::Shopping::BarCode.where(:bar_code_tag => args[0])
	raise Mongoid::Errors::DocumentNotFound.new(Auth::Shopping::BarCode,[args[0]]) if (@bar_codes.nil? || (@bar_codes.size != 1))
	@bar_codes.first
end

.transfer_bar_code(from_object, to_object) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'app/models/auth/shopping/bar_code.rb', line 63

def self.transfer_bar_code(from_object,to_object)

	returned_document = Auth::Shopping::BarCode.collection.find_one_and_update(
			{	
				"$and" => 
				[
					{
						"assigned_to_object_id" => from_object.id.to_s
					},
					{
						"assigned_to_object_class" => from_object.class.name.to_s
					}
				]
			},
			{
				"$set" => {
					
					"assigned_to_object_id" => to_object.id.to_s,
					"assigned_to_object_class" => to_object.class.name.to_s
				}
			},
			{
				:return_document => :after
			})


	returned_document


end

.upsert_and_assign_object(object) ⇒ Object

there should not already exist a bar code with this bar code or one where this object id is already assigned.



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
133
134
135
136
137
138
139
140
141
142
143
# File 'app/models/auth/shopping/bar_code.rb', line 96

def self.upsert_and_assign_object(object)
	returned_document = Auth::Shopping::BarCode.collection.find_one_and_update(
			{
				"$or" => 
				[
					{
						"bar_code_tag" => object.bar_code_tag
					},
					{
						"$and" => 
						[
							{
								"assigned_to_object_id" => object.id.to_s
							},
							{
								"assigned_to_object_class" => object.class.name.to_s
							}
						]
					}
				]
			},
			{
				"$setOnInsert" => {
					"bar_code_tag" => object.bar_code_tag,
					"assigned_to_object_id" => object.id.to_s,
					"assigned_to_object_class" => object.class.name.to_s
				}
			},
			{
				:return_document => :after,
				:upsert => true
			})


	puts "returned document is:"
	puts returned_document.to_s

	return nil if returned_document.nil?

	returned_document = Mongoid::Factory.from_db(Auth::Shopping::BarCode,returned_document)

	return nil if returned_document.assigned_to_object_id.to_s != object.id.to_s

	return nil if returned_document.bar_code_tag != object.bar_code_tag

	return returned_document

end

Instance Method Details

#resource_idObject

let me continue with the navigation,



6
# File 'app/models/auth/shopping/bar_code.rb', line 6

field :resource_id, type: String

#set_assigned_objectObject

so query options can be added as checkboxes. i can have some by default, others not.



147
148
149
150
151
152
153
# File 'app/models/auth/shopping/bar_code.rb', line 147

def set_assigned_object
	begin
   		self.assigned_to_object = self.assigned_to_object_class.constantize.find(self.assigned_to_object_id)
   	rescue Mongoid::Errors::DocumentNotFound
   		self.errors.add(:_id, "could not find the assigned object")
   	end
end

#to_paramObject

this changes what is being used as id.



21
22
23
# File 'app/models/auth/shopping/bar_code.rb', line 21

def to_param
	bar_code_tag
end