Class: DynamicFieldsets::FieldsetsController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/dynamic_fieldsets/fieldsets_controller.rb

Overview

Basic controller for managing fieldsets

Instance Method Summary collapse

Instance Method Details

#associate_childObject

associates a field or fieldset with the given fieldset by creating a fieldset child



61
62
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
# File 'app/controllers/dynamic_fieldsets/fieldsets_controller.rb', line 61

def associate_child
  @fieldset = DynamicFieldsets::Fieldset.find(params[:id])
  @field = DynamicFieldsets::Field.find(params[:field])

  @fieldset_child = DynamicFieldsets::FieldsetChild.where(:child_id => @field.id, :child_type => @field.class.name, :fieldset_id => @fieldset.id).first
  if(@fieldset_child.nil?)
    @fieldset_child = DynamicFieldsets::FieldsetChild.new(
      :child_id => @field.id, 
      :child_type => "DynamicFieldsets::Field",
      :fieldset_id => @fieldset.id,
      :order_num => DynamicFieldsets::FieldsetChild.where(:fieldset_id => @fieldset.id).count + 1)
  else
    child_already_exists = true
  end

  respond_to do |format|
    if @fieldset_child.save
      if child_already_exists
        notice_text = "Field was already associated with the fieldset"
      else
        notice_text = "Successfully associated a field"
      end
      format.html { redirect_to(dynamic_fieldsets_children_dynamic_fieldsets_fieldset_path(@fieldset), :notice => notice_text )}
    else
      format.html { redirect_to(dynamic_fieldsets_children_dynamic_fieldsets_fieldset_path(@fieldset), :notice => @fieldset_child.errors) }
    end
  end
end

#childrenObject

Show a record and all children



52
53
54
55
56
57
58
# File 'app/controllers/dynamic_fieldsets/fieldsets_controller.rb', line 52

def children
  @fieldset = DynamicFieldsets::Fieldset.find params[:id]

  respond_to do |format|
    format.html
  end
end

#createObject

Save new record



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'app/controllers/dynamic_fieldsets/fieldsets_controller.rb', line 131

def create
  parent_id = params[:parent]
  @fieldset = DynamicFieldsets::Fieldset.new(params[:dynamic_fieldsets_fieldset])

  respond_to do |format|
    if @fieldset.save
      if !parent_id.empty?
        parent = DynamicFieldsets::Fieldset.find(parent_id)
        DynamicFieldsets::FieldsetChild.create( :fieldset => parent, :child => @fieldset )
        #relation = @fieldset.fieldset_children.build( :fieldset => parent )
        #relation.child = @fieldset
        #relation.save
      end
      format.html { redirect_to( dynamic_fieldsets_fieldset_path(@fieldset), :notice => "Successfully created a new fieldset" )}
    else
      format.html { render :action => "new" }
    end
  end
end

#destroyObject

Destroy existing record



165
166
167
168
169
170
171
172
# File 'app/controllers/dynamic_fieldsets/fieldsets_controller.rb', line 165

def destroy
  @fieldset = DynamicFieldsets::Fieldset.find(params[:id])
  @fieldset.destroy

  respond_to do |format|
    format.html { redirect_to( dynamic_fieldsets_fieldsets_path, :notice => "Fieldset deleted successfully!" )}
  end
end

#editObject

Edit existing record



43
44
45
46
47
48
49
# File 'app/controllers/dynamic_fieldsets/fieldsets_controller.rb', line 43

def edit
  @fieldset = DynamicFieldsets::Fieldset.find(params[:id])

  respond_to do |format|
    format.html
  end
end

#indexObject

Show all fieldsets



7
8
9
10
11
12
13
# File 'app/controllers/dynamic_fieldsets/fieldsets_controller.rb', line 7

def index
  @fieldsets = DynamicFieldsets::Fieldset.all

  respond_to do |format|
    format.html
  end
end

#newObject

Create new fieldset



34
35
36
37
38
39
40
# File 'app/controllers/dynamic_fieldsets/fieldsets_controller.rb', line 34

def new
  @fieldset = DynamicFieldsets::Fieldset.new()

  respond_to do |format|
    format.html
  end
end

#reorderObject



91
92
93
94
95
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
# File 'app/controllers/dynamic_fieldsets/fieldsets_controller.rb', line 91

def reorder
  root = params[:id]
  parent_child_pairs = params[:child].inject({}) do |hash,(key,val)|
    if val.eql? 'root' then hash[key.to_i] = root.to_i
    else hash[key.to_i] = val.to_i
    end
    hash
  end
  
  @order = {}
  parent_child_pairs.each do |key,val|
    if @order.keys.include? val
    then @order[val].push key
    else @order.merge! val=>[key]
    end
  end
  
  # e.g. { 1 => [6], 6 => [7,8] }
  # First number is always the root Fieldset id.
  # The rest are FieldsetChild ids.
  @order.each do |parent_identifier,children|
    if parent_identifier.eql? @order.first[0] # This is the first number:
    then parent_id = parent_identifier # the root fieldset id.
    # Otherwise, we need to retrieve the parent fieldset_id from the FieldsetChild's child_id.
    else parent_id = DynamicFieldsets::FieldsetChild.find(parent_identifier).child_id
    end
    children.each_with_index do |fieldset_child_id,index|
      fieldset_child = DynamicFieldsets::FieldsetChild.find fieldset_child_id
      fieldset_child.fieldset_id = parent_id
      fieldset_child.order_num = index+1
      fieldset_child.save
    end
  end

  respond_to do |format|
    format.json { render :json => @order }
  end
end

#rootsObject

Show all root fieldsets



16
17
18
19
20
21
22
# File 'app/controllers/dynamic_fieldsets/fieldsets_controller.rb', line 16

def roots
  @fieldsets = DynamicFieldsets::Fieldset.roots

  respond_to do |format|
    format.html { render action: 'index' }
  end
end

#showObject

Show single fieldset



25
26
27
28
29
30
31
# File 'app/controllers/dynamic_fieldsets/fieldsets_controller.rb', line 25

def show
  @fieldset = DynamicFieldsets::Fieldset.find(params[:id])

  respond_to do |format|
    format.html
  end
end

#updateObject

Update existing record



152
153
154
155
156
157
158
159
160
161
162
# File 'app/controllers/dynamic_fieldsets/fieldsets_controller.rb', line 152

def update
  @fieldset = DynamicFieldsets::Fieldset.find(params[:id])

  respond_to do |format|
    if @fieldset.update_attributes(params[:dynamic_fieldsets_fieldset])
      format.html { redirect_to( dynamic_fieldsets_fieldset_path(@fieldset), :notice => "Successfully updated a fieldset" )}
    else
      format.html { render :action => "edit" }
    end
  end
end