Bergrebell / CyberCoach

1 stars 1 forks source link

refactor rest_adapter.rb #24

Closed lexruee closed 9 years ago

lexruee commented 9 years ago

Split the file rest_adapter.rb into multiple files in the lib folder. For example:

Type Class File
Resouce User user.rb
Resource Partnership partnership.rb
Resource Sport sport.rb
AuthProxy auth_proxy.rb
Resource Resource resource.rb
Resource BaseResource base_resource.rb

But pay attention. Each class must be in the right namespace. Wrong example:

class User < BaseResource

end

Correct example:

module RestAdapter
     class User < BaseResource

      end
end

Before you push to dev, please run the tests!

svetakrasikova commented 9 years ago

Thanks, I am looking at them right now anyway, so I'll do that. @lexruee Alex, thanks for your email!

lexruee commented 9 years ago

@svetakrasikova Thanks :-).

svetakrasikova commented 9 years ago

Alex, there's an issue which I don't know how to resolve. I split the adapter into separate files, wrapped every class in the module, as you suggested. When trying to run tests, e.g. user, I get an error that that the user cannot be loaded. Here is my error:

Unable to load user, underlying cause Unable to autoload constant Resource, expected /Users/svetakrasikova/BeNeFri/Winter2014/ASE/CyberCoach/lib/resource.rb to define it

Do you have an idea of what is needed in the resource, what kind of constant?? Trying to google but found nothing so far.

19 Oct 2014, в 20:44, Alexander Rüedlinger notifications@github.com написал(а):

@svetakrasikova Thanks :-).

— Reply to this email directly or view it on GitHub.

lexruee commented 9 years ago

Sorry. I forgot to mention that you need to create a directory called rest_adapter in the lib directory. You can put all the created files there.

lib
|----rest_adapter
          |----user.rb
          |----resource.rb
          |----base_resource.rb 
          | etc.         

http://stackoverflow.com/questions/24111360/rails-4-1-unable-to-autoload-namespaced-class

svetakrasikova commented 9 years ago

Thank you! 19 Oct 2014, в 21:55, Alexander Rüedlinger notifications@github.com написал(а):

Sorry. I forgot to mention that you need to create a directory called rest_adapter in the lib directory. You can put all the created files there.

http://stackoverflow.com/questions/24111360/rails-4-1-unable-to-autoload-namespaced-class

— Reply to this email directly or view it on GitHub.

svetakrasikova commented 9 years ago

Alex, one more question, sorry! I've been trying to do this myself for almost an hour now and am stuck. How should I include the Helper and Privacy modules inside the classes where they are used? I just left them inside the file rest_adpter.rb and get errors like

NameError: uninitialized constant RestAdapter::Helper lib/rest_adapter/auth_proxy.rb:33:in `initialize'

I tried 'include', but it does not work. Maybe these modules now have to be somewhere else.

19 Oct 2014, в 21:56, Sveta Krasikova contact@svetakrasikova.eu написал(а):

Thank you! 19 Oct 2014, в 21:55, Alexander Rüedlinger notifications@github.com написал(а):

Sorry. I forgot to mention that you need to create a directory called rest_adapter in the lib directory. You can put all the created files there.

http://stackoverflow.com/questions/24111360/rails-4-1-unable-to-autoload-namespaced-class

— Reply to this email directly or view it on GitHub.

lexruee commented 9 years ago

Just left them inside the file rest_adapter.rb Have you put the remaining code inside the module RestAdapter?

It should look like this:

module RestAdapter

  # Helper modules

  module Privacy
    Privat = 0
    Member = 1
    Public = 2
  end

  module Helper
    # Builds a basic auth string and returns the final basic auth string.
    # params = {username: username, password: password }
    def self.basic_auth_encryption(params)
      'Basic ' + Base64.encode64("#{params[:username]}:#{params[:password]}")
    end
  end
end  

1) Maybe somewhere the keyword 'end' is missing... 2) The file rest_adapter.rb must be outside of the folder rest_adapter.

svetakrasikova commented 9 years ago

Good morning, had to quit at 11 yesterday: Asya got a fever and an ear pain, was a busy night. Luckily everybody is better today:) Yes, this is how the RestAdapter looks like now, I did not move Helper and Privacy anywhere. But they seem not to be accessible from the classes. I found some discussions on stack overflow, will see what I can do later today.

Отправлено с iPhone

19 окт. 2014 г., в 23:22, Alexander Rüedlinger notifications@github.com написал(а):

Just left them inside the file rest_adapter.rb Have you put the remaining code inside the module RestAdapter?

It should look like this:

module RestAdapter

Helper modules

module Privacy Privat = 0 Member = 1 Public = 2 end

module Helper

Builds a basic auth string and returns the final basic auth string.

# params = {username: username, password: password }
def self.basic_auth_encryption(params)
  'Basic ' + Base64.encode64("#{params[:username]}:#{params[:password]}")
end

end end
— Reply to this email directly or view it on GitHub.

lexruee commented 9 years ago

Can you push it on a separate branch? Maybe I can find out the higgs bugson :-)

svetakrasikova commented 9 years ago

Alex, I just pushed the version with classes in separate files to the brunch subscr_adapter. I will get to this tonight anyway and try to resolve the issue. Talk soon, Sveta

20 Oct 2014, в 12:46, Alexander Rüedlinger notifications@github.com написал(а):

Can you push it on a separate branch? Maybe I can find out the higgs bugson :-)

— Reply to this email directly or view it on GitHub.

lexruee commented 9 years ago

Only the tests are affected. The app still works. So it's might be a kind of naming convention problem in the unit tests.

lexruee commented 9 years ago

Okay, I've found the problem: Line 139 in users.rb

partnerships =  params['partnerships'].map {|p| Partnership.create p }

There we get a 'uninitialized constant Partnership' error.

Simple workaround is to use the module name RestAdapter as prefix before Partnership.

RestAdapter::Partnership.create 

Or much better without hard coding:

module_name = Module.nesting.last  # workaround corresponds to the prefix RestAdapter
module_name::Partnership.create

For me this error is pretty strange. Without this workaround the app still works, only the tests are bitchy :-).

@svetakrasikova I fixed the problem. Can you pull it again and verify that the tests are okay? If everything is okay, merge it into the dev branch :-)

svetakrasikova commented 9 years ago

I'll check right away if it's working and merge. You are a genius!

20 Oct 2014, в 21:26, Alexander Rüedlinger notifications@github.com написал(а):

Okay, I've found the problem: Line 139 in users.rb

partnerships = params['partnerships'].map {|p| Partnership.create p }

There we get a 'uninitialized constant Partnership' error.

Simple workaround is to use the RestAdapter as prefix before Partnership.

RestAdapter::Partnership.create

Or much better without hard coding:

module_name = Module.nesting.last # workaround corresponds to the prefix RestAdapter module_name::Partnership.create For me this error is pretty strange. Without this workaround the app still works, only the tests are bitchy :-).

@svetakrasikova I fixed the problem. Can you pull it again and verify that the tests are okay? If everything is okay, merge it into the dev branch :-)

— Reply to this email directly or view it on GitHub.