Commit 5d4a0723d1bc83fed7709faecf699b263ae2ea6f

Authored by Daniela Feitosa
2 parents 61931718 06036612

Merge branch 'custom_field_on_moderation' into 'master'

Adds custom_fields details to moderation tasks

Adds custom_values to community when moderated;
Adds option to show custom_field in moderation task;

    Signed-off-by: Alexandre Barbosa <alexandreab@live.com>
    Signed-off-by: Arthur Jahn <stutrzbecher@gmail.com>
    Signed-off-by: Marcos Ronaldo <marcos.rpj2@gmail.com>

See merge request !825
app/controllers/my_profile/tasks_controller.rb
... ... @@ -4,6 +4,7 @@ class TasksController &lt; MyProfileController
4 4  
5 5 protect [:perform_task, :view_tasks], :profile, :only => [:index]
6 6 protect :perform_task, :profile, :except => [:index]
  7 + helper CustomFieldsHelper
7 8  
8 9 def index
9 10 @filter_type = params[:filter_type].presence
... ...
app/helpers/custom_fields_helper.rb
... ... @@ -37,8 +37,15 @@ module CustomFieldsHelper
37 37 end
38 38  
39 39 def display_custom_field_value(custom_field_value)
40   - value = profile.custom_value(custom_field_value.custom_field.name)
41   - case custom_field_value.custom_field.format
  40 + value_for_format custom_field_value.custom_field.format, custom_field_value.value
  41 + end
  42 +
  43 + def display_value_for_custom_field(custom_field, value)
  44 + value_for_format custom_field.format, value
  45 + end
  46 +
  47 + def value_for_format format, value
  48 + case format
