Commit aeef9a08cadfe496281ec3a94c5c0c5adbfc5406
Committed by
Paulo Meireles
1 parent
10f47566
Exists in
master
and in
28 other branches
[Mezuro] Readings in reading_group.
Showing
5 changed files
with
128 additions
and
30 deletions
Show diff stats
plugins/mezuro/controllers/myprofile/mezuro_plugin_reading_controller.rb
| @@ -12,10 +12,9 @@ class MezuroPluginReadingController < MezuroPluginMyprofileController | @@ -12,10 +12,9 @@ class MezuroPluginReadingController < MezuroPluginMyprofileController | ||
| 12 | def create | 12 | def create |
| 13 | reading_group_content = profile.articles.find(params[:id]) | 13 | reading_group_content = profile.articles.find(params[:id]) |
| 14 | 14 | ||
| 15 | - reading = Kalibro::Reading.new( params[:reading] ) | ||
| 16 | - reading.save(reading_group_content.reading_group_id) | ||
| 17 | - | ||
| 18 | - if( reading.errors.empty? ) | 15 | + reading = Kalibro::Reading.new params[:reading] |
| 16 | + | ||
| 17 | + if( reading.save(reading_group_content.reading_group_id) ) | ||
| 19 | redirect_to "/#{profile.identifier}/#{reading_group_content.name.downcase.gsub(/\s/, '-').gsub(/[^0-9A-Za-z\-]/, '')}" | 18 | redirect_to "/#{profile.identifier}/#{reading_group_content.name.downcase.gsub(/\s/, '-').gsub(/[^0-9A-Za-z\-]/, '')}" |
| 20 | else | 19 | else |
| 21 | redirect_to_error_page reading.errors[0].message | 20 | redirect_to_error_page reading.errors[0].message |
| @@ -28,43 +27,23 @@ class MezuroPluginReadingController < MezuroPluginMyprofileController | @@ -28,43 +27,23 @@ class MezuroPluginReadingController < MezuroPluginMyprofileController | ||
| 28 | @data_profile = reading_group_content.profile.identifier | 27 | @data_profile = reading_group_content.profile.identifier |
| 29 | @reading_group_content_id = reading_group_content.id | 28 | @reading_group_content_id = reading_group_content.id |
| 30 | 29 | ||
| 31 | - @reading_types = Kalibro::Reading.reading_types | ||
| 32 | - | ||
| 33 | - configurations = Kalibro::Configuration.all | ||
| 34 | - configurations = [] if (configurations.nil?) | ||
| 35 | - @configuration_select = configurations.map do |configuration| | ||
| 36 | - [configuration.name,configuration.id] | ||
| 37 | - end | ||
| 38 | - | ||
| 39 | - @reading = reading_group_content.repositories.select{ |reading| reading.id.to_s == params[:reading_id] }.first | 30 | + @reading = Kalibro::Reading.find params[:reading_id] |
| 40 | end | 31 | end |
| 41 | 32 | ||
| 42 | def update | 33 | def update |
| 43 | reading_group_content = profile.articles.find(params[:id]) | 34 | reading_group_content = profile.articles.find(params[:id]) |
| 44 | - | ||
| 45 | - reading = Kalibro::Reading.new( params[:reading] ) | ||
| 46 | - reading.save(reading_group_content.reading_group_id) | 35 | + reading = Kalibro::Reading.new params[:reading] |
| 47 | 36 | ||
| 48 | - if( reading.errors.empty? ) | ||
| 49 | - reading.process | 37 | + if( reading.save(reading_group_content.reading_group_id) ) |
| 50 | redirect_to "/profile/#{profile.identifier}/plugin/mezuro/reading/show/#{reading_group_content.id}?reading_id=#{reading.id}" | 38 | redirect_to "/profile/#{profile.identifier}/plugin/mezuro/reading/show/#{reading_group_content.id}?reading_id=#{reading.id}" |
| 51 | else | 39 | else |
| 52 | redirect_to_error_page reading.errors[0].message | 40 | redirect_to_error_page reading.errors[0].message |
| 53 | end | 41 | end |
| 54 | end | 42 | end |
| 55 | 43 | ||
| 56 | - def show | ||
| 57 | - reading_group_content = profile.articles.find(params[:id]) | ||
| 58 | - @reading_group_name = reading_group_content.name | ||
| 59 | - @reading = reading_group_content.repositories.select{ |reading| reading.id.to_s == params[:reading_id] }.first | ||
| 60 | - @configuration_name = Kalibro::Configuration.configuration_of(@reading.id).name | ||
| 61 | - @data_profile = reading_group_content.profile.identifier | ||
| 62 | - @data_content = reading_group_content.id | ||
| 63 | - end | ||
| 64 | - | ||
| 65 | def destroy | 44 | def destroy |
| 66 | reading_group_content = profile.articles.find(params[:id]) | 45 | reading_group_content = profile.articles.find(params[:id]) |
| 67 | - reading = reading_group_content.repositories.select{ |reading| reading.id.to_s == params[:reading_id] }.first | 46 | + reading = Kalibro::Reading.find params[:reading_id] |
| 68 | reading.destroy | 47 | reading.destroy |
| 69 | if( reading.errors.empty? ) | 48 | if( reading.errors.empty? ) |
| 70 | redirect_to "/#{profile.identifier}/#{reading_group_content.name.downcase.gsub(/\s/, '-')}" | 49 | redirect_to "/#{profile.identifier}/#{reading_group_content.name.downcase.gsub(/\s/, '-')}" |
plugins/mezuro/lib/kalibro/reading.rb
| @@ -7,7 +7,10 @@ class Kalibro::Reading < Kalibro::Model | @@ -7,7 +7,10 @@ class Kalibro::Reading < Kalibro::Model | ||
| 7 | end | 7 | end |
| 8 | 8 | ||
| 9 | def self.readings_of( group_id ) | 9 | def self.readings_of( group_id ) |
| 10 | - request(:readings_of, {:group_id => group_id})[:reading].to_a.map { |reading| new reading } | 10 | + response = request(:readings_of, {:group_id => group_id})[:reading] |
| 11 | + response = [] if response.nil? | ||
| 12 | + response = [response] if response.is_a?(Hash) | ||
| 13 | + response.map { |reading| new reading } | ||
| 11 | end | 14 | end |
| 12 | 15 | ||
| 13 | def self.reading_of( range_id ) | 16 | def self.reading_of( range_id ) |
plugins/mezuro/test/functional/myprofile/mezuro_plugin_reading_controller_test.rb
0 → 100644
| @@ -0,0 +1,95 @@ | @@ -0,0 +1,95 @@ | ||
| 1 | +require 'test_helper' | ||
| 2 | + | ||
| 3 | +require "#{RAILS_ROOT}/plugins/mezuro/test/fixtures/reading_fixtures" | ||
| 4 | +require "#{RAILS_ROOT}/plugins/mezuro/test/fixtures/reading_group_content_fixtures" | ||
| 5 | + | ||
| 6 | +class MezuroPluginReadingControllerTest < ActionController::TestCase | ||
| 7 | + | ||
| 8 | + def setup | ||
| 9 | + @controller = MezuroPluginReadingController.new | ||
| 10 | + @request = ActionController::TestRequest.new | ||
| 11 | + @response = ActionController::TestResponse.new | ||
| 12 | + @profile = fast_create(Community) | ||
| 13 | + | ||
| 14 | + @reading = ReadingFixtures.reading | ||
| 15 | + @created_reading = ReadingFixtures.created_reading | ||
| 16 | + @reading_hash = ReadingFixtures.hash | ||
| 17 | + @content = MezuroPlugin::ReadingGroupContent.new(:profile => @profile, :name => name) | ||
| 18 | + @content.expects(:send_reading_group_to_service).returns(nil) | ||
| 19 | + @content.stubs(:solr_save) | ||
| 20 | + @content.save | ||
| 21 | + end | ||
| 22 | + | ||
| 23 | + should 'set variables to create a new reading' do | ||
| 24 | + get :new, :profile => @profile.identifier, :id => @content.id | ||
| 25 | + | ||
| 26 | + assert_equal @content.id, assigns(:reading_group_content_id) | ||
| 27 | + assert_equal @content.name, assigns(:reading_group_name) | ||
| 28 | + assert_equal @content.profile.identifier, assigns(:data_profile) | ||
| 29 | + end | ||
| 30 | + | ||
| 31 | + should 'create a reading' do | ||
| 32 | + Kalibro::Reading.expects(:new).with(@reading_hash.to_s).returns(@created_reading) | ||
| 33 | + @created_reading.expects(:save).with(@content.reading_group_id).returns(true) | ||
| 34 | + get :create, :profile => @profile.identifier, :id => @content.id, :reading => @reading_hash | ||
| 35 | + assert @created_reading.errors.empty? | ||
| 36 | + assert_response :redirect | ||
| 37 | + end | ||
| 38 | + | ||
| 39 | + should 'put an Exception in reading when an error occurs in create action' do | ||
| 40 | + @created_reading.errors = [Exception.new] | ||
| 41 | + Kalibro::Reading.expects(:new).with(@reading_hash.to_s).returns(@created_reading) | ||
| 42 | + @created_reading.expects(:save).with(@content.reading_group_id).returns(false) | ||
| 43 | + get :create, :profile => @profile.identifier, :id => @content.id, :reading => @reading_hash | ||
| 44 | + assert !@created_reading.errors.empty? | ||
| 45 | + assert_response :redirect | ||
| 46 | + end | ||
| 47 | + | ||
| 48 | + should 'set variables to edit a reading' do | ||
| 49 | + Kalibro::Reading.expects(:find).with(@reading.id.to_s).returns(@reading) | ||
| 50 | + | ||
| 51 | + get :edit, :profile => @profile.identifier, :id => @content.id, :reading_id => @reading.id | ||
| 52 | + | ||
| 53 | + assert_equal @content.id, assigns(:reading_group_content_id) | ||
| 54 | + assert_equal @content.name, assigns(:reading_group_name) | ||
| 55 | + assert_equal @content.profile.identifier, assigns(:data_profile) | ||
| 56 | + assert_equal @reading, assigns(:reading) | ||
| 57 | + end | ||
| 58 | + | ||
| 59 | + should 'update a reading' do | ||
| 60 | + Kalibro::Reading.expects(:new).with(@reading_hash.to_s).returns(@reading) | ||
| 61 | + @reading.expects(:save).with(@content.reading_group_id).returns(true) | ||
| 62 | + get :update, :profile => @profile.identifier, :id => @content.id, :reading => @reading_hash | ||
| 63 | + assert @reading.errors.empty? | ||
| 64 | + assert_response :redirect | ||
| 65 | + end | ||
| 66 | + | ||
| 67 | + should 'put an Exception in reading when an error occurs in update action' do | ||
| 68 | + @reading.errors = [Exception.new] | ||
| 69 | + Kalibro::Reading.expects(:new).with(@reading_hash.to_s).returns(@reading) | ||
| 70 | + @reading.expects(:save).with(@content.reading_group_id).returns(false) | ||
| 71 | + get :update, :profile => @profile.identifier, :id => @content.id, :reading => @reading_hash | ||
| 72 | + assert_response :redirect | ||
| 73 | + end | ||
| 74 | + | ||
| 75 | + should 'destroy a reading' do | ||
| 76 | + @reading.expects(:destroy) | ||
| 77 | + Kalibro::Reading.expects(:find).with(@reading.id.to_s).returns(@reading) | ||
| 78 | + | ||
| 79 | + get :destroy, :profile => @profile.identifier, :id => @content.id, :reading_id => @reading.id | ||
| 80 | + | ||
| 81 | + assert @reading.errors.empty? | ||
| 82 | + assert_response :redirect | ||
| 83 | + end | ||
| 84 | + | ||
| 85 | + should 'put an Exception in reading when an error occurs in destroy action' do | ||
| 86 | + @reading.errors = [Exception.new] | ||
| 87 | + @reading.expects(:destroy) | ||
| 88 | + Kalibro::Reading.expects(:find).with(@reading.id.to_s).returns(@reading) | ||
| 89 | + | ||
| 90 | + get :destroy, :profile => @profile.identifier, :id => @content.id, :reading_id => @reading.id | ||
| 91 | + | ||
| 92 | + assert !@reading.errors.empty? | ||
| 93 | + assert_response :redirect | ||
| 94 | + end | ||
| 95 | +end |
plugins/mezuro/views/cms/mezuro_plugin/_reading_group_content.html.erb
| @@ -8,7 +8,7 @@ | @@ -8,7 +8,7 @@ | ||
| 8 | 8 | ||
| 9 | <%= hidden_field_tag 'reading_group_content[profile_id]', profile.id %> | 9 | <%= hidden_field_tag 'reading_group_content[profile_id]', profile.id %> |
| 10 | <%= hidden_field_tag 'id', @article.id %> | 10 | <%= hidden_field_tag 'id', @article.id %> |
| 11 | -<%= hidden_field_tag 'reading_group_id', reading_group.id %> | 11 | +<%= hidden_field_tag 'reading_group_id', (reading_group.id unless reading_group.nil?) %> |
| 12 | 12 | ||
| 13 | <%= required_fields_message %> | 13 | <%= required_fields_message %> |
| 14 | 14 |
plugins/mezuro/views/mezuro_plugin_reading/edit.html.erb
0 → 100644
| @@ -0,0 +1,21 @@ | @@ -0,0 +1,21 @@ | ||
| 1 | +<script src="/javascripts/colorpicker.js" type="text/javascript"></script> | ||
| 2 | +<script src="/javascripts/colorpicker-noosfero.js" type="text/javascript"></script> | ||
| 3 | +<h3> <%= link_to( @reading_group_name, homepage_url(@data_profile, @reading_group_name.downcase.gsub(/[^0-9A-Za-z\-]/, '')) ) %></h3> | ||
| 4 | + | ||
| 5 | +<% form_for :reading, :url => {:action =>"create", :controller => "mezuro_plugin_reading"}, :method => :get do |f| %> | ||
| 6 | + <%= hidden_field_tag :id, @reading_group_content_id %> | ||
| 7 | + | ||
| 8 | + <%= f.hidden_field :id %> | ||
| 9 | + <%= required labelled_form_field _('label:'), f.text_field(:label) %> | ||
| 10 | + | ||
| 11 | + <%= required labelled_form_field _('grade:'), | ||
| 12 | + f.text_field(:grade) %> | ||
| 13 | + | ||
| 14 | + <%= required labelled_form_field _('color:'), | ||
| 15 | + colorpicker_field(:reading, :color) %>Click in the field to change Color | ||
| 16 | + | ||
| 17 | + <p> | ||
| 18 | + <%= f.submit "Add" %> | ||
| 19 | + </p> | ||
| 20 | + | ||
| 21 | +<% end %> |