Class: PyrRules::RulesController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/pyr_rules/rules_controller.rb

Instance Method Summary collapse

Instance Method Details

#add_actionObject



40
41
42
43
44
45
46
47
48
49
# File 'app/controllers/pyr_rules/rules_controller.rb', line 40

def add_action
  @pyr_rules_rule = PyrRules::Rule.find(params[:id])
  action = params[:action_class].gsub("__","::") rescue nil
  @pyr_rules_rule.actions << PyrRules::Action.new(type: action)
  if @pyr_rules_rule.save
    redirect_to :back, notice: "Added event: #{action}"
  else
    redirect_to :back, error: "Could not add event: #{action}.  Suggest you check the logs and maybe this will help, too: #{@pyr_rules_rule.errors}"
  end
end

#add_action_mappingObject



77
78
79
80
81
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
# File 'app/controllers/pyr_rules/rules_controller.rb', line 77

def add_action_mapping
  PyrRules::Action.add_mapping_type_equivalency(:messaging_user, [:user, :tree_user, :contact])
  PyrRules::Action.add_mapping_type_equivalency(:object, [:user, :messaging_user, :contact, :string])
  @pyr_rules_rule = PyrRules::Rule.find(params[:id])
  action_id = params[:action_id]
  rule_field = params[:rule_field]
  action_field = params[:action_field]
  @action = @pyr_rules_rule.actions.detect{|a| a.id.to_s == action_id}
  @was_mapped = @action.context_mapping[action_field]
  @action_field_name = action_field.split(":").first
  @action_field_type = action_field.split(":").last
  if @action_field_type == :class_lookup
    klazz = rule_field.constantize
    @action.context_mapping[action_field] = rule_field
    @pyr_rules_rule.save 
  else
    @rule_field_name = rule_field.split(":").first
    rule_type = rule_field.split(":").last
    if PyrRules::Action.check_mapping_type?(@action_field_type, rule_type)
      @action.context_mapping[action_field] = rule_field
      @pyr_rules_rule.save 
    else
      @error = "Illegal field type mapping from #{rule_type} to #{@action_field_type}"
    end
  end
  respond_to do |format|
    format.html { redirect_to pyr_rules_rule_path(@pyr_rules_rule) }
    format.js {}
  end
end

#add_eventObject



12
13
14
15
16
17
18
19
20
21
# File 'app/controllers/pyr_rules/rules_controller.rb', line 12

def add_event
  @pyr_rules_rule = PyrRules::Rule.find(params[:id])
  event = params[:event].gsub("__","::") rescue nil
  @pyr_rules_rule.events << event unless @pyr_rules_rule.events.index event
  if @pyr_rules_rule.save
    redirect_to :back, notice: "#{event} is a trigger for this rule"
  else
    redirect_to :back, error: "Could not add event: #{event}.  Suggest you check the logs and maybe this will help, too: #{@pyr_rules_rule.errors}"
  end
end

#createObject

POST /pyr_rules/rules POST /pyr_rules/rules.json



179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'app/controllers/pyr_rules/rules_controller.rb', line 179

def create
  @pyr_rules_rule = PyrRules::Rule.new(params[:pyr_rules_rule])

  respond_to do |format|
    if @pyr_rules_rule.save
      format.html { redirect_to (params[:event_id].presence ? pyr_rules_event_rule_url(@pyr_rules_rule, event_id: params[:event_id]) : pyr_rules_rule_url(@pyr_rules_rule)), notice: 'Rule was successfully created.' }
      format.json { render json: @pyr_rules_rule, status: :created, location: @pyr_rules_rule }
    else
      format.html { render action: "new" }
      format.json { render json: @pyr_rules_rule.errors, status: :unprocessable_entity }
    end
  end
end

#destroyObject

DELETE /pyr_rules/rules/1 DELETE /pyr_rules/rules/1.json



222
223
224
225
226
227
228
229
230
# File 'app/controllers/pyr_rules/rules_controller.rb', line 222

def destroy
  @pyr_rules_rule = PyrRules::Rule.find(params[:id])
  @pyr_rules_rule.destroy

  respond_to do |format|
    format.html { redirect_to (params[:event_id].presence ? pyr_rules_event_rules_url(event_id: params[:event_id]) : pyr_rules_rules_url) }
    format.json { head :no_content }
  end
end

#editObject

GET /pyr_rules/rules/1/edit



173
174
175
# File 'app/controllers/pyr_rules/rules_controller.rb', line 173

def edit
  @pyr_rules_rule = PyrRules::Rule.find(params[:id])
end

#indexObject

GET /pyr_rules/rules GET /pyr_rules/rules.json



140
141
142
143
144
145
146
147
# File 'app/controllers/pyr_rules/rules_controller.rb', line 140

def index
  @pyr_rules_rules = params[:event_id].presence ? PyrRules::Rule.where(event: params[:event_id]).all  : PyrRules::Rule.all

  respond_to do |format|
    format.html # index.html.erb
    format.json { render json: @pyr_rules_rules }
  end
end

#lookup_actionsObject



34
35
36
37
38
# File 'app/controllers/pyr_rules/rules_controller.rb', line 34

def lookup_actions
  r = Regexp.new(".*#{params[:q]}.*", "i")
  list = PyrRules::ActionHandler.action_listing.map(&:to_s).grep r
  render json: list.first(10).map{|a| {id: a.gsub("::","__") ,value: a}}
end

#lookup_eventsObject

before_filter :admin_required # If you include Rules, you want this controller SECURE!!!



6
7
8
9
10
# File 'app/controllers/pyr_rules/rules_controller.rb', line 6