42 49 when 'text', 'list', 'numeric', 'date', 'string'
43 50 value
44 51 when 'checkbox'
... ... @@ -47,6 +54,7 @@ module CustomFieldsHelper
47 54 url = value[/\Ahttps?:\/\//i] ? value : "http://#{value}"
48 55 link_to(value, url, :target => '_blank')
49 56 end
  57 +
50 58 end
51 59  
52 60 private
... ...
app/models/community.rb
... ... @@ -33,7 +33,7 @@ class Community &lt; Organization
33 33 community = Community.new(attributes)
34 34 community.environment = environment
35 35 if community.environment.enabled?('admin_must_approve_new_communities')
36   - CreateCommunity.create!(attributes.merge(:requestor => requestor, :environment => environment).except(:custom_values))
  36 + CreateCommunity.create!(attributes.merge(:requestor => requestor, :environment => environment))
37 37 else
38 38 community.save!
39 39 community.add_admin(requestor)
... ...
app/models/create_community.rb
... ... @@ -20,6 +20,9 @@ class CreateCommunity &lt; Task
20 20 attr_accessible field.to_sym
21 21 end
22 22  
  23 + settings_items :custom_values
  24 + attr_accessible :custom_values
  25 +
23 26 def validate
24 27 self.environment.required_community_fields.each do |field|
25 28 if self.send(field).blank?
... ... @@ -36,6 +39,7 @@ class CreateCommunity &lt; Task
36 39  
37 40 community.update(community_data)
38 41 community.image = image if image
  42 + community.custom_values = custom_values
39 43 community.environment = self.environment
40 44 community.save!
41 45 community.add_admin(self.requestor)
... ... @@ -67,6 +71,10 @@ class CreateCommunity &lt; Task
67 71 true
68 72 end
69 73  
  74 + def custom_fields_moderate
  75 + true
  76 + end
  77 +
70 78 # tells if this request was rejected
71 79 def rejected?
72 80 self.status == Task::Status::CANCELLED
... ...
app/models/custom_field.rb
1 1 class CustomField < ActiveRecord::Base
2   - attr_accessible :name, :default_value, :format, :extras, :customized_type, :active, :required, :signup, :environment
  2 + attr_accessible :name, :default_value, :format, :extras, :customized_type, :active, :required, :signup, :environment, :moderation_task
3 3 serialize :customized_type
4 4 serialize :extras
5 5 has_many :custom_field_values, :dependent => :delete_all
... ...
app/models/moderate_user_registration.rb
... ... @@ -22,6 +22,10 @@ class ModerateUserRegistration &lt; Task
22 22 "#{name} (#{email})"
23 23 end
24 24  
  25 + def custom_fields_moderate
  26 + true
  27 + end
  28 +
25 29 def perform
26 30 user=environment.users.find_by_id(user_id)
27 31 user.activate
... ... @@ -58,4 +62,4 @@ class ModerateUserRegistration &lt; Task
58 62 _("User \"%{user}\" just requested to register. You have to approve or reject it through the \"Pending Validations\" section in your control panel.\n") % { :user => self.name }
59 63 end
60 64  
61   -end
62 65 \ No newline at end of file
  66 +end
... ...
app/models/task.rb
... ... @@ -182,6 +182,10 @@ class Task &lt; ActiveRecord::Base
182 182 false
183 183 end
184 184  
  185 + def custom_fields_moderate
  186 + false
  187 + end
  188 +
185 189 def icon
186 190 {:type => :defined_image, :src => "/images/icons-app/user-minor.png", :name => requestor.name, :url => requestor.url}
187 191 end
... ...
app/views/features/custom_fields/_form.html.erb
... ... @@ -16,6 +16,7 @@
16 16 <%= labelled_check_box _('Active'), "custom_fields[#{id}][active]", 1, field.active, :id => "active_checkbox", :onclick => "active_action('custom_fields[#{id}][active]','custom_fields[#{id}][required]', 'custom_fields[#{id}][signup]')" %>
17 17 <%= labelled_check_box _('Required'), "custom_fields[#{id}][required]", 1, field.required, :id => "required_checkbox", :onclick => "required_action('custom_fields[#{id}][active]','custom_fields[#{id}][required]', 'custom_fields[#{id}][signup]')" %>
18 18 <%= labelled_check_box _('Display on creation?'), "custom_fields[#{id}][signup]", 1, field.signup, :id => "signup_checkbox",:onclick => "signup_action('custom_fields[#{id}][active]','custom_fields[#{id}][required]', 'custom_fields[#{id}][signup]')" %>
  19 + <%= labelled_check_box _('Display on moderation?'), "custom_fields[#{id}][moderation_task]", 1, field.moderation_task %>
19 20 </div>
20 21  
21 22 <% if field.format == "list" %>
... ...
app/views/tasks/_create_community_custom_fields.html.erb 0 → 100644
... ... @@ -0,0 +1,10 @@
  1 +<% if task.custom_values.present? %>
  2 + <% task.custom_values.each_pair do |name, value| %>
  3 + <% custom_field = CustomField.where(:name =>name, :environment => task.environment).first%>
  4 + <% if custom_field.moderation_task %>
  5 + <div class="field-name">
  6 + <%= name +": " + display_value_for_custom_field(custom_field, value['value']) %>
  7 + </div>
  8 + <% end %>
  9 + <% end %>
  10 +<% end %>
... ...
app/views/tasks/_moderate_user_registration_custom_fields.html.erb 0 → 100644
... ... @@ -0,0 +1,7 @@
  1 +<% task.requestor.custom_field_values.each do |custom_value| %>
  2 + <% if custom_value.custom_field.moderation_task %>
  3 + <div class="field-name">
  4 + <%= custom_value.custom_field.name + ": " + display_custom_field_value(custom_value) %>
  5 + </div>
  6 + <% end %>
  7 +<% end %>
... ...
app/views/tasks/_task.html.erb
... ... @@ -51,6 +51,12 @@
51 51 </div>
52 52  
53 53 <%= fields_for "tasks[#{task.id}][task]", task do |f| %>
  54 + <% if task.custom_fields_moderate %>
  55 + <div id="custom-field-information-<%=task.id%>" style="display: none">
  56 + <%= render :partial => task.class.name.underscore+'_custom_fields', :locals => {:task => task} %>
  57 + </div>
  58 + <% end %>
  59 +
54 60 <% if task.accept_details %>
55 61 <div id="on-accept-information-<%=task.id%>" style="display: none">
56 62 <%= render :partial => partial_for_class(task.class, nil, :accept_details), :locals => {:task => task, :f => f} %>
... ...
db/migrate/20160324132518_add_moderation_task_to_custom_field.rb 0 → 100644
... ... @@ -0,0 +1,9 @@
  1 +class AddModerationTaskToCustomField < ActiveRecord::Migration
  2 + def up
  3 + add_column :custom_fields, :moderation_task, :boolean, :default => false
  4 + end
  5 +
  6 + def down
  7 + remove_column :custom_fields, :moderation_task
  8 + end
  9 +end
... ...
db/schema.rb
... ... @@ -11,7 +11,7 @@
11 11 #
12 12 # It's strongly recommended that you check this file into your version control system.
13 13  
14   -ActiveRecord::Schema.define(version: 20160309122141) do
  14 +ActiveRecord::Schema.define(version: 20160324132518) do
15 15  
16 16 # These are extensions that must be enabled in order to support this database
17 17 enable_extension "plpgsql"
... ... @@ -323,6 +323,7 @@ ActiveRecord::Schema.define(version: 20160309122141) do
323 323 t.integer "environment_id"
324 324 t.datetime "created_at"
325 325 t.datetime "updated_at"
  326 + t.boolean "moderation_task", default: false
326 327 end
327 328  
328 329 add_index "custom_fields", ["customized_type", "name", "environment_id"], name: "index_custom_field", unique: true, using: :btree
... ...
public/javascripts/tasks.js
... ... @@ -5,6 +5,7 @@
5 5 $('#on-accept-information-' + task_id).show('fast');
6 6 $('#on-reject-information-' + task_id).hide('fast');
7 7 $('#on-skip-information-' + task_id).hide('fast');
  8 + $('#custom-field-information-' + task_id).show('fast');
8 9 })
9 10  
10 11 $("input.task_reject_radio").click(function(){
... ... @@ -12,6 +13,7 @@
12 13 $('#on-accept-information-' + task_id).hide('fast');
13 14 $('#on-reject-information-' + task_id).show('fast');
14 15 $('#on-skip-information-' + task_id).hide('fast');
  16 + $('#custom-field-information-' + task_id).show('fast');
15 17 })
16 18  
17 19 $("input.task_skip_radio").click(function(){
... ... @@ -19,6 +21,7 @@
19 21 $('#on-accept-information-' + task_id).hide('fast');
20 22 $('#on-reject-information-' + task_id).hide('fast');
21 23 $('#on-skip-information-' + task_id).show('fast');
  24 + $('#custom-field-information-' + task_id).hide('fast');
22 25 })
23 26  
24 27 // There is probably an elegant way to do this...
... ...
test/functional/tasks_controller_test.rb
... ... @@ -670,4 +670,38 @@ class TasksControllerTest &lt; ActionController::TestCase
670 670 assert_equal profile, t.reload.closed_by
671 671 end
672 672  
  673 + should 'list custom field details in moderation user tasks when moderation_tasks is true' do
  674 + person_custom_field = CustomField.create(:name => "great_field", :format=>"string", :default_value => "value for person", :customized_type=>"Person", :active => true, :environment => Environment.default, :moderation_task => true, :required => true)
  675 + p1 = create_user("great_person").person
  676 + p1.custom_values = {"great_field" => "new_value!"}
  677 + p1.save!
  678 + p1.reload
  679 + admin = create_user("admin").person
  680 + Environment.default.add_admin(admin)
  681 + @controller.stubs(:profile).returns(admin)
  682 + login_as "admin"
  683 +
  684 + ModerateUserRegistration.create!(:requestor => p1, :name => "great_person", :email => "alo@alo.alo", :target => Environment.default)
  685 +
  686 + get :index
  687 +
  688 + assert_tag :tag=> 'div', :attributes => { :class => 'field-name' }, :content => /great_field: new_value!/
  689 + end
  690 +
  691 + should 'list custom field details in moderation of community creation tasks when moderation_tasks is true' do
  692 + community_custom_field = CustomField.create(:name => "great_field", :format=>"string", :default_value => "value for community", :customized_type=>"Community", :active => true, :environment => Environment.default, :moderation_task => true, :required => true)
  693 + p1 = create_user("great_person").person
  694 + p1.save!
  695 + admin = create_user("admin").person
  696 + Environment.default.add_admin(admin)
  697 + @controller.stubs(:profile).returns(admin)
  698 + login_as "admin"
  699 +
  700 + CreateCommunity.create!(:requestor => p1, :name => "great_community", :target => Environment.default, :custom_values => {"great_field" => {"value" => "new value for community!"}})
  701 +
  702 + get :index
  703 +
  704 + assert_tag :tag=> 'div', :attributes => { :class => 'field-name' }, :content => /great_field: new value for community!/
  705 + end
  706 +
673 707 end
... ...