Class: ErpApp::Desktop::ConfigurationManagement::TypesController

Inherits:
BaseController show all
Defined in:
app/controllers/erp_app/desktop/configuration_management/types_controller.rb

Instance Method Summary collapse

Instance Method Details

#add_optionObject



100
101
102
103
104
105
106
107
108
109
110
# File 'app/controllers/erp_app/desktop/configuration_management/types_controller.rb', line 100

def add_option
  option = ConfigurationOption.find(params[:option_id])
  type   = ConfigurationItemType.find(params[:model_id])

  if type.options.where('configuration_options.id = ?', option.id).first.nil?
    type.add_option(option)
    render :json => (type.save ? {:success => true}: {:success => false})
  else
    render :json => {:success => false, :message => 'Option already exits for Configuration Type'}
  end
end

#categoriesObject



6
7
8
9
10
11
# File 'app/controllers/erp_app/desktop/configuration_management/types_controller.rb', line 6

def categories
  render :json => {:success => true,
    :categories => Category.all.collect{|category|
    category.to_hash(:methods => [:description], :id => :category_id)}
  }
end

#create_or_updateObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'app/controllers/erp_app/desktop/configuration_management/types_controller.rb', line 80

def create_or_update
  type = params[:model_id].blank? ? ConfigurationItemType.new : ConfigurationItemType.find(params[:model_id])
  type.description = params[:description]
  type.internal_identifier = params[:internal_identifier]
  type.allow_user_defined_options = (params[:user_defined_options] == 'yes')
  type.is_multi_optional = (params[:multi_optional] == 'yes')
  if type.category.nil?
    CategoryClassification.create(:classification => type, :category => Category.find(params[:category_id]))
  else
    type.category_classification.category = Category.find(params[:category_id])
    type.category_classification.save
  end

  render :json => (type.save ? {:success => true} : {:success => false})
end

#destroyObject



96
97
98
# File 'app/controllers/erp_app/desktop/configuration_management/types_controller.rb', line 96

def destroy
   render :json => (ConfigurationItemType.destroy(params[:id]) ? {:success => true}: {:success => false})
end

#indexObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'app/controllers/erp_app/desktop/configuration_management/types_controller.rb', line 20

def index
  item_types = [].tap do |item_types_array|
    ConfigurationItemType.all.group_by(&:category).each do |category, categorized_item_types|
      category_hash = {:text => (category.nil? ? 'uncategorized' : category.description), :iconCls => 'icon-documents', :leaf => false, :children => []}
      categorized_item_types.each do |item_type|
        categorized_config_item_type_hash = {
          :type => 'ConfigurationItemType',
          :category_id => (category.nil? ? nil : category.id),
          :text => item_type.description,
          :description => item_type.description,
          :internal_identifier => item_type.internal_identifier,
          :user_defined_options => item_type.allow_user_defined_options ? 'yes' : 'no',
          :multi_optional => item_type.is_multi_optional ? 'yes' : 'no',
          :model_id => item_type.id,
          :iconCls => 'icon-document_info',
          :leaf => false,
          :children => []
        }
        if item_type.allow_user_defined_options
          categorized_config_item_type_hash[:children] << {
            :text => "[User Defined]",
            :iconCls => 'icon-user',
            :leaf => true,
            :children => []
          }
        else
          #this is not dry clean up later
          item_type.options.default.each do |option|
            categorized_config_item_type_hash[:children] << {
              :text => option.description,
              :iconCls => 'icon-document_check',
              :type => 'ConfigurationOption',
              :model_id => option.id,
              :type_id => item_type.id,
              :leaf => true,
              :children => []
            }
          end

          item_type.options.non_default.each do |option|
            categorized_config_item_type_hash[:children] << {
              :text => option.description,
              :iconCls => 'icon-document',
              :type => 'ConfigurationOption',
              :model_id => option.id,
              :type_id => item_type.id,
              :leaf => true,
              :children => []
            }
          end
        end
        category_hash[:children] << categorized_config_item_type_hash
      end#categorized_item_types
      item_types_array << category_hash
    end#ConfigurationItemType.all
  end#tap

  render :json => item_types
end

#remove_optionObject



112
113
114
115
116
117
# File 'app/controllers/erp_app/desktop/configuration_management/types_controller.rb', line 112

def remove_option
  option = ConfigurationOption.find(params[:option_id])
  type   = ConfigurationItemType.find(params[:type_id])

  render :json => (type.remove_option(option) ? {:success => true}: {:success => false})
end

#set_option_as_defaultObject



119
120
121
122
123
124
# File 'app/controllers/erp_app/desktop/configuration_management/types_controller.rb', line 119

def set_option_as_default
  option = ConfigurationOption.find(params[:option_id])
  type = ConfigurationItemType.find(params[:type_id])

  render :json => (type.add_default_option(option) ? {:success => true}: {:success => false})
end

#types_storeObject



13
14
15
16
17
18
# File 'app/controllers/erp_app/desktop/configuration_management/types_controller.rb', line 13

def types_store
  render :json => {:success => true,
    :types => ConfigurationItemType.joins(:category_classification)
              .where(:category_classifications => {:category_id => params[:category_id]}).all.collect{|type| type.to_hash(:only => [:id, :internal_identifier, :description])}
  }
end