def lookup_events
  r = Regexp.new(".*#{params[:q]}.*", "i")
  list = PyrRules::RulesConfig.events.grep r
  render json: list.first(10).map{|e| {id: e.gsub("::","__") ,value: e}}
end

#lookup_sub_propertiesObject



64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/controllers/pyr_rules/rules_controller.rb', line 64

def lookup_sub_properties
  @rule = PyrRules::Rule.find(params[:rule_id])
  @div_id = request[:div_id]
  @path = params[:path]
  @ctx = @rule.lookup_path(@path)
  respond_to do |format|
    format.js {}
    format.json { render json: @ctx }
  end

  
end

#newObject

GET /pyr_rules/rules/new GET /pyr_rules/rules/new.json



162
163
164
165
166
167
168
169
170
# File 'app/controllers/pyr_rules/rules_controller.rb', line 162

def new
  @pyr_rules_rule = PyrRules::Rule.new
  @pyr_rules_rule.event = params[:event_id] if params[:event_id]

  respond_to do |format|
    format.html # new.html.erb
    format.json { render json: @pyr_rules_rule }
  end
end

#reload_rules_engineObject



132
133
134
135
136
# File 'app/controllers/pyr_rules/rules_controller.rb', line 132

def reload_rules_engine
  c = PyrRules::RulesEngine.reload_configuration
  notice = "Loaded #{c[:events].try(:size) || '0'} Events and #{c[:actions].try(:size) || '0'} Actions "
  redirect_to :back, notice: notice
end

#remove_actionObject



51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/controllers/pyr_rules/rules_controller.rb', line 51

def remove_action
  @pyr_rules_rule = PyrRules::Rule.find(params[:id])
  action_id = params[:action_id] rescue nil
  action = @pyr_rules_rule.actions.detect{|a| a.id.to_s == action_id}
  action.delete if action
  if @pyr_rules_rule.save
    redirect_to :back, notice: "Action deleted"
  else
    redirect_to :back, error: "Could not remove action.  Suggest you check the logs and maybe this will help, too: #{@pyr_rules_rule.errors}"
  end

end

#remove_action_mappingObject



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'app/controllers/pyr_rules/rules_controller.rb', line 109

def remove_action_mapping
  @pyr_rules_rule = PyrRules::Rule.find(params[:id])
  action_id = params[:action_id]
  action_field = params[:action_field]
  @action = @pyr_rules_rule.actions.detect{|a| a.id.to_s == action_id}
  @action_field_name = action_field.split(":").first
  @action_field_type = action_field.split(":").last
  mapped_from = @action.context_mapping[action_field] 
  if mapped_from
    @action.context_mapping[action_field] = nil
    @rule_field_name = mapped_from.split(":").first
    @rule_field_type = mapped_from.split(":").last
    @message = "Removed field mapping for #{@action_field_name}"
  else
    @message = "#{action_field} was not mapped"
  end
  @pyr_rules_rule.save
  respond_to do |format|
    format.html { redirect_to pyr_rules_rule_path(@pyr_rules_rule) }
    format.js {} 
  end
end

#remove_eventObject



23
24
25
26
27
28
29
30
31
32
# File 'app/controllers/pyr_rules/rules_controller.rb', line 23

def remove_event
  @pyr_rules_rule = PyrRules::Rule.find(params[:id])
  event = params[:event].gsub("__","::") rescue nil
  @pyr_rules_rule.events.delete event
  if @pyr_rules_rule.save
    redirect_to :back, notice: "#{event} is no longer a trigger for this rule"
  else
    redirect_to :back, error: "Could not remove event: #{event}.  Suggest you check the logs and maybe this will help, too: #{@pyr_rules_rule.errors}"
  end
end

#showObject

GET /pyr_rules/rules/1 GET /pyr_rules/rules/1.json



151
152
153
154
155
156
157
158
# File 'app/controllers/pyr_rules/rules_controller.rb', line 151

def show
  @pyr_rules_rule = PyrRules::Rule.find(params[:id])

  respond_to do |format|
    format.html # show.html.erb
    format.json { render json: @pyr_rules_rule }
  end
end

#updateObject

PUT /pyr_rules/rules/1 PUT /pyr_rules/rules/1.json



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'app/controllers/pyr_rules/rules_controller.rb', line 195

def update
  @pyr_rules_rule = PyrRules::Rule.find(params[:id])
  # {"action"=>{"522e0313a4c6b2154c000001"=>{"template"=>{"name"=>"asdf"}}}}
  action_update = params[:pyr_rules_rule].delete(:action) rescue nil
  if action_update 
    action_id = action_update.keys.first
    action = @pyr_rules_rule.actions.detect{|a| a.id.to_s == action_id}
    puts "Updating Rule #{@pyr_rules_rule.name}- Action #{action.class}"
    template_name = action_update.values.first[:template].keys.first
    template_value = action_update.values.first[:template].values.first
    puts "TemplateName[#{template_name}]  TemplateValue[#{template_value}]"
    action.template[template_name] = template_value
    @pyr_rules_rule.save!
  end
  respond_to do |format|
    if @pyr_rules_rule.update_attributes(params[:pyr_rules_rule])
      format.html { redirect_to (params[:event_id].presence ? pyr_rules_event_rule_url(@pyr_rules_rule, event_id: params[:event_id]) : pyr_rules_rule_url(@pyr_rules_rule)), notice: 'Rule was successfully updated.' }
      format.json { head :no_content }
    else
      format.html { render action: "edit" }
      format.json { render json: @pyr_rules_rule.errors, status: :unprocessable_entity }
    end
  end
end