Skip to content
Snippets Groups Projects
Commit c3d02779 authored by Tyler Lemburg's avatar Tyler Lemburg
Browse files

Change to resource field methods

parent 9436e114
No related branches found
No related tags found
No related merge requests found
require 'active_record'
require 'models/resource'
require 'models/resource_class'
require 'models/resource_field'
require 'models/resource_field_data'
class AddResourceFields < ActiveRecord::Migration
def up
# add a table to hold the various "classes" of resources
create_table :resource_classes do |t|
t.string :name
end
# add a table to hold the various types of fields that get attached to resources
create_table :resource_fields do |t|
t.string :field_name
t.integer :resource_class_id
end
# add a table to contain the actual data
create_table :resource_field_data do |t|
t.integer :resource_id
t.integer :resource_field_id
t.string :data
end
# add a column to the resource table to reference its class if it exists
add_column :resources, :resource_class_id, :integer
# we seem to have a random column with no use
remove_column :resources, :resource_type
# add a column to reservations for just extraneous details
add_column :reservations, :details, :text
# move all model data, for NIS resources, into this new arch.
# create a class
ResourceClass.reset_column_information
ResourceField.reset_column_information
ResourceFieldData.reset_column_information
tool_class = ResourceClass.create(
:name => 'Innovation Studio Tool'
)
# add model as a field to this class
model_field = tool_class.resource_fields.create(
:field_name => 'Model'
)
# for each of those resources, add a field_data with its model
Resource.where(:service_space_id => 1).all.each do |resource|
ResourceFieldData.create(
resource_id: resource.id,
resource_field_id: model_field.id,
data: resource.model
)
resource.resource_class_id = tool_class.id
resource.save
end
# lastly, drop the column
remove_column :resources, :model
end
end
\ No newline at end of file
require 'active_record'
require 'models/resource_approver'
require 'models/resource_authorization'
require 'models/resource_class'
require 'models/resource_field'
require 'models/resource_field_data'
class Resource < ActiveRecord::Base
has_many :reservations, dependent: :destroy
has_many :resource_approvers, dependent: :destroy
has_many :resource_authorizations, dependent: :destroy
belongs_to :resource_class
has_many :resource_field_datas, dependent: :destroy
alias_method :approvers, :resource_approvers
def method_missing(meth_sym)
# check if this symbol can be associated with a field for this class
if !self.resource_class.nil? && (field = ResourceField.find_by(:resource_class_id => self.resource_class.id, :field_name => meth_sym.to_s.downcase))
# use this field id to retrieve the data from the resoruce field data table
data = ResourceFieldData.find_by(:resource_id => self.id, :resource_field_id => field.id)
unless data.nil?
return data.data
end
end
super
end
def get_field_data
{}.tap do |hash|
self.resource_field_datas.each do |data|
hash[data.resource_field.field_name.downcase] = data.data
end
end
end
end
\ No newline at end of file
require 'active_record'
class ResourceClass < ActiveRecord::Base
has_many :resource_fields
end
\ No newline at end of file
require 'active_record'
class ResourceField < ActiveRecord::Base
belongs_to :resource_class
end
\ No newline at end of file
require 'active_record'
require 'models/resource'
require 'models/resource_field'
class ResourceFieldData < ActiveRecord::Base
belongs_to :resource
belongs_to :resource_field
end
\ No newline at end of file
This diff is collapsed.
......@@ -27,7 +27,6 @@ end
post '/:service_space_url_name/admin/resources/create/?' do
resource = Resource.new
resource.name = params[:name]
resource.model = params[:model]
resource.description = params[:description]
resource.service_space_id = @space.id
resource.needs_authorization = true
......@@ -70,7 +69,6 @@ post '/:service_space_url_name/admin/resources/:resource_id/edit/?' do
end
resource.name = params[:name]
resource.model = params[:model]
resource.description = params[:description]
resource.is_reservable = params.checked?('is_reservable')
resource.time_slot_type = params[:time_slot_type]
......
......@@ -222,7 +222,7 @@ label.day-header {
position: relative;
z-index: 1;
background-color: #d8d8d8;
padding: .5em calc(~"1em + 30px") 0 5em;
padding: .5em 30px 0 5em;
}
.calendar {
......
......@@ -63,7 +63,7 @@
<label for="resource">Resource</label>
<select id="resource" name="resource" class="use-select2" style="width: 100%;">
<% resources.each do |resource| %>
<option <%= 'selected="selected"' if !event.reservation.nil? && !event.reservation.resource.nil? && event.reservation.resource.id == resource.id %> value="<%= resource.id %>"><%= "#{resource.name} - #{resource.model}" %></option>
<option <%= 'selected="selected"' if !event.reservation.nil? && !event.reservation.resource.nil? && event.reservation.resource.id == resource.id %> value="<%= resource.id %>"><%= "#{resource.name}" %></option>
<% end %>
</select>
</div>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment