I have some models which share the same functionality just on other paths. So I decided to put these methods in a module and set the path in the model. My problem is that I'm not able to access the attribute in my module.
model:
class Job < ActiveRecord::Base
include ImageModel
image_dir = "jobs"
end
module:
module ImageModel
extend ActiveSupport::Concern
def delete_image
unless pic_link == "" || pic_link == nil
begin
if File.delete(Rails.root.join("public", "images", image_dir, pic_link))
return true
else
return false
end
rescue
return true #an error occured but when the image does not exist we still return true
end
end
return true
end
def replace_image(new_image)
File.open(Rails.root.join("public", "images", image_dir, new_image.original_filename), "wb") do |f|
if f.write new_image.read
delete_image
pic_link = new_image.original_filename
return true #everything went fine
else
return false #return false if new image could not be written
end
end
end
end
The error i get:
undefined local variable or method `image_dir' for #<Job:0x007f8a93b9e8d8>
on this line:
File.open(Rails.root.join("public", "images", image_dir, new_image.original_filename), "wb") do |f|
Did I miss something or did I oversee something important?
Felix
I think the design of module still have room to improve. But for this specific question, here is the quickfix.
class Job < ActiveRecord::Base
include ImageModel
def image_dir
"jobs"
end
end
You should define your image_dir = "jobs" in the module itself. because you are including your module in your model, and your module is not able to get the declaration, you have done in your model.
Or you can change your delete_image method to take parameters :
def delete_image(image_dir)
unless pic_link == "" || pic_link == nil
begin
if File.delete(Rails.root.join("public", "images", image_dir, pic_link))
return true
else
return false
end
rescue
return true #an error occured but when the image does not exist we still return true
end
end
return true
end
and where you are calling that method, pass an argument like this:
delete_image("jobs")
same in the case of replace_image method:
def replace_image(new_image, image_dir)
File.open(Rails.root.join("public", "images", image_dir, new_image.original_filename), "wb") do |f|
if f.write new_image.read
delete_image
pic_link = new_image.original_filename
return true #everything went fine
else
return false #return false if new image could not be written
end
end
end
Hope it will help. Thanks
Related
I've got a field from github webhook - webhook.repository.private - which checks if created repository was private (boolean). I want use return if block to handle scenario:
check if webhook.repository.private is true and if not call new class PublicRepositoryCreated but if this is true - return and execute fields_hash
code below:
def required_fields
PublicRepositoryCreated.new(webhook).call unless webhook.repository.private
fields_hash
end
private
def fields_hash
{
'fields' => {
'summary' => 'summary',
'description' => 'description',
'project' => '123'
}
}
end
Right now it seems that fields_hash is still executed even when webhook.repository.private is false
You have multiple ways of solving your problem.
You can either :
call your function and return
def required_fields
PublicRepositoryCreated.new(webhook).call && return unless webhook.repository.private
fields_hash
end
return your function
def required_fields
return PublicRepositoryCreated.new(webhook).call unless webhook.repository.private
fields_hash
end
use a ternary
def required_fields
webhook.repository.private ? fields_hash : PublicRepositoryCreated.new(webhook).call
end
I have a Sidekiq worker and in it using a case statement:
module Api
def self.authenticate(company_id)
Thread.current['api_company_id'] = company_id
yield if block_given?
ensure
Thread.current['api_company_id'] = nil
end
end
module Apis
class PollTrackableJobWorker
include Sidekiq::Worker
class EDocumentNotDoneError < StandardError; end
class UnhandledCaseError < StandardError; end
def perform(job_id, _invoice_id)
Api.authenticate(1) do
response = Api::TrackableJob.find(job_id).first
case response['status']
when 'done' then return true
when 'error' then return handle_error(response['errors'])
when 'pending' || 'running' then return raise EDocumentNotDoneError
else raise UnhandledCaseError, response
end
end
end
private
def handle_error(exception)
Bugsnag.notify(exception) if defined?(Bugsnag)
end
end
end
#perform method is tested for all possible outcomes. Below, is a case for done:
class PollTrackableJobWorkerTest < ActiveSupport::TestCase
test 'status is done' do
response = {
'type' => 'trackable_jobs',
'id' => 'xxxxx',
'status' => 'done',
'errors' => nil
}
Api.expects(:authenticate).with(1).yields
Api::TrackableJob.stubs(:find).with('xxxxx').returns([response])
assert_equal(true, Apis::PollTrackableJobWorker.new.perform('xxxxx', 123))
end
end
The test passes perfectly.
However, when I use implicit return, it keeps failing (returns nil).
For example, using a private method with an explicit return statement fails
case response['status']
when 'done' then returns_true
# code omitted
end
private
def returns_true
return true
end
or using implicit return fails too
when 'done' then true
Why do I need explicit return for case statements?
I have some service objects that use Nokogiri to make AR instances. I created a rake task so that I can update the instances with a cron job. What I want to test is if it's adding items that weren't there before, ie:
Create an Importer with a url of spec/fixtures/feed.xml, feed.xml having 10 items.
Expect Show.count == 1 and Episode.count == 10
Edit spec/fixtures/feed.xml to have 11 items
Invoke rake task
Expect Show.count == 1 and Episode.count == 11
How could I test this in RSpec, or modify my code to be more testable?
# models/importer.rb
class Importer < ActiveRecord::Base
after_create :parse_importer
validates :title, presence: true
validates :url, presence: true
validates :feed_format, presence: true
private
def parse_importer
Parser.new(self)
end
end
# models/show.rb
class Show < ActiveRecord::Base
validates :title, presence: true
validates :title, uniqueness: true
has_many :episodes
attr_accessor :entries
end
# models/episode.rb
class Episode < ActiveRecord::Base
validates :title, presence: true
validates :title, uniqueness: true
belongs_to :show
end
#lib/tasks/admin.rake
namespace :admin do
desc "Checks all Importer URLs for new items."
task refresh: :environment do
#importers = Importer.all
#importers.each do |importer|
Parser.new(importer)
end
end
end
# services/parser.rb
class Parser
def initialize(importer)
feed = Feed.new(importer)
show = Show.where(rss_link: importer.url).first
if show # add new episodes
new_episodes = Itunes::Channel.refresh(feed.origin)
new_episodes.each do |new_episode|
show.episodes.create feed.episode(new_episode)
end
else # create a show and its episodes
new_show = Show.new(feed.show) if (feed && feed.show)
if (new_show.save && new_show.entries.any?)
new_show.entries.each do |entry|
new_show.episodes.create feed.episode(entry)
end
end
end
end
end
# services/feed.rb
class Feed
require "nokogiri"
require "open-uri"
require "formats/itunes"
attr_reader :params, :origin, :show, :episode
def initialize(params)
#params = params
end
def origin
#origin = Nokogiri::XML(open(params[:url]))
end
def format
#format = params[:feed_format]
end
def show
case format
when "iTunes"
Itunes::Channel.fresh(origin)
end
end
def episode(entry)
#entry = entry
case format
when "iTunes"
Itunes::Item.fresh(#entry)
end
end
end
# services/formats/itunes.rb
class Itunes
class Channel
def initialize(origin)
#origin = origin
end
def title
#origin.xpath("//channel/title").text
end
def description
#origin.xpath("//channel/description").text
end
def summary
#origin.xpath("//channel/*[name()='itunes:summary']").text
end
def subtitle
#origin.xpath("//channel/*[name()='itunes:subtitle']/text()").text
end
def rss_link
#origin.xpath("//channel/*[name()='atom:link']/#href").text
end
def main_link
#origin.xpath("//channel/link/text()").text
end
def docs_link
#origin.xpath("//channel/docs/text()").text
end
def release
#origin.xpath("//channel/pubDate/text()").text
end
def image
#origin.xpath("//channel/image/url/text()").text
end
def language
#origin.xpath("//channel/language/text()").text
end
def keywords
keywords_array(#origin)
end
def categories
category_array(#origin)
end
def explicit
explicit_check(#origin)
end
def entries
entry_array(#origin)
end
def self.fresh(origin)
#show = Itunes::Channel.new origin
return {
description: #show.description,
release: #show.release,
explicit: #show.explicit,
language: #show.language,
title: #show.title,
summary: #show.summary,
subtitle: #show.subtitle,
image: #show.image,
rss_link: #show.rss_link,
main_link: #show.main_link,
docs_link: #show.docs_link,
categories: #show.categories,
keywords: #show.keywords,
entries: #show.entries
}
end
def self.refresh(origin)
#show = Itunes::Channel.new origin
return #show.entries
end
private
def category_array(channel)
arr = []
channel.xpath("//channel/*[name()='itunes:category']/#text").each do |category|
arr.push(category.to_s)
end
return arr
end
def explicit_check(channel)
string = channel.xpath("//channel/*[name()='itunes:explicit']").text
if string === "yes" || string === "Yes"
true
else
false
end
end
def keywords_array(channel)
keywords = channel.xpath("//channel/*[name()='itunes:keywords']/text()").text
arr = keywords.split(",")
return arr
end
def entry_array(channel)
arr = []
channel.xpath("//item").each do |item|
arr.push(item)
end
return arr
end
end
class Item
def initialize(origin)
#origin = origin
end
def description
#origin.xpath("*[name()='itunes:subtitle']").text
end
def release
#origin.xpath("pubDate").text
end
def image
#origin.xpath("*[name()='itunes:image']/#href").text
end
def explicit
explicit_check(#origin)
end
def duration
#origin.xpath("*[name()='itunes:duration']").text
end
def title
#origin.xpath("title").text
end
def enclosure_url
#origin.xpath("enclosure/#url").text
end
def enclosure_length
#origin.xpath("enclosure/#length").text
end
def enclosure_type
#origin.xpath("enclosure/#type").text
end
def keywords
keywords_array(#origin.xpath("*[name()='itunes:keywords']").text)
end
def self.fresh(entry)
#episode = Itunes::Item.new entry
return {
description: #episode.description,
release: #episode.release,
image: #episode.image,
explicit: #episode.explicit,
duration: #episode.duration,
title: #episode.title,
enclosure_url: #episode.enclosure_url,
enclosure_length: #episode.enclosure_length,
enclosure_type: #episode.enclosure_type,
keywords: #episode.keywords
}
end
private
def explicit_check(item)
string = item.xpath("*[name()='itunes:explicit']").text
if string === "yes" || string === "Yes"
true
else
false
end
end
def keywords_array(item)
keywords = item.split(",")
return keywords
end
end
end
Before anything else, good for you for using service objects! I've been using this approach a great deal and find POROs preferable to fat models in many situations.
It appears the behavior you're interested in testing is contained in Parser.initialize.
First, I'd create a class method for Parser called parse. IMO, Parser.parse(importer) is clearer about what Parser is doing than is Parser.new(importer). So, it might look like:
#services/parser.rb
class Parser
class << self
def parse(importer)
#importer = importer
#feed = Feed.new(importer)
if #show = Show.where(rss_link: importer.url).first
create_new_episodes Itunes::Channel.refresh(#feed.origin)
else
create_show_and_episodes
end
end # parse
end
end
Then add the create_new_episodes and create_show_and_episodes class methods.
#services/parser.rb
class Parser
class << self
def parse(importer)
#importer = importer
#feed = Feed.new(importer)
if #show = Show.where(rss_link: #importer.url).first
create_new_episodes Itunes::Channel.refresh(#feed.origin)
else
create_show_and_episodes
end
end # parse
def create_new_episodes(new_episodes)
new_episodes.each do |new_episode|
#show.episodes.create #feed.episode(new_episode)
end
end # create_new_episodes
def create_show_and_episodes
new_show = Show.new(#feed.show) if (#feed && #feed.show)
if (new_show.save && new_show.entries.any?)
new_show.entries.each do |entry|
new_show.episodes.create #feed.episode(entry)
end
end
end # create_show_and_episodes
end
end
Now you have a Parser.create_new_episodes method that you can test independently. So, your test might look something like:
require 'rspec_helper'
describe Parser do
describe '.create_new_episodes' do
context 'when an initial parse has been completed' do
before(:each) do
first_file = Nokogiri::XML(open('spec/fixtures/feed_1.xml'))
#second_file = Nokogiri::XML(open('spec/fixtures/feed_2.xml'))
Parser.create_show_and_episodes first_file
end
it 'changes Episodes.count by 1' do
expect{Parser.create_new_episodes(#second_file)}.to change{Episodes.count}.by(1)
end
it 'changes Show.count by 0' do
expect{Parser.create_new_episodes(#second_file)}.to change{Show.count}.by(0)
end
end
end
end
Naturally, you'll need feed_1.xml and feed_2.xml in the spec\fixtures directory.
Apologies for any typos. And, I didn't run the code. So, might be buggy. Hope it helps.
I (want to) have a method in a parent model (groups) to check if a child (subjects) has children (goals)
groups.rb:
def has_goals?
#answer = []
subjects = self.subjects
subjects.each do |subject|
if subject.try(:goals).present?
#answer << true
else
#answer << false
end
end
if #answer.include?("true")
true
else
false
end
end
I would use this like so -
if group.has_goals?
# do something
else
# do something else
end
at the moment it's not working as it's returning false for everything - whether the subject has goals or not. Any ideas how to get this working?
Check if any of the subjects has at least a goal (subjects.goals should return [] if the subject has no goals):
def has_goals
subjects.any? { |subject| subject.goals.present? }
end
Enumerable#any? reference: http://ruby-doc.org/core-2.0/Enumerable.html#method-i-any-3F
I have the following model/Admin.rb class that I would like to extract and convert into a lib/UserApi class. I am not familiar into creating lib classes and being able to call them from my controllers. Any advice appreciated.
class Admin
attr_accessor :id
attr_accessor :firstname
attr_accessor :lastname
attr_accessor :usergroups
def initialize json_attrs = {}
#usergroups = []
unless json_attrs.blank?
#id = json_attrs["id"]
#fname = json_attrs["fname"]
#lname = json_attrs["lname"]
#groups = json_attrs["groups"]
#authenticated = true
end
if json_attrs.blank?
#firstname = "blank"
end
end
def is_authenticated?
#authenticated ||= false
end
def in_groups? group_names
return !(#usergroups & group_names).empty? if group_names.kind_of?(Array)
#usergroups.include?(group_names)
end
def authenticate username, password
options={:basic_auth => {:username => CONFIG[:API_CLIENT_NAME],
:password => CONFIG[:API_CLIENT_PASSWORD]}}
api_response = HTTParty.get("#{CONFIG[:API_HOST]}auth/oauth2?username=#{username}&password=#{password}", options)
raise "API at #{CONFIG[:API_HOST]} is not responding" if api_response.code == 500 || api_response.code == 404
if api_response.parsed_response.has_key? "error"
return false
else
initialize(api_response.parsed_response["user"].select {|k,v| ["id", "fname", "lname", "groups"].include?(k) })
#authenticated = true
return true
end
end
def full_name
"#{#name} #{#name}"
end
end
This is what I currently use in the auth_controller"
class Admin::AuthController < Admin::BaseController
def auth
admin_user = Admin.new
auth_result = admin_user.authenticate(params[:username], params[:password])
end
Create the UserApi class in the lib directory:
# lib/user_api.rb
class UserApi
...
Update the controller:
class Admin::AuthController < Admin::BaseController
def auth
admin_user = UserApi.new
auth_result = admin_user.authenticate(params[:username], params[:password])
end
Load the classes you put in your lib/ directory, so they are accessible in the controller: Best way to load module/class from lib folder in Rails 3?
I typically create a config/initializers/00_requires.rb file and require the lib files I need.