diff options
| author | mo k <mo.khan@gmail.com> | 2013-12-23 19:49:44 -0700 |
|---|---|---|
| committer | mo k <mo.khan@gmail.com> | 2013-12-23 19:49:44 -0700 |
| commit | bb3d66927684ca0269be7182fc08d3494245437d (patch) | |
| tree | e1527baf4cd4547aea398709b9d07505d2e1f437 | |
| parent | aafbb8027f0dad4107d8d2b812386e8e99b5a329 (diff) | |
| parent | 45c6bbc66580fcc39610659a956cf6ab82f7fa8e (diff) | |
Merged in clean-house (pull request #2)
Remove some comments and dead code.
| -rw-r--r-- | .vimrc | 2 | ||||
| -rw-r--r-- | app/controllers/application_controller.rb | 3 | ||||
| -rw-r--r-- | app/controllers/favorites_controller.rb | 1 | ||||
| -rw-r--r-- | app/models/admin_user.rb | 8 | ||||
| -rw-r--r-- | app/models/category.rb | 1 | ||||
| -rw-r--r-- | app/models/creation.rb | 27 | ||||
| -rw-r--r-- | app/models/favorite.rb | 1 | ||||
| -rw-r--r-- | app/models/interest.rb | 1 | ||||
| -rw-r--r-- | app/models/photo.rb | 1 | ||||
| -rw-r--r-- | app/models/tutorial.rb | 3 | ||||
| -rw-r--r-- | app/models/user.rb | 2 | ||||
| -rw-r--r-- | app/services/dto/display_creation_dto.rb | 9 | ||||
| -rw-r--r-- | app/services/mappers/creation_to_display_creation_dto_mapper.rb | 13 | ||||
| -rw-r--r-- | app/services/queries/find_all_creations_query.rb | 12 | ||||
| -rw-r--r-- | app/views/layouts/_header.html.erb | 2 | ||||
| -rw-r--r-- | spec/services/queries/find_all_creations_query_spec.rb | 22 |
16 files changed, 41 insertions, 67 deletions
diff --git a/.vimrc b/.vimrc deleted file mode 100644 index 6a322fb5..00000000 --- a/.vimrc +++ /dev/null @@ -1,2 +0,0 @@ -nmap ,t :w\|:!clear && script/test %<cr> -nmap ,ta :w\|:!script/test<cr> diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 0db1df02..39476b91 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -18,9 +18,6 @@ class ApplicationController < ActionController::Base end def load_categories - #@categories = Rails.cache.fetch("categories-#{Category.count}") do - #Category.all - #end @categories = Category.all end diff --git a/app/controllers/favorites_controller.rb b/app/controllers/favorites_controller.rb index 10052ddb..8ac00a75 100644 --- a/app/controllers/favorites_controller.rb +++ b/app/controllers/favorites_controller.rb @@ -22,6 +22,7 @@ class FavoritesController < ApplicationController end private + def find_creation @creation = Creation.find(params[:creation_id]) raise ActiveRecord::RecordNotFound unless @creation diff --git a/app/models/admin_user.rb b/app/models/admin_user.rb index 5a38533f..492d9a40 100644 --- a/app/models/admin_user.rb +++ b/app/models/admin_user.rb @@ -1,9 +1,3 @@ class AdminUser < ActiveRecord::Base - # Include default devise modules. Others available are: - # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable - devise :database_authenticatable, - :recoverable, :rememberable, :trackable, :validatable - - # Setup accessible (or protected) attributes for your model - #attr_accessible :email, :password, :password_confirmation, :remember_me + devise :database_authenticatable, :recoverable, :rememberable, :trackable, :validatable end diff --git a/app/models/category.rb b/app/models/category.rb index 2495135f..4ccdc9c8 100644 --- a/app/models/category.rb +++ b/app/models/category.rb @@ -1,6 +1,5 @@ class Category < ActiveRecord::Base has_and_belongs_to_many :creations, :join_table => 'creations_categories' - #attr_accessible :name, :slug default_scope -> { order(:name) } def to_param diff --git a/app/models/creation.rb b/app/models/creation.rb index 8f896af3..2e498df8 100644 --- a/app/models/creation.rb +++ b/app/models/creation.rb @@ -6,19 +6,14 @@ class Creation < ActiveRecord::Base has_many :favorites, :dependent => :destroy acts_as_taggable - default_scope -> { order("created_at DESC") } + default_scope -> { order(:created_at => :desc) } - # to be removed and moved to the DisplayCreationDTO def to_param "#{id}-#{name.gsub(/[^a-z0-9]+/i, '-')}" end def primary_image - if photos.any? - photos.first - else - Photo.new - end + photos.any? ? photos.first : Photo.new end def published? @@ -30,12 +25,7 @@ class Creation < ActiveRecord::Base end def add_photo(photo) - photos.create({:image => photo}) - end - - def self.search(query) - sql_search = "%#{query}%" - Creation.where("upper(name) like upper(?) OR upper(story) like upper(?)", sql_search, sql_search) + photos.create(:image => photo) end def is_liked_by(user) @@ -50,7 +40,14 @@ class Creation < ActiveRecord::Base end end - def self.visible_creations - Creation.includes(:user, :photos).where(:is_restricted => false).where('photos_count > 0').uniq + class << self + def search(query) + sql_search = "%#{query}%" + Creation.where("upper(name) like upper(?) OR upper(story) like upper(?)", sql_search, sql_search) + end + + def visible_creations + Creation.includes(:user, :photos).where(:is_restricted => false).where('photos_count > 0').uniq + end end end diff --git a/app/models/favorite.rb b/app/models/favorite.rb index 51190099..6818820e 100644 --- a/app/models/favorite.rb +++ b/app/models/favorite.rb @@ -1,5 +1,4 @@ class Favorite < ActiveRecord::Base belongs_to :user belongs_to :creation, :counter_cache => true - #attr_accessible :user_id, :creation_id end diff --git a/app/models/interest.rb b/app/models/interest.rb index 54a8c850..a52286d4 100644 --- a/app/models/interest.rb +++ b/app/models/interest.rb @@ -1,3 +1,2 @@ class Interest < ActiveRecord::Base - #attr_accessible :name end diff --git a/app/models/photo.rb b/app/models/photo.rb index c77d2120..87e20c3c 100644 --- a/app/models/photo.rb +++ b/app/models/photo.rb @@ -1,5 +1,4 @@ class Photo < ActiveRecord::Base - #attr_accessible :is_primary, :creation, :image belongs_to :creation, :counter_cache => true validates :image, :presence => true mount_uploader :image, PhotoUploader diff --git a/app/models/tutorial.rb b/app/models/tutorial.rb index 4b9f498a..4e6456e5 100644 --- a/app/models/tutorial.rb +++ b/app/models/tutorial.rb @@ -1,11 +1,8 @@ class Tutorial < ActiveRecord::Base - #attr_accessible :description, :heading, :url, :image_url, :user_id, :author, :author_url validates :url, :presence => true belongs_to :user acts_as_taggable - #default_scope -> { order("created_at DESC") } - def to_param "#{id}-#{heading.gsub(/[^a-z0-9]+/i, '-')}" end diff --git a/app/models/user.rb b/app/models/user.rb index f892b6db..2ae299f6 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -10,11 +10,9 @@ class User < ActiveRecord::Base validates :name, :presence => true validates :website, :format => URI::regexp(%w(http https)), :allow_blank => true devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :token_authenticatable - #attr_accessible :name, :email, :website, :twitter, :facebook, :city, :latitude, :longitude, :password, :password_confirmation, :current_password, :remember_me, :interest_ids has_many :creations, :dependent => :destroy has_many :favorites, :dependent => :destroy has_many :tutorials, :dependent => :destroy - #has_and_belongs_to_many :interests, :join_table => 'users_interests', uniq: true, :autosave => true has_and_belongs_to_many :interests, :join_table => 'users_interests', :autosave => true has_one :avatar acts_as_tagger diff --git a/app/services/dto/display_creation_dto.rb b/app/services/dto/display_creation_dto.rb deleted file mode 100644 index b80b70d2..00000000 --- a/app/services/dto/display_creation_dto.rb +++ /dev/null @@ -1,9 +0,0 @@ -class DisplayCreationDTO - attr_accessor :id, :name, :story, :primary_image, :thumb_url, :user, :favorites - def self.model_name - ActiveModel::Name.new(self, nil, 'Creation') - end - def to_param - "#{id}-#{name.gsub(/[^a-z0-9]+/i, '-')}" - end -end diff --git a/app/services/mappers/creation_to_display_creation_dto_mapper.rb b/app/services/mappers/creation_to_display_creation_dto_mapper.rb deleted file mode 100644 index efa41c29..00000000 --- a/app/services/mappers/creation_to_display_creation_dto_mapper.rb +++ /dev/null @@ -1,13 +0,0 @@ -class CreationToDisplayCreationDTOMapper - def map_from(creation) - dto = DisplayCreationDTO.new - dto.id = creation.id - dto.name = creation.name - dto.story = creation.story - dto.primary_image = creation.primary_image - dto.thumb_url = creation.primary_image.image.thumb.url - dto.user = creation.user - dto.favorites = creation.favorites - dto - end -end diff --git a/app/services/queries/find_all_creations_query.rb b/app/services/queries/find_all_creations_query.rb index acb8bbfe..f67ca694 100644 --- a/app/services/queries/find_all_creations_query.rb +++ b/app/services/queries/find_all_creations_query.rb @@ -1,15 +1,9 @@ class FindAllCreationsQuery - def initialize(mapper = CreationToDisplayCreationDTOMapper.new) - @mapper = mapper + def initialize(repository = Creation) + @repository = repository end def fetch(params) - find_creations.page(params[:page]).per(9) - end - - private - - def find_creations - Creation.includes(:user, :photos).where(:is_restricted => false).where('photos_count > 0').uniq + @repository.visible_creations.page(params[:page]).per(9) end end diff --git a/app/views/layouts/_header.html.erb b/app/views/layouts/_header.html.erb index 9f1c2a07..84d8aa0d 100644 --- a/app/views/layouts/_header.html.erb +++ b/app/views/layouts/_header.html.erb @@ -13,9 +13,11 @@ <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown">Categories<b class="caret"></b></a> <ul class="dropdown-menu"> + <% cache @categories do %> <% @categories.each do |category| %> <li><%= link_to category.name, category_path(category) %></li> <% end %> + <% end %> </ul> </li> </ul> diff --git a/spec/services/queries/find_all_creations_query_spec.rb b/spec/services/queries/find_all_creations_query_spec.rb new file mode 100644 index 00000000..be36c437 --- /dev/null +++ b/spec/services/queries/find_all_creations_query_spec.rb @@ -0,0 +1,22 @@ +require "spec_helper" + +describe FindAllCreationsQuery do + let(:sut) { FindAllCreationsQuery.new } + let(:results) { sut.fetch({}) } + + let!(:cake_with_a_photo) { create(:creation, photos: [create(:photo)]) } + let!(:cake_without_a_photo) { create(:creation, photos: []) } + let!(:restricted_cake) { create(:creation, is_restricted: true, photos: [create(:photo)]) } + + it "returns all creations with at least one photo" do + results.should include(cake_with_a_photo) + end + + it "ignores cakes without a photo" do + results.should_not include(cake_without_a_photo) + end + + it "ignores restricted cakes" do + results.should_not include(restricted_cake) + end +end |